Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 파일 업로드 관련 API 수정 #58

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.potatocake.everymoment.controller;

import com.potatocake.everymoment.dto.SuccessResponse;
import com.potatocake.everymoment.dto.request.FileRequest;
import com.potatocake.everymoment.dto.response.FileResponse;
import com.potatocake.everymoment.security.MemberDetails;
import com.potatocake.everymoment.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down Expand Up @@ -34,10 +34,12 @@ public class FileController {
private final FileService fileService;

@Operation(summary = "파일 목록 조회", description = "특정 일기의 파일 목록을 조회합니다.")
@ApiResponse(responseCode = "200", description = "파일 목록 조회 성공", content = @Content(schema = @Schema(implementation = FileResponse.class)))
@ApiResponse(responseCode = "200", description = "파일 목록 조회 성공", content = @Content(array = @ArraySchema(schema = @Schema(implementation = FileResponse.class))))
@GetMapping
public ResponseEntity<SuccessResponse<FileResponse>> getFiles(@Parameter(description = "조회할 일기 ID", required = true)
@PathVariable Long diaryId) {
public ResponseEntity<SuccessResponse<List<FileResponse>>> getFiles(
@Parameter(description = "조회할 일기 ID", required = true)
@PathVariable Long diaryId
) {
List<FileResponse> files = fileService.getFiles(diaryId);

return ResponseEntity.ok()
Expand All @@ -53,11 +55,9 @@ public ResponseEntity<SuccessResponse> uploadFiles(
@Parameter(description = "인증된 사용자 정보", hidden = true)
@AuthenticationPrincipal MemberDetails memberDetails,
@Parameter(description = "업로드할 파일 목록", required = true)
@RequestPart List<MultipartFile> files,
@Parameter(description = "일기에 보일 파일 이름과 순서", required = true)
@RequestPart List<FileRequest> info
@RequestPart List<MultipartFile> files
) {
fileService.uploadFiles(diaryId, memberDetails.getId(), files, info);
fileService.uploadFiles(diaryId, memberDetails.getId(), files);

return ResponseEntity.ok()
.body(SuccessResponse.ok());
Expand All @@ -72,11 +72,9 @@ public ResponseEntity<SuccessResponse> updateFiles(
@Parameter(description = "인증된 사용자 정보", hidden = true)
@AuthenticationPrincipal MemberDetails memberDetails,
@Parameter(description = "수정할 파일 목록", required = true)
@RequestPart List<MultipartFile> files,
@Parameter(description = "일기에 보일 파일 이름과 순서", required = true)
@RequestPart List<FileRequest> info
@RequestPart List<MultipartFile> files
) {
fileService.updateFiles(diaryId, memberDetails.getId(), files, info);
fileService.updateFiles(diaryId, memberDetails.getId(), files);

return ResponseEntity.ok()
.body(SuccessResponse.ok());
Expand Down

This file was deleted.

21 changes: 6 additions & 15 deletions src/main/java/com/potatocake/everymoment/service/FileService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.potatocake.everymoment.service;

import com.potatocake.everymoment.dto.request.FileRequest;
import com.potatocake.everymoment.dto.response.FileResponse;
import com.potatocake.everymoment.entity.Diary;
import com.potatocake.everymoment.entity.File;
Expand All @@ -11,8 +10,6 @@
import com.potatocake.everymoment.util.S3FileUploader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -34,29 +31,23 @@ public List<FileResponse> getFiles(Long diaryId) {
.toList();
}

public void uploadFiles(Long diaryId, Long memberId, List<MultipartFile> files, List<FileRequest> infos) {
public void uploadFiles(Long diaryId, Long memberId, List<MultipartFile> files) {
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(() -> new GlobalException(ErrorCode.DIARY_NOT_FOUND));

diary.checkOwner(memberId);

Map<String, MultipartFile> fileMap = files.stream()
.collect(Collectors.toMap(MultipartFile::getOriginalFilename, f -> f, (f1, f2) -> f1));

List<File> fileEntities = new ArrayList<>();

for (FileRequest info : infos) {
MultipartFile file = fileMap.get(info.getFilename());
if (file == null) {
throw new GlobalException(ErrorCode.FILE_NOT_FOUND);
}
for (int i = 0; i < files.size(); i++) {
MultipartFile file = files.get(i);

String url = uploader.uploadFile(file);

File fileEntity = File.builder()
.diary(diary)
.imageUrl(url)
.order(info.getOrder())
.order(i + 1)
.build();

fileEntities.add(fileEntity);
Expand All @@ -65,15 +56,15 @@ public void uploadFiles(Long diaryId, Long memberId, List<MultipartFile> files,
fileRepository.saveAll(fileEntities);
}

public void updateFiles(Long diaryId, Long memberId, List<MultipartFile> files, List<FileRequest> infos) {
public void updateFiles(Long diaryId, Long memberId, List<MultipartFile> files) {
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(() -> new GlobalException(ErrorCode.DIARY_NOT_FOUND));

diary.checkOwner(memberId);

fileRepository.deleteByDiary(diary);

uploadFiles(diaryId, memberId, files, infos);
uploadFiles(diaryId, memberId, files);
}

}
Loading