-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE] feat: Uploaded 상태로 오래되거나 Abandoned 상태의 UploadFile을 삭제하는 기능 추가 (#956
) (#957) * feat: UploadFile 삭제 기능 추가 * chore: 로그 띄어쓰기 삭제 * feat: 업로드 파일 삭제 어드민 API 추가 * refactor: AdminUploadV1Controller -> AdminUploadImageV1Controller 클래스명 변경 - 더 명확한 의미를 가지도록 변경
- Loading branch information
1 parent
20aef31
commit 13f7318
Showing
12 changed files
with
447 additions
and
7 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...main/java/com/festago/admin/dto/upload/AdminDeleteAbandonedPeriodUploadFileV1Request.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.festago.admin.dto.upload; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import java.time.LocalDateTime; | ||
|
||
public record AdminDeleteAbandonedPeriodUploadFileV1Request( | ||
@NotNull LocalDateTime startTime, | ||
@NotNull LocalDateTime endTime | ||
) { | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...nd/src/main/java/com/festago/admin/presentation/v1/AdminUploadFileDeleteV1Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.festago.admin.presentation.v1; | ||
|
||
import com.festago.admin.dto.upload.AdminDeleteAbandonedPeriodUploadFileV1Request; | ||
import com.festago.upload.application.UploadFileDeleteService; | ||
import io.swagger.v3.oas.annotations.Hidden; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/admin/api/v1/upload/delete") | ||
@RequiredArgsConstructor | ||
@Hidden | ||
public class AdminUploadFileDeleteV1Controller { | ||
|
||
private final UploadFileDeleteService uploadFileDeleteService; | ||
|
||
@DeleteMapping("/abandoned-period") | ||
public ResponseEntity<Void> deleteAbandonedWithPeriod( | ||
@RequestBody @Valid AdminDeleteAbandonedPeriodUploadFileV1Request request | ||
) { | ||
uploadFileDeleteService.deleteAbandonedStatusWithPeriod(request.startTime(), request.endTime()); | ||
return ResponseEntity.ok() | ||
.build(); | ||
} | ||
|
||
@DeleteMapping("/old-uploaded") | ||
public ResponseEntity<Void> deleteOldUploaded() { | ||
uploadFileDeleteService.deleteOldUploadedStatus(); | ||
return ResponseEntity.ok() | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/com/festago/upload/application/UploadFileDeleteService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.festago.upload.application; | ||
|
||
import com.festago.upload.domain.StorageClient; | ||
import com.festago.upload.domain.UploadFile; | ||
import com.festago.upload.domain.UploadStatus; | ||
import com.festago.upload.repository.UploadFileRepository; | ||
import java.time.Clock; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class UploadFileDeleteService { | ||
|
||
private final StorageClient storageClient; | ||
private final UploadFileRepository uploadFileRepository; | ||
private final Clock clock; | ||
|
||
public void deleteAbandonedStatusWithPeriod(LocalDateTime startTime, LocalDateTime endTime) { | ||
List<UploadFile> uploadFiles = uploadFileRepository.findByCreatedAtBetweenAndStatus(startTime, endTime, UploadStatus.ABANDONED); | ||
deleteUploadFiles(uploadFiles); | ||
} | ||
|
||
private void deleteUploadFiles(List<UploadFile> uploadFiles) { | ||
storageClient.delete(uploadFiles); | ||
uploadFileRepository.deleteByIn(uploadFiles); | ||
} | ||
|
||
public void deleteOldUploadedStatus() { | ||
LocalDateTime yesterday = LocalDateTime.now(clock).minusDays(1); | ||
List<UploadFile> uploadFiles = uploadFileRepository.findByCreatedAtBeforeAndStatus(yesterday, UploadStatus.UPLOADED); | ||
deleteUploadFiles(uploadFiles); | ||
} | ||
} |
13 changes: 11 additions & 2 deletions
13
backend/src/main/java/com/festago/upload/domain/StorageClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
package com.festago.upload.domain; | ||
|
||
import java.util.List; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public interface StorageClient { | ||
|
||
/** | ||
* MultipartFile을 보관(영속)하는 클래스 <br/> 업로드 작업이 끝나면, 업로드한 파일의 정보를 가진 UploadStatus.UPLOADED 상태의 UploadFile를 반환해야 한다. | ||
* <br/> 반환된 UploadFile을 영속하는 책임은 해당 클래스를 사용하는 클라이언트가 구현해야 한다. <br/> | ||
* MultipartFile을 보관(영속)하는 메서드 <br/> 업로드 작업이 끝나면, 업로드한 파일의 정보를 가진 UploadStatus.UPLOADED 상태의 UploadFile를 반환해야 한다. | ||
* <br/> 반환된 UploadFile을 영속하는 책임은 해당 메서드를 사용하는 클라이언트가 구현해야 한다. <br/> | ||
* | ||
* @param file 업로드 할 MultipartFile | ||
* @return UploadStatus.PENDING 상태의 영속되지 않은 UploadFile 엔티티 | ||
*/ | ||
UploadFile storage(MultipartFile file); | ||
|
||
/** | ||
* 업로드 파일을 삭제하는 메서드 <br/> 삭제 작업이 끝나면, UploadFile이 가진 정보에 대한 업로드 된 파일이 없으므로, 인자로 들어온 UploadFiles를 삭제해야 한다. <br/> 삭제가 | ||
* 끝나고 UploadFile을 삭제하는 책임은 해당 메서드를 사용하는 클라이언트가 구현해야 한다. <br/> | ||
* | ||
* @param uploadFiles 삭제하려는 업로드 된 파일의 정보가 담긴 UploadFile 목록 | ||
*/ | ||
void delete(List<UploadFile> uploadFiles); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...rc/test/java/com/festago/admin/presentation/v1/AdminUploadFileDeleteV1ControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.festago.admin.presentation.v1; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.festago.admin.dto.upload.AdminDeleteAbandonedPeriodUploadFileV1Request; | ||
import com.festago.auth.domain.Role; | ||
import com.festago.support.CustomWebMvcTest; | ||
import com.festago.support.WithMockAuth; | ||
import jakarta.servlet.http.Cookie; | ||
import java.time.LocalDateTime; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
@CustomWebMvcTest | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
@SuppressWarnings("NonAsciiCharacters") | ||
class AdminUploadFileDeleteV1ControllerTest { | ||
|
||
private static final Cookie TOKEN_COOKIE = new Cookie("token", "token"); | ||
|
||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
@Nested | ||
class ABANDONED_상태와_기간에_포함되는_파일_삭제 { | ||
|
||
final String uri = "/admin/api/v1/upload/delete/abandoned-period"; | ||
|
||
@Nested | ||
@DisplayName("DELETE " + uri) | ||
class 올바른_주소로 { | ||
|
||
@Test | ||
@WithMockAuth(role = Role.ADMIN) | ||
void 요청을_보내면_200_응답이_반환된다() throws Exception { | ||
// given | ||
LocalDateTime now = LocalDateTime.now(); | ||
var request = new AdminDeleteAbandonedPeriodUploadFileV1Request(now, now); | ||
|
||
// when & then | ||
mockMvc.perform(delete(uri) | ||
.content(objectMapper.writeValueAsString(request)) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.cookie(TOKEN_COOKIE)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
void 토큰_없이_보내면_401_응답이_반환된다() throws Exception { | ||
// when & then | ||
mockMvc.perform(delete(uri)) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
@WithMockAuth(role = Role.MEMBER) | ||
void 토큰의_권한이_Admin이_아니면_404_응답이_반환된다() throws Exception { | ||
// when & then | ||
mockMvc.perform(delete(uri) | ||
.cookie(TOKEN_COOKIE)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
} | ||
} | ||
|
||
@Nested | ||
class 오래된_UPLOADED_상태_파일_삭제 { | ||
|
||
final String uri = "/admin/api/v1/upload/delete/old-uploaded"; | ||
|
||
@Nested | ||
@DisplayName("DELETE " + uri) | ||
class 올바른_주소로 { | ||
|
||
@Test | ||
@WithMockAuth(role = Role.ADMIN) | ||
void 요청을_보내면_200_응답이_반환된다() throws Exception { | ||
// given | ||
// when & then | ||
mockMvc.perform(delete(uri) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.cookie(TOKEN_COOKIE)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
void 토큰_없이_보내면_401_응답이_반환된다() throws Exception { | ||
// when & then | ||
mockMvc.perform(delete(uri)) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
@WithMockAuth(role = Role.MEMBER) | ||
void 토큰의_권한이_Admin이_아니면_404_응답이_반환된다() throws Exception { | ||
// when & then | ||
mockMvc.perform(delete(uri) | ||
.cookie(TOKEN_COOKIE)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.