Repository navigation
feat: 휴지통 목록 API (#54) #55
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
677adfa
feat: 휴지통 목록 조회와 커서 구현 (#54)
oTAETAEo 1cfc6f4
feat: 휴지통 목록 API 추가 (#54)
oTAETAEo dd392e5
refactor: 휴지통 보관 기간을 도메인 규칙으로 분리 (#54)
oTAETAEo d735c07
refactor: 휴지통 커서를 서비스 패키지로 이동 (#54)
oTAETAEo cd1225f
refactor: 휴지통 목록을 본문 없이 조회하도록 변경 (#54)
oTAETAEo 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
42 changes: 42 additions & 0 deletions
42
src/main/java/com/sniptext/domain/template/controller/TrashController.java
This file contains hidden or 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,42 @@ | ||
| package com.sniptext.domain.template.controller; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.sniptext.domain.template.controller.dto.TrashListResponse; | ||
| import com.sniptext.domain.template.service.TrashQueryService; | ||
| import com.sniptext.domain.template.service.dto.query.TrashListQuery; | ||
| import com.sniptext.global.response.ApiResponse; | ||
| import com.sniptext.global.security.LoginUser; | ||
|
|
||
| import jakarta.validation.constraints.Max; | ||
| import jakarta.validation.constraints.Min; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| /** | ||
| * 휴지통 (로그인 필요, 캐시 금지). | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/api/v1/trash/templates") | ||
| @RequiredArgsConstructor | ||
| public class TrashController { | ||
|
|
||
| private final TrashQueryService trashQueryService; | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<ApiResponse<TrashListResponse>> getTrash( | ||
| @LoginUser Long userId, | ||
| @RequestParam(required = false) String cursor, | ||
| @RequestParam(defaultValue = "20") | ||
| @Min(value = 1, message = "TRASH400_1") @Max(value = 50, message = "TRASH400_1") int size) { | ||
|
|
||
| TrashListResponse response = TrashListResponse.from( | ||
| trashQueryService.getTrash(new TrashListQuery(userId, cursor, size))); | ||
|
|
||
| return ApiResponse.success(TrashSuccessCode.TRASH_LIST_FOUND, response); | ||
| } | ||
|
|
||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/sniptext/domain/template/controller/TrashSuccessCode.java
This file contains hidden or 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,20 @@ | ||
| package com.sniptext.domain.template.controller; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import com.sniptext.global.response.ResultCode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum TrashSuccessCode implements ResultCode { | ||
|
|
||
| TRASH_LIST_FOUND(HttpStatus.OK, "TRASH200_1", "휴지통 조회에 성공했습니다."); | ||
|
|
||
| private final HttpStatus httpStatus; | ||
| private final String code; | ||
| private final String message; | ||
|
|
||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/sniptext/domain/template/controller/dto/TrashListResponse.java
This file contains hidden or 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,20 @@ | ||
| package com.sniptext.domain.template.controller.dto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import com.sniptext.domain.template.service.dto.result.TrashListResult; | ||
|
|
||
| /** 명세 15번 휴지통 목록 */ | ||
| public record TrashListResponse(long totalCount, List<TrashTemplateResponse> templates, boolean hasNext, | ||
| String nextCursor) { | ||
|
|
||
| public static TrashListResponse from(TrashListResult result) { | ||
| return new TrashListResponse( | ||
| result.totalCount(), | ||
| result.templates().stream().map(TrashTemplateResponse::from).toList(), | ||
| result.hasNext(), | ||
| result.nextCursor() | ||
| ); | ||
| } | ||
|
|
||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/sniptext/domain/template/controller/dto/TrashTemplateResponse.java
This file contains hidden or 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,26 @@ | ||
| package com.sniptext.domain.template.controller.dto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import com.sniptext.domain.template.service.dto.result.TrashTemplateResult; | ||
|
|
||
| /** 명세 15번 휴지통 항목. 시각은 전역 직렬화 설정이 +09:00을 붙여 내보낸다 */ | ||
| public record TrashTemplateResponse( | ||
| Long templateId, | ||
| String title, | ||
| String shortcut, | ||
| LocalDateTime deletedAt, | ||
| LocalDateTime purgeAt | ||
| ) { | ||
|
|
||
| public static TrashTemplateResponse from(TrashTemplateResult result) { | ||
| return new TrashTemplateResponse( | ||
| result.templateId(), | ||
| result.title(), | ||
| result.shortcut(), | ||
| result.deletedAt(), | ||
| result.purgeAt() | ||
| ); | ||
| } | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/sniptext/domain/template/exception/TrashErrorCode.java
This file contains hidden or 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,21 @@ | ||
| package com.sniptext.domain.template.exception; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import com.sniptext.global.response.ResultCode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum TrashErrorCode implements ResultCode { | ||
|
|
||
| /** 커서를 해석할 수 없거나 size가 1~50 밖 */ | ||
| INVALID_PAGE_REQUEST(HttpStatus.BAD_REQUEST, "TRASH400_1", "잘못된 페이지 요청이에요."); | ||
|
|
||
| private final HttpStatus httpStatus; | ||
| private final String code; | ||
| private final String message; | ||
|
|
||
| } |
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/sniptext/domain/template/repository/dto/TrashTemplateRow.java
This file contains hidden or 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,10 @@ | ||
| package com.sniptext.domain.template.repository.dto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| /** | ||
| * 휴지통 목록 한 행. 성능 때문이 아니라 본문을 읽지 않기 위한 조회 전용 객체다. | ||
| * 엔티티로 읽으면 내보내지도 않는 본문을 복호화하고, 복호화에 실패한 행 하나가 목록 전체를 500으로 만든다. | ||
| */ | ||
| public record TrashTemplateRow(Long id, String title, String shortcut, LocalDateTime deletedAt) { | ||
| } |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/sniptext/domain/template/service/TrashCursor.java
This file contains hidden or 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,45 @@ | ||
| package com.sniptext.domain.template.service; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.time.format.DateTimeParseException; | ||
| import java.util.Base64; | ||
| import java.util.Optional; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * 휴지통 목록의 다음 페이지 위치. 이 페이지 마지막 항목의 (삭제 시각, ID)를 이어 붙여 Base64 URL로 인코딩한다. | ||
| * 화면은 값을 해석하지 않고 nextCursor를 그대로 돌려보낸다. | ||
| * 같은 초 안에서도 순서가 갈리므로 시각은 DATETIME(6)과 같은 마이크로초까지 넣는다. | ||
| * 해석할 때 자릿수를 고정해 DB가 받지 못하는 연도(+12026)가 쿼리까지 가지 않게 한다. | ||
| */ | ||
| record TrashCursor(LocalDateTime deletedAt, Long id) { | ||
|
|
||
| private static final String SEPARATOR = "_"; | ||
| private static final DateTimeFormatter DELETED_AT_FORMAT = DateTimeFormatter.ofPattern( | ||
| "uuuu-MM-dd'T'HH:mm:ss.SSSSSS"); | ||
| private static final Pattern FORMAT = Pattern.compile( | ||
| "(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{6})" + SEPARATOR + "(\\d{1,19})"); | ||
|
|
||
| public String encode() { | ||
| String raw = DELETED_AT_FORMAT.format(deletedAt) + SEPARATOR + id; | ||
| return Base64.getUrlEncoder().withoutPadding().encodeToString(raw.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
|
|
||
| /** 서버가 만든 형식이 아니면 비어 있다. 어떤 에러로 응답할지는 서비스가 정한다 */ | ||
| public static Optional<TrashCursor> decode(String cursor) { | ||
| try { | ||
| String raw = new String(Base64.getUrlDecoder().decode(cursor), StandardCharsets.UTF_8); | ||
| Matcher matcher = FORMAT.matcher(raw); | ||
| if (!matcher.matches()) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(new TrashCursor(LocalDateTime.parse(matcher.group(1)), Long.valueOf(matcher.group(2)))); | ||
| } catch (IllegalArgumentException | DateTimeParseException exception) { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| } |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/sniptext/domain/template/service/TrashQueryService.java
This file contains hidden or 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,53 @@ | ||
| package com.sniptext.domain.template.service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.data.domain.Limit; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import com.sniptext.domain.template.exception.TrashErrorCode; | ||
| import com.sniptext.domain.template.repository.TemplateRepository; | ||
| import com.sniptext.domain.template.repository.dto.TrashTemplateRow; | ||
| import com.sniptext.domain.template.service.dto.query.TrashListQuery; | ||
| import com.sniptext.domain.template.service.dto.result.TrashListResult; | ||
| import com.sniptext.domain.template.service.dto.result.TrashTemplateResult; | ||
| import com.sniptext.global.exception.BusinessException; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class TrashQueryService { | ||
|
|
||
| private final TemplateRepository templateRepository; | ||
|
|
||
| /** 요청한 개수보다 1개 더 읽어 다음 페이지가 있는지 판단한다 */ | ||
| public TrashListResult getTrash(TrashListQuery query) { | ||
| List<TrashTemplateRow> rows = findPageWithOneMore(query); | ||
| boolean hasNext = rows.size() > query.size(); | ||
| List<TrashTemplateRow> page = hasNext ? rows.subList(0, query.size()) : rows; | ||
|
|
||
| return TrashListResult.of( | ||
| templateRepository.countByUserIdAndDeletedAtIsNotNull(query.userId()), | ||
| page.stream().map(TrashTemplateResult::from).toList(), | ||
| hasNext ? nextCursor(page.getLast()) : null | ||
| ); | ||
| } | ||
|
|
||
| private List<TrashTemplateRow> findPageWithOneMore(TrashListQuery query) { | ||
| Limit limit = Limit.of(query.size() + 1); | ||
| if (query.cursor() == null || query.cursor().isEmpty()) { | ||
| return templateRepository.findTrashFirstPage(query.userId(), limit); | ||
| } | ||
| TrashCursor cursor = TrashCursor.decode(query.cursor()) | ||
| .orElseThrow(() -> new BusinessException(TrashErrorCode.INVALID_PAGE_REQUEST)); | ||
| return templateRepository.findTrashAfter(query.userId(), cursor.deletedAt(), cursor.id(), limit); | ||
| } | ||
|
|
||
| private static String nextCursor(TrashTemplateRow last) { | ||
| return new TrashCursor(last.deletedAt(), last.id()).encode(); | ||
| } | ||
|
|
||
| } | ||
4 changes: 4 additions & 0 deletions
4
src/main/java/com/sniptext/domain/template/service/dto/query/TrashListQuery.java
This file contains hidden or 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,4 @@ | ||
| package com.sniptext.domain.template.service.dto.query; | ||
|
|
||
| public record TrashListQuery(Long userId, String cursor, int size) { | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sniptext/domain/template/service/dto/result/TrashListResult.java
This file contains hidden or 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,12 @@ | ||
| package com.sniptext.domain.template.service.dto.result; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record TrashListResult(long totalCount, List<TrashTemplateResult> templates, boolean hasNext, | ||
| String nextCursor) { | ||
|
|
||
| public static TrashListResult of(long totalCount, List<TrashTemplateResult> templates, String nextCursor) { | ||
| return new TrashListResult(totalCount, List.copyOf(templates), nextCursor != null, nextCursor); | ||
| } | ||
|
|
||
| } |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/sniptext/domain/template/service/dto/result/TrashTemplateResult.java
This file contains hidden or 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,27 @@ | ||
| package com.sniptext.domain.template.service.dto.result; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import com.sniptext.domain.template.repository.dto.TrashTemplateRow; | ||
| import com.sniptext.domain.template.support.TrashRetention; | ||
|
|
||
| /** 휴지통 항목. 본문은 화면에 보이지 않아 내보내지 않는다 */ | ||
| public record TrashTemplateResult( | ||
| Long templateId, | ||
| String title, | ||
| String shortcut, | ||
| LocalDateTime deletedAt, | ||
| LocalDateTime purgeAt | ||
| ) { | ||
|
|
||
| public static TrashTemplateResult from(TrashTemplateRow row) { | ||
| return new TrashTemplateResult( | ||
| row.id(), | ||
| row.title(), | ||
| row.shortcut(), | ||
| row.deletedAt(), | ||
| TrashRetention.purgeAt(row.deletedAt()) | ||
| ); | ||
| } | ||
|
|
||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/sniptext/domain/template/support/TrashRetention.java
This file contains hidden or 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,20 @@ | ||
| package com.sniptext.domain.template.support; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| /** | ||
| * 휴지통 보관 규칙. 삭제하고 30일이 지나면 자동 영구 삭제 대상이 된다. | ||
| * 목록의 예정 시각과 휴지통 정리 배치가 같은 기간을 쓰도록 여기 한 곳에 둔다. | ||
| */ | ||
| public final class TrashRetention { | ||
|
|
||
| private static final int RETENTION_DAYS = 30; | ||
|
|
||
| private TrashRetention() { | ||
| } | ||
|
|
||
| public static LocalDateTime purgeAt(LocalDateTime deletedAt) { | ||
| return deletedAt.plusDays(RETENTION_DAYS); | ||
| } | ||
|
|
||
| } |
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.
휴지통 목록은 본문을 내보내지 않는데 엔티티를 통째로 읽어서, 최대 51개 행의 본문을 매번 복호화하고 있습니다. 비용도 있지만, 복호화가 실패하는 행이 하나라도 생기면 본문을 보여주지도 않는 휴지통 목록 전체가
500이 될 거 같아요! 필요한 컬럼만 고르는 조회(repository/dto의 행 객체)로 바꾸는 걸 고려하면 어떨까 싶습니다!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.
아 맞네여 휴지통 목록은 본문을 안보여주니까, 말씀 주신 부분 맞습니다 감사합니당 😄
제안 해 주신
repository/dto로 가면 좋을것 같은 생각이여서 해당 부분 반영 하겠습니다 !