Skip to content

Commit

Permalink
[hotfix]: 자료집 수정 api 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
qogustj committed Oct 13, 2024
1 parent 581f79f commit eb5e699
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ public ResponseEntity<ApiResponse<?>> createBoardPost(@Parameter(hidden = true)
기본적으로 액세스 토큰을 필요로 합니다.
요청 path에 fileCategory(카테고리 ex.총학생회칙) 값을 문자열 형태로 넣으면 됩니다.
""")
@PostMapping("/data/{fileCategory}/{fileType}/post")
@PostMapping("/data/{fileCategory}/post")
public ResponseEntity<ApiResponse<?>> createDataPost(@Parameter(hidden = true) @UserId Long userId,
@PathVariable(name = "fileCategory") String fileCategory,
@PathVariable(name = "fileType") String fileType,
@RequestBody PostCreateRequest postCreateRequest) {
return ApiResponse.success(postManageService.createDataPost(userId, fileCategory, fileType, postCreateRequest));
return ApiResponse.success(postManageService.createDataPost(userId, fileCategory, postCreateRequest));
}

@Operation(summary = "게시물 생성 시 파일 및 이미지 저장 api", description = """
Expand All @@ -110,19 +109,17 @@ public ResponseEntity<ApiResponse<?>> createDataPost(@Parameter(hidden = true) @
이 반환값을 가지고 게시물 생성 api에 있는 dto의 List에 url를 보내주어야 합니다.
기본적으로 액세스 토큰을 필요로 합니다.
PathVariable로 노션에 있는 boardCode중 하나를 적습니다.
s3에 저장할 MultipartFile 형식의 파라미터를 2개 두었습니다.
첫번째 파라미터는 files라는 key값으로 보내주면 됩니다.
두번째 파라미터는 images라는 key값으로 보내주면 됩니다.
s3에 저장할 MultipartFile 형식을 fileType에 담아주시면 됩니다.
이미지와 파일을 경로를 구분하여 저장하기 위해 이러한 방식을 선택했습니다.
기본적인 api로직은 받은 파라미터를 통해 파일들을 먼저 s3에 저장하고 db에 순차적으로 생성된 id와 url을 하나의 dto로 이를 List로 반환합니다.
""")
@PostMapping(value = "/{boardCode}/files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
@PostMapping(value = "/{boardCode}/files/{fileType}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ApiResponse<?>> createBoardPostFile(@Parameter(hidden = true) @UserId Long userId,
@PathVariable(name = "boardCode") String boardCode,
@RequestPart(value = "files", required = false) MultipartFile[] files,
@RequestPart(value = "images", required = false) MultipartFile[] images) {
return ApiResponse.success(postManageService.createBoardPostFile(userId, boardCode, files, images));
@PathVariable(name = "fileType") String fileType) {
return ApiResponse.success(postManageService.createBoardPostFile(userId, boardCode, files, fileType));
}

@Operation(summary = "게시물 단건 조회 후 파일 혹은 이미지 삭제 api", description = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,16 @@ public PostCreateResponse createBoardPost(Long userId, String boardCode, PostCre
}

@Transactional
public PostCreateResponse createDataPost(Long userId, String fileCategory, String fileType, PostCreateRequest postCreateRequest){
public PostCreateResponse createDataPost(Long userId, String fileCategory, PostCreateRequest postCreateRequest){
Board board = boardReader.getBoardWithBoardCode(BoardCode.DATA.getStringBoardCode());
Post post = postAppender.createPost(postCreateRequest.toDomain(board.getId(), userId, Category.getEnumCategoryCodeFromStringCategoryCode(fileType)));
Post post = postAppender.createPost(postCreateRequest.toDomain(board.getId(), userId, Category.getEnumCategoryCodeFromStringCategoryCode(postCreateRequest.categoryCode())));
postFileAppender.updatePostIdAndFileCategoryForIds(postCreateRequest.postFileList(), post.getId(), fileCategory);
return PostCreateResponse.of(post.getId(), BoardCode.DATA.getStringBoardCode());
}

@Transactional
public PostFileListResponse createBoardPostFile(Long userId, String boardCode, MultipartFile[] files, MultipartFile[] images){
PostFileMediatorResponse response = s3utils.uploadFileWithPath(userId, boardCode, files, images);
public PostFileListResponse createBoardPostFile(Long userId, String boardCode, MultipartFile[] files, String fileType){
PostFileMediatorResponse response = s3utils.uploadFileWithPath(userId, boardCode, files, fileType);
List<PostFile> postFiles = convertUrlsToPostFiles(response);
List<PostFile> afterSaveList = postFileAppender.saveAllPostFile(postFiles);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum Category {
AUDIT_RESULT("감사결과"),
ETC("기타"),

//자료집카테고리
//자료집카테고리(이거 나중에 없앨 예정)
RESULT_REPORT("결과보고서"),
MINUTES("회의록"),
STUDENT_COUNCIL_RULES("총학생회칙"),
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/ussum/homepage/infra/utils/S3utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ public String uploadFile(MultipartFile file) {
return amazonS3.getUrl(bucket, originalFilename).toString();
}

public PostFileMediatorResponse uploadFileWithPath(Long userId, String boardCode, MultipartFile[] files, MultipartFile[] images) {
public PostFileMediatorResponse uploadFileWithPath(Long userId, String boardCode, MultipartFile[] files, String fileType) {
List<Map<String, String>> uploadedFileUrls = new ArrayList<>();
List<String> originalFileNames = new ArrayList<>();

if (images != null && images.length > 0) {
PostFileMediatorResponse imageResponse = uploadFiles(userId, boardCode, images, "images");
if (files != null && files.length > 0) {
PostFileMediatorResponse imageResponse = uploadFiles(userId, boardCode, files, fileType);
uploadedFileUrls.addAll(imageResponse.urlList());
originalFileNames.addAll(formatOriginalFileNames(imageResponse.originalFileNames()));
}

if (files != null && files.length > 0) {
PostFileMediatorResponse fileResponse = uploadFiles(userId, boardCode, files, "files");
uploadedFileUrls.addAll(fileResponse.urlList());
originalFileNames.addAll(formatOriginalFileNames(fileResponse.originalFileNames()));
}
//
// if (files != null && files.length > 0) {
// PostFileMediatorResponse fileResponse = uploadFiles(userId, boardCode, files, "files");
// uploadedFileUrls.addAll(fileResponse.urlList());
// originalFileNames.addAll(formatOriginalFileNames(fileResponse.originalFileNames()));
// }

return PostFileMediatorResponse.of(originalFileNames, uploadedFileUrls);
}
Expand Down

0 comments on commit eb5e699

Please sign in to comment.