-
Notifications
You must be signed in to change notification settings - Fork 8
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
[BE] feat: Uploaded 상태로 오래되거나 Abandoned 상태의 UploadFile을 삭제하는 기능 추가 (#956) #957
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
관련 글 찾아보니
deleteAllInBatch()
도 같은 역할을 하던데, 해당 메서드를 구현해서 사용하면 예외가 발생하더라구요.아마 구현을
Repository
타입으로 해서 그런건가 싶기도 합니다.(
deleteAllInBatch()
는JpaRepository
인터페이스에 작성되어 있네요)