Skip to content

Commit

Permalink
Merge pull request #131 from Katchup-dev/fix/#130-file-and-screenshot…
Browse files Browse the repository at this point in the history
…-delete-api

[FIX] 업무 카드 생성 전, 파일 및 스크린샷 삭제 로직 수정
  • Loading branch information
yeseul106 authored Aug 26, 2023
2 parents 07d7978 + 3789d82 commit 788a1e3
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -35,9 +36,10 @@ public class CardController {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ApiResponseDto createCard(Principal principal,
@Valid @RequestBody CardCreateRequestDto cardCreateRequestDto ) {
@Valid @RequestBody CardCreateRequestDto cardCreateRequestDto, HttpServletResponse response) {
Long memberId = MemberUtil.getMemberId(principal);
cardService.createCard(memberId, cardCreateRequestDto);
Long cardId = cardService.createCard(memberId, cardCreateRequestDto);
response.addHeader("Location", String.valueOf(cardId));
return ApiResponseDto.success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public class CardListGetResponseDto {
private List<KeywordGetResponseDto> keywordList = new ArrayList<>();
private String content;
private Boolean existFile;
private Boolean existScreenshot;

public static CardListGetResponseDto of (Card card, SubTask subTask, List<KeywordGetResponseDto> keywordList) {
Boolean existFile = card.getFiles().isEmpty() ? false : true;
Boolean existScreenshot = card.getScreenshots().isEmpty() ? false : true;
return CardListGetResponseDto.builder()
.cardId(card.getId())
.subTaskId(subTask.getId())
Expand All @@ -33,6 +35,7 @@ public static CardListGetResponseDto of (Card card, SubTask subTask, List<Keywor
.keywordList(keywordList)
.content(card.getContent())
.existFile(existFile)
.existScreenshot(existScreenshot)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface CardService {
List<CardListGetResponseDto> getCardList(Long taskId);
CardGetResponseDto getCard(Long cardId);
void deleteCardList(CardDeleteRequestDto cardDeleteRequestDto);
void createCard(Long memberId, CardCreateRequestDto cardCreateRequestDto);
Long createCard(Long memberId, CardCreateRequestDto cardCreateRequestDto);
void updateCard(Long memberId, Long cardId, CardUpdateRequestDto cardUpdateRequestDto);

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void deleteCardList(CardDeleteRequestDto cardDeleteRequestDto) {

@Override
@Transactional
public void createCard(Long memberId, CardCreateRequestDto requestDto) {
public Long createCard(Long memberId, CardCreateRequestDto requestDto) {

SubTask subTask;
if (requestDto.getSubTaskId() == SUB_TASK_ETC_ID) {
Expand Down Expand Up @@ -125,7 +125,8 @@ public void createCard(Long memberId, CardCreateRequestDto requestDto) {
for (ScreenshotCreateRequestDto screenshotInfo : requestDto.getScreenshotList()) {
Screenshot newScreenshot = Screenshot.builder()
.id(screenshotInfo.getScreenshotUUID())
.screenshotKey(screenshotService.createKey(memberId, screenshotInfo))
.screenshotKey(screenshotService.createKey(memberId, screenshotInfo.getScreenshotUploadDate(), screenshotInfo.getScreenshotUUID().toString(),
screenshotInfo.getScreenshotName()))
.url(screenshotService.findUrl(memberId, screenshotInfo))
.card(savedCard)
.build();
Expand All @@ -146,14 +147,18 @@ public void createCard(Long memberId, CardCreateRequestDto requestDto) {
for (FileCreateRequestDto fileInfo : requestDto.getFileList()) {
File newFile = File.builder()
.id(fileInfo.getFileUUID())
.fileKey(fileService.createKey(memberId, fileInfo))
.name(fileInfo.getFileName())
.fileKey(fileService.createKey(memberId, fileInfo.getFileUploadDate(), fileInfo.getFileUUID().toString()
, fileInfo.getFileOriginalName()))
.originalName(fileInfo.getFileOriginalName())
.changedName(fileInfo.getFileChangedName())
.size(fileInfo.getSize())
.card(savedCard)
.build();

fileRepository.save(newFile);
}

return savedCard.getId();
}

@Override
Expand Down Expand Up @@ -185,7 +190,8 @@ public void updateCard(Long memberId, Long cardId, CardUpdateRequestDto requestD
for (ScreenshotCreateRequestDto screenshotInfo : requestDto.getScreenshotList()) {
Screenshot newScreenshot = Screenshot.builder()
.id(screenshotInfo.getScreenshotUUID())
.screenshotKey(screenshotService.createKey(memberId, screenshotInfo))
.screenshotKey(screenshotService.createKey(memberId, screenshotInfo.getScreenshotUploadDate(), screenshotInfo.getScreenshotUUID().toString(),
screenshotInfo.getScreenshotName()))
.url(screenshotService.findUrl(memberId, screenshotInfo))
.card(card)
.build();
Expand All @@ -196,8 +202,10 @@ public void updateCard(Long memberId, Long cardId, CardUpdateRequestDto requestD
for (FileCreateRequestDto fileInfo : requestDto.getFileList()) {
File newFile = File.builder()
.id(fileInfo.getFileUUID())
.fileKey(fileService.createKey(memberId, fileInfo))
.name(fileInfo.getFileName())
.fileKey(fileService.createKey(memberId, fileInfo.getFileUploadDate(), fileInfo.getFileUUID().toString()
, fileInfo.getFileOriginalName()))
.originalName(fileInfo.getFileOriginalName())
.changedName(fileInfo.getFileChangedName())
.size(fileInfo.getSize())
.card(card)
.build();
Expand Down Expand Up @@ -260,7 +268,7 @@ private List<ScreenshotGetResponseDto> getScreenshotDtoList(Long cardId) {

private List<FileGetResponseDto> getFileDtoList(Long cardId) {
return cardRepository.findByIdOrThrow(cardId).getFiles().stream()
.map(file -> FileGetResponseDto.of(file.getId(), file.getName(), file.getSize()))
.map(file -> FileGetResponseDto.of(file.getId(), file.getOriginalName(), file.getChangedName(), file.getSize()))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public ApiResponseDto<FileGetUploadPreSignedResponseDto> upload(Principal princi
@ApiResponse(responseCode = "400", description = "파일 삭제 실패", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@DeleteMapping("/files/{fileId}")
@DeleteMapping("/files")
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto delete(@PathVariable String fileId) {
fileService.deleteFile(fileId);
public ApiResponseDto delete(Principal principal, @RequestParam(value = "fileOriginalName") String fileOriginalName
, @RequestParam(value = "fileUploadDate") String fileUploadDate, @RequestParam(value = "fileUUID") String fileUUID) {
Long memberId = MemberUtil.getMemberId(principal);
fileService.deleteFile(memberId, fileOriginalName, fileUploadDate, fileUUID);
return success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public class File extends BaseEntity {
private UUID id;

@Column(nullable = false)
private String name;
private String originalName;

@Column(nullable = false)
private String changedName;

@Column(nullable = false, name = "file_key")
private String fileKey;
Expand All @@ -34,9 +37,10 @@ public class File extends BaseEntity {
private Card card;

@Builder
public File(UUID id, String name, String fileKey, int size, Card card) {
public File(UUID id, String originalName, String changedName, String fileKey, int size, Card card) {
this.id = id;
this.name = name;
this.originalName = originalName;
this.changedName = changedName;
this.fileKey = fileKey;
this.card = card;
this.size = size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ public class FileCreateRequestDto {
@NotNull
private UUID fileUUID;

@Schema(description = "파일 이름", example = "와이어프레임 및 드라이브 사용법.pdf")
@Schema(description = "파일 원본 이름", example = "와이어프레임 및 드라이브 사용법.pdf")
@NotNull
private String fileName;
private String fileOriginalName;

@Schema(description = "파일 변경된 이름", example = "카테고리_업무_세부업무_와이어프레임 사용법.pdf")
@NotNull
private String fileChangedName;

@Schema(description = "파일 업로드 일자", example = "2023/08/23")
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public class FileGetResponseDto {
@Schema(description = "파일 고유 id", example = "ddwd-wdwd-wdwd-wdwdwdwd")
private UUID id;

@Schema(description = "파일 이름", example = "와이어프레임 및 드라이브 사용법.pdf")
private String name;
@Schema(description = "파일 원본 이름", example = "와이어프레임 및 드라이브 사용법.pdf")
private String originalName;

@Schema(description = "파일 변경된 이름", example = "카테고리_업무_세부업무_와이어프레임 사용법.pdf")
private String changedName;

@Schema(description = "파일 사이즈 (KB 단위로 저장)", example = "189277")
private int size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ default File findByIdOrThrow(UUID uuid) {
return findById(uuid).orElseThrow(
() -> new NotFoundException(ErrorCode.NOT_FOUND_FILE));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public interface FileService {

FileGetDownloadPreSignedResponseDto getDownloadPreSignedUrl(String filePath, String fileName);

String createKey(Long memberId, FileCreateRequestDto requestDto);
String createKey(Long memberId, String fileDate, String fileUUID, String fileName);

void deleteFile(String fileId);
void deleteFile(Long memberId, String fileOriginalName, String fileUploadDate, String fileUUID);

FileGetUploadPreSignedResponseDto getUploadPreSignedUrl(Long memberId, String fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import site.katchup.katchupserver.common.util.S3Util;

import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;

@Service
Expand Down Expand Up @@ -47,18 +48,18 @@ public FileGetDownloadPreSignedResponseDto getDownloadPreSignedUrl(String fileUU

@Override
@Transactional
public String createKey(Long memberId, FileCreateRequestDto requestDto) {
public String createKey(Long memberId, String fileDate, String fileUUID, String fileName) {
String fileUploadPrefix = makeUploadPrefix(memberId);
return fileUploadPrefix + "/" + requestDto.getFileUploadDate() + "/" + requestDto.getFileUUID() + requestDto.getFileName();
return fileUploadPrefix + "/" + fileDate + "/" + fileUUID + fileName;
}

@Override
@Transactional
public void deleteFile(String fileId) {
File file = fileRepository.findByIdOrThrow(UUID.fromString(fileId));
String fileKey = file.getFileKey();
s3Util.deleteFile(fileKey);
fileRepository.deleteById(UUID.fromString(fileId));
public void deleteFile(Long memberId, String fileOriginalName, String fileUploadDate, String fileUUID) {
Optional<File> file = fileRepository.findById(UUID.fromString(fileUUID));
if (file.isPresent()) {
fileRepository.delete(file.get());
} s3Util.deleteFile(createKey(memberId, fileUploadDate, fileUUID, fileOriginalName));
}

private String makeUploadPrefix(Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public ApiResponseDto<ScreenshotGetPreSignedResponseDto> createPresigned(Princip
@ApiResponse(responseCode = "400", description = "스크린샷 삭제 실패", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@DeleteMapping("/screenshots/{screenshotId}")
@DeleteMapping("/screenshots")
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto deleteScreenshot(@PathVariable String screenshotId) {
screenshotService.delete(screenshotId);
public ApiResponseDto delete(Principal principal, @RequestParam(value = "screenshotName") String screenshotName
, @RequestParam(value = "screenshotUploadDate") String screenshotUploadDate, @RequestParam(value = "screenshotUUID") String screenshotUUID) {
Long memberId = MemberUtil.getMemberId(principal);
screenshotService.deleteFile(memberId, screenshotName, screenshotUploadDate, screenshotUUID);
return success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ public interface ScreenshotService {

ScreenshotGetPreSignedResponseDto getPreSignedUrl(Long memberId, String screenshotName);

@Transactional
String createKey(Long memberId, ScreenshotCreateRequestDto requestDto);
String createKey(Long memberId, String screenshotDate, String screenshotUUID, String screenshotName);

void delete(String screenshotId);
void deleteFile(Long memberId, String screenshotName, String screenshotUploadDate, String screenshotUUID);
String findUrl(Long memberId, ScreenshotCreateRequestDto requestDto);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import site.katchup.katchupserver.common.util.S3Util;

import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;

@Service
Expand All @@ -40,23 +41,24 @@ public ScreenshotGetPreSignedResponseDto getPreSignedUrl(Long memberId, String s
@Override
@Transactional
public String findUrl(Long memberId, ScreenshotCreateRequestDto requestDto) {
return s3Util.findUrlByName(createKey(memberId, requestDto));
return s3Util.findUrlByName(createKey(memberId, requestDto.getScreenshotUploadDate(), requestDto.getScreenshotUUID().toString()
, requestDto.getScreenshotName()));
}

@Override
@Transactional
public String createKey(Long memberId, ScreenshotCreateRequestDto requestDto) {
public String createKey(Long memberId, String screenshotDate, String screenshotUUID, String screenshotName) {
String screenshotUploadPrefix = makeUploadPrefix(memberId);
return screenshotUploadPrefix + "/" + requestDto.getScreenshotUploadDate() + "/" + requestDto.getScreenshotUUID() + requestDto.getScreenshotName();
return screenshotUploadPrefix + "/" + screenshotDate + "/" + screenshotUUID + screenshotName;
}

@Override
@Transactional
public void delete(String screenshotId) {
Screenshot screenshot = screenshotRepository.findByIdOrThrow(UUID.fromString(screenshotId));
String screenshotKey = screenshot.getScreenshotKey();
s3Util.deleteFile(screenshotKey);
screenshotRepository.deleteById(UUID.fromString(screenshotId));
public void deleteFile(Long memberId, String screenshotName, String screenshotUploadDate, String screenshotUUID) {
Optional<Screenshot> screenshot = screenshotRepository.findById(UUID.fromString(screenshotUUID));
if (screenshot.isPresent()) {
screenshotRepository.delete(screenshot.get());
} s3Util.deleteFile(createKey(memberId, screenshotUploadDate, screenshotUUID, screenshotName));
}

private String makeUploadPrefix(Long memberId) {
Expand Down

0 comments on commit 788a1e3

Please sign in to comment.