Skip to content

Commit

Permalink
Chore: 큐시트 파일 업로드 filepath 업데이트 (#34)
Browse files Browse the repository at this point in the history
* Chore: 큐시트 파일 업로드 수정

---------

Co-authored-by: yby654 <yby654321@gmail.com>
  • Loading branch information
yby654 and yby654321 authored Oct 15, 2023
1 parent 1cd9753 commit 87f9914
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 23 deletions.
24 changes: 19 additions & 5 deletions nw/src/main/java/lab/cherry/nw/controller/QsheetController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand Down Expand Up @@ -106,8 +107,16 @@ public ResponseEntity<?> findAllQsheets(
@ApiResponse(responseCode = "400", description = "입력 값이 잘못 되었습니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
})
@Operation(summary = "Qsheet 생성", description = "Qsheet를 추가합니다.")
public ResponseEntity<?> createQsheet(@Valid @RequestPart QsheetEntity.QsheetCreateDto qsheetCreateDto, @RequestPart List<MultipartFile> files) {
public ResponseEntity<?> createQsheet(@RequestPart QsheetEntity.QsheetCreateDto qsheetCreateDto, @RequestPart(required = false) List<MultipartFile> files) {
log.info("[QsheetController] createQsheet...!");
log.error ("files : {}", files);
for(MultipartFile file:files){
if(file.isEmpty()){
files = null;
break;
}
}

qsheetService.createQsheet(qsheetCreateDto, files);

final ResultResponse response = ResultResponse.of(SuccessCode.OK);
Expand All @@ -129,11 +138,16 @@ public ResponseEntity<?> createQsheet(@Valid @RequestPart QsheetEntity.QsheetCre
@Operation(summary = "큐시트 업데이트", description = "특정 큐시트를 업데이트합니다.")
public ResponseEntity<?> updateById(
@PathVariable("id") String id,
@RequestBody QsheetEntity.QsheetUpdateDto qsheetUpdateDto) {

@RequestPart QsheetEntity.QsheetUpdateDto qsheetUpdateDto, @RequestPart(required = false) List<MultipartFile> files) {
log.info("[QsheetController] updateQsheet...!");

qsheetService.updateById(id, qsheetUpdateDto);
for(MultipartFile file:files){
if(file.isEmpty()){
files = null;
break;
}
}
qsheetService.updateById(id, qsheetUpdateDto, files);

// final ResultResponse response = ResultResponse.of(SuccessCode.OK);
return new ResponseEntity<>(qsheetService.findById(id), new HttpHeaders(), HttpStatus.OK);
Expand Down
2 changes: 2 additions & 0 deletions nw/src/main/java/lab/cherry/nw/model/QsheetEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void sortDataByOrderIndex() {
data.sort(Comparator.comparingInt(ItemData::getOrderIndex));
}
}

// public void sortDataByOrderIndex() {
// if (data != null) {
// data = data.entrySet()
Expand All @@ -175,6 +176,7 @@ public void sortDataByOrderIndex() {
// ));
// }
// }

@Getter
@Builder
@NoArgsConstructor @AllArgsConstructor
Expand Down
103 changes: 86 additions & 17 deletions nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lab.cherry.nw.model.OrgEntity;
import lab.cherry.nw.model.QsheetEntity;
import lab.cherry.nw.model.UserEntity;
import lab.cherry.nw.model.QsheetEntity.ItemData;
import lab.cherry.nw.repository.QsheetRepository;
import lab.cherry.nw.service.FileService;
import lab.cherry.nw.service.OrgService;
Expand All @@ -17,8 +18,6 @@
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -96,26 +95,48 @@ public void createQsheet(QsheetEntity.QsheetCreateDto qsheetCreateDto, List<Mult
}

////////////
List<ItemData> newItemData = new ArrayList<>();
if (files != null){
Map<String, String> info = new HashMap<>();
info.put("type", "사용자");
info.put("user", userEntity.getId());
info.put("qsheetSeq", objectid.toString());

log.error("name {}", qsheetCreateDto.getName());
log.error("memo {}", qsheetCreateDto.getMemo());

Map<String, String> info = new HashMap<>();
info.put("type", "사용자");
info.put("user", userEntity.getId());
info.put("qsheetSeq", objectid.toString());
List<String> fileUrls = fileService.uploadFiles(info, files);
log.error("fileUrls {}", fileUrls);
////////////


List<String> fileUrls = fileService.uploadFiles(info, files);
log.error("fileUrls {}", fileUrls);
////////////
for (ItemData data : qsheetCreateDto.getData()) {
for (String filePath : fileUrls) {
if (filePath.contains(data.getFilePath())) {
ItemData tempData = ItemData.builder()
.orderIndex(data.getOrderIndex())
.process(data.getProcess())
.content(data.getContent())
.actor(data.getActor())
.note(data.getNote())
.filePath(filePath)
.build();
data = tempData;
break;
}
}
newItemData.add(data);
}
}else{
newItemData = qsheetCreateDto.getData();
}



QsheetEntity qsheetEntity = QsheetEntity.builder()
.id(objectid.toString())
.userid(userEntity)
.orgid(orgEntity)
.name(qsheetCreateDto.getName())
.data(qsheetCreateDto.getData())
.data(newItemData)
// .data(qsheetCreateDto.getData())
.memo(qsheetCreateDto.getMemo())
.org_approver(orgUserEntity)
.org_confirm(false)
Expand All @@ -138,11 +159,12 @@ public void createQsheet(QsheetEntity.QsheetCreateDto qsheetCreateDto, List<Mult
*
* Author : yby654(yby654@github.com)
*/
public void updateById(String id, QsheetEntity.QsheetUpdateDto qsheetUpdateDto) {
public void updateById(String id, QsheetEntity.QsheetUpdateDto qsheetUpdateDto, List<MultipartFile> files) {
Instant instant = Instant.now();
QsheetEntity qsheetEntity = findById(id);
List<ItemData> newItemData =qsheetEntity.getData();

if (qsheetEntity.getData() != null ) {
if (qsheetEntity != null ) {
// qsheetEntity.updateFromDto(qsheetUpdateDto);
// qsheetRepository.save(qsheetEntity);
OrgEntity orgEntity = qsheetEntity.getOrgid();
Expand All @@ -156,16 +178,63 @@ public void updateById(String id, QsheetEntity.QsheetUpdateDto qsheetUpdateDto)
}else{
log.error("[QsheetServiceImpl - udpateQsheet] org_approver와 isOrg_confirm 입력이 잘못되었습니다.");
throw new CustomException(ErrorCode.INVALID_INPUT_VALUE);
}
}

}
if(files!=null){
newItemData= new ArrayList<>();
Map<String, String> info = new HashMap<>();
info.put("type", "사용자");
info.put("user", qsheetEntity.getUserid().getId());
info.put("qsheetSeq", qsheetEntity.getId().toString());

List<String> fileUrls = fileService.uploadFiles(info, files);
log.error("fileUrls {}", fileUrls);
////////////

for (ItemData data : qsheetUpdateDto.getData()) {
for (String filePath : fileUrls) {
if (filePath.contains(data.getFilePath())) {
ItemData tempData = ItemData.builder()
.orderIndex(data.getOrderIndex())
.process(data.getProcess())
.content(data.getContent())
.actor(data.getActor())
.note(data.getNote())
.filePath(filePath)
.build();
data = tempData;
break;
}
}
newItemData.add(data);
}


}else if(qsheetUpdateDto.getData()!=null && files==null){
newItemData= new ArrayList<>();
for(ItemData data : qsheetUpdateDto.getData()){
ItemData tempData = ItemData.builder()
.orderIndex(data.getOrderIndex())
.process(data.getProcess())
.content(data.getContent())
.actor(data.getActor())
.note(data.getNote())
.filePath(data.getFilePath())
.build();
newItemData.add(tempData);
}
}



qsheetEntity = QsheetEntity.builder()
.id(qsheetEntity.getId())
.name(qsheetEntity.getName())
.orgid(orgEntity)
.userid(qsheetEntity.getUserid())
.created_at(qsheetEntity.getCreated_at())
.data(qsheetUpdateDto.getData()!=null?qsheetUpdateDto.getData():qsheetEntity.getData())
.data(newItemData)
.org_approver(orgUserEntity)
.org_confirm(qsheetUpdateDto.isOrg_confirm()==!(qsheetEntity.isOrg_confirm())?qsheetUpdateDto.isOrg_confirm():qsheetEntity.isOrg_confirm())
.client_confirm(qsheetUpdateDto.isClient_confirm()==!(qsheetEntity.isClient_confirm())?qsheetUpdateDto.isClient_confirm():qsheetEntity.isClient_confirm())
Expand Down
2 changes: 1 addition & 1 deletion nw/src/main/java/lab/cherry/nw/service/QsheetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface QsheetService {
// QsheetEntity findByUserId(String userid);
// QsheetEntity findByOrgId(String orgid);
void createQsheet(QsheetEntity.QsheetCreateDto qsheetCreateDto, List<MultipartFile> files);
void updateById(String id, QsheetEntity.QsheetUpdateDto updateDto);
void updateById(String id, QsheetEntity.QsheetUpdateDto updateDto, List<MultipartFile> files);
void deleteById(String id);
Page<QsheetEntity> findPageByUserId(String userid, Pageable pageable);
Page<QsheetEntity> findPageByOrgId(String orgid, Pageable pageable);
Expand Down
6 changes: 6 additions & 0 deletions nw/src/main/java/lab/cherry/nw/util/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ public static String getFileExtension(MultipartFile file) {
return ""; // 확장자가 없을 경우 빈 문자열 반환
}
}

public String getFileNameFromPath(String path) {
String[] parts = path.split("/");
return parts[parts.length - 1];
}

}

0 comments on commit 87f9914

Please sign in to comment.