Skip to content

Commit

Permalink
Merge pull request #140 from Bamdoliro/perf/#136
Browse files Browse the repository at this point in the history
[개선] 공지사항 여러 파일 업로드
  • Loading branch information
jyj1289 authored Oct 3, 2024
2 parents 33b30f6 + b1f4dd3 commit d2524a1
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
import com.bamdoliro.maru.shared.response.IdResponse;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
@UseCase
public class CreateNoticeUseCase {

private final NoticeRepository noticeRepository;

public IdResponse execute(NoticeRequest request) {
String fileName = request.getFileName();
List<String> fileNameList = request.getFileNameList();
Notice notice = noticeRepository.save(
new Notice(request.getTitle(), request.getContent(), fileName)
new Notice(request.getTitle(), request.getContent(), fileNameList)
);

return new IdResponse(notice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.bamdoliro.maru.domain.notice.domain.Notice;
import com.bamdoliro.maru.infrastructure.s3.FileService;
import com.bamdoliro.maru.infrastructure.s3.constants.FolderConstant;
import com.bamdoliro.maru.presentation.notice.dto.response.DownloadFileResponse;
import com.bamdoliro.maru.presentation.notice.dto.response.NoticeResponse;
import com.bamdoliro.maru.shared.annotation.UseCase;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
@UseCase
public class QueryNoticeUseCase {
Expand All @@ -16,10 +19,15 @@ public class QueryNoticeUseCase {

public NoticeResponse execute(Long id) {
Notice notice = noticeFacade.getNotice(id);
String fileUrl = notice.getFileName() != null ?
fileService.getPresignedUrl(FolderConstant.NOTICE_FILE, notice.getFileName()).getDownloadUrl()
: null;

return new NoticeResponse(notice, fileUrl);
List<DownloadFileResponse> fileList = null;
if (notice.getFileNameList() != null && !notice.getFileNameList().isEmpty()) {
fileList = notice.getFileNameList().stream().map(fileName -> new DownloadFileResponse(
fileService.getPresignedUrl(FolderConstant.NOTICE_FILE, fileName).getDownloadUrl(),
fileName
)).toList();
}

return new NoticeResponse(notice, fileList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class UpdateNoticeUseCase {
@Transactional
public void execute(Long id, NoticeRequest request) {
Notice notice = noticeFacade.getNotice(id);
notice.update(request.getTitle(), request.getContent(), request.getFileName());
notice.update(request.getTitle(), request.getContent(), request.getFileNameList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
import com.bamdoliro.maru.shared.annotation.UseCase;
import lombok.RequiredArgsConstructor;

import java.util.UUID;
import java.util.*;

@RequiredArgsConstructor
@UseCase
public class UploadFileUseCase {

private final FileService fileService;

public UploadFileResponse execute(UploadFileRequest request) {
String fileName = UUID.randomUUID() + "_" + request.getFileName();
public List<UploadFileResponse> execute(UploadFileRequest request) {
List<String> fileNameList = request.getFileNameList().stream()
.map(fileName -> UUID.randomUUID() + "_" + fileName)
.toList();

return new UploadFileResponse(fileService.getPresignedUrl(FolderConstant.NOTICE_FILE, fileName), fileName);
return fileNameList.stream()
.map(fileName -> new UploadFileResponse(
fileService.getPresignedUrl(FolderConstant.NOTICE_FILE, fileName),
fileName
))
.toList();
}
}
23 changes: 11 additions & 12 deletions src/main/java/com/bamdoliro/maru/domain/notice/domain/Notice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.bamdoliro.maru.domain.notice.domain;

import com.bamdoliro.maru.shared.entity.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "tbl_notice")
Expand All @@ -29,19 +26,21 @@ public class Notice extends BaseTimeEntity {
@Column(nullable = false, length = 1024)
private String content;

@Column(unique = true, nullable = true)
private String fileName;
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "tbl_notice_file",
joinColumns = @JoinColumn(name = "notice_id"))
private List<String> fileNameList;

@Builder
public Notice(String title, String content, String fileName) {
public Notice(String title, String content, List<String> fileNameList) {
this.title = title;
this.content = content;
this.fileName = fileName;
this.fileNameList = fileNameList;
}

public void update(String title, String content, String fileName) {
public void update(String title, String content, List<String> fileName) {
this.title = title;
this.content = content;
this.fileName = fileName;
this.fileNameList = fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public SingleCommonResponse<IdResponse> createNotice(
}

@PostMapping("/file")
public SingleCommonResponse<UploadFileResponse> uploadFile(
public ListCommonResponse<UploadFileResponse> uploadFile(
@AuthenticationPrincipal(authority = Authority.ADMIN) User user,
@RequestBody @Valid UploadFileRequest request
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -21,5 +23,6 @@ public class NoticeRequest {
private String content;

@Nullable
private String fileName;
@Size(max = 3, message = "최대 3개의 파일만 업로드할 수 있습니다.")
private List<String> fileNameList;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.bamdoliro.maru.presentation.notice.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UploadFileRequest {

@NotBlank(message = "필수값입니다.")
private String fileName;
@NotEmpty(message = "필수값입니다.")
@Size(max = 3, message = "최대 3개의 파일만 업로드할 수 있습니다.")
private List<String> fileNameList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bamdoliro.maru.presentation.notice.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class DownloadFileResponse {

private final String downloadUrl;
private final String fileName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;

@Getter
public class NoticeResponse {

private final String title;
private final String content;
private final String fileName;
private final String fileUrl;
private final List<DownloadFileResponse> fileList;
private final LocalDateTime createdAt;
private final LocalDateTime updatedAt;

public NoticeResponse(Notice notice, String fileUrl) {
public NoticeResponse(Notice notice, List<DownloadFileResponse> fileList) {
this.title = notice.getTitle();
this.content = notice.getContent();
this.fileName = notice.getFileName();
this.fileUrl = fileUrl;
this.fileList = fileList;
this.createdAt = notice.getCreatedAt();
this.updatedAt = notice.getUpdatedAt();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.bamdoliro.maru.presentation.notice.dto.response;

import com.bamdoliro.maru.infrastructure.s3.dto.response.UrlResponse;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class UploadFileResponse {

private final UrlResponse url;

private final String fileName;

public UploadFileResponse(UrlResponse url, String fileName) {
this.url = url;
this.fileName = fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -33,7 +34,7 @@ class CreateNoticeUseCaseTest {
void 유저가_공지사항을_생성한다() {
// given
Notice notice = NoticeFixture.createNotice();
NoticeRequest request = new NoticeRequest(notice.getTitle(), notice.getContent(), UUID.randomUUID().toString());
NoticeRequest request = new NoticeRequest(notice.getTitle(), notice.getContent(), List.of(UUID.randomUUID().toString()));
ArgumentCaptor<Notice> captor = ArgumentCaptor.forClass(Notice.class);

given(noticeRepository.save(any(Notice.class))).willReturn(notice);
Expand All @@ -47,6 +48,6 @@ class CreateNoticeUseCaseTest {
assertEquals(response.getId(), savedNotice.getId());
assertEquals(request.getTitle(), savedNotice.getTitle());
assertEquals(request.getContent(), savedNotice.getContent());
assertEquals(request.getFileName(), savedNotice.getFileName() != null ? savedNotice.getFileName() : null);
assertEquals(request.getFileNameList(), savedNotice.getFileNameList() != null ? savedNotice.getFileNameList() : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -30,7 +32,7 @@ class UpdateNoticeUseCaseTest {
void 유저가_공지사항을_수정한다() {
//given
Notice notice = NoticeFixture.createNotice();
NoticeRequest request = new NoticeRequest("제목 바뀌나?", "내용도 바뀌나?", "파일도 바뀌나?.pdf");
NoticeRequest request = new NoticeRequest("제목 바뀌나?", "내용도 바뀌나?", List.of("파일도 바뀌나?.pdf"));

given(noticeFacade.getNotice(notice.getId())).willReturn(notice);

Expand All @@ -41,14 +43,14 @@ class UpdateNoticeUseCaseTest {
verify(noticeFacade, times(1)).getNotice(notice.getId());
assertEquals(request.getTitle(), notice.getTitle());
assertEquals(request.getContent(), notice.getContent());
assertEquals(request.getFileName(), notice.getFileName() != null ? notice.getFileName() : null);
assertEquals(request.getFileNameList(), notice.getFileNameList() != null ? notice.getFileNameList() : null);
}

@Test
void 유저가_공지사항을_수정할_때_공지사항이_없으면_에러가_발생한다() {
// given
Notice notice = NoticeFixture.createNotice();
NoticeRequest request = new NoticeRequest("제목 바뀌나?", "내용도 바뀌나?", "파일도 바뀌나?.pdf");
NoticeRequest request = new NoticeRequest("제목 바뀌나?", "내용도 바뀌나?", List.of("파일도 바뀌나?.pdf"));

given(noticeFacade.getNotice(notice.getId())).willThrow(NoticeNotFoundException.class);

Expand All @@ -58,6 +60,6 @@ class UpdateNoticeUseCaseTest {
verify(noticeFacade, times(1)).getNotice(notice.getId());
assertNotEquals(request.getTitle(), notice.getTitle());
assertNotEquals(request.getContent(), notice.getContent());
assertNotEquals(request.getFileName(), notice.getFileName() != null ? notice.getFileName() : null);
assertNotEquals(request.getFileNameList(), notice.getFileNameList() != null ? notice.getFileNameList() : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
Expand All @@ -27,7 +29,7 @@ public class UploadFileUseCaseTest {
void 공지사항_파일을_업로드한다() {
// given
given(fileService.getPresignedUrl(any(String.class), any(String.class))).willReturn(SharedFixture.createNoticeFileUrlResponse());
UploadFileRequest request = new UploadFileRequest("notice-file.pdf");
UploadFileRequest request = new UploadFileRequest(List.of("notice-file.pdf"));

// when
uploadFileUseCase.execute(request);
Expand Down
Loading

0 comments on commit d2524a1

Please sign in to comment.