Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import com.sniptext.domain.template.controller.dto.TemplateCreateRequest;
import com.sniptext.domain.template.controller.dto.TemplateCreateResponse;
import com.sniptext.domain.template.controller.dto.TemplateDetailResponse;
import com.sniptext.domain.template.controller.dto.TemplateListResponse;
import com.sniptext.domain.template.controller.dto.TemplateUpdateRequest;
import com.sniptext.domain.template.controller.dto.TemplateUpdateResponse;
import com.sniptext.domain.template.service.TemplateCommandService;
import com.sniptext.domain.template.service.TemplateQueryService;
import com.sniptext.domain.template.service.dto.query.TemplateDetailQuery;
import com.sniptext.domain.template.service.dto.result.TemplateCreateResult;
import com.sniptext.domain.template.service.dto.result.TemplateDetailResult;
import com.sniptext.domain.template.service.dto.result.TemplateListResult;
import com.sniptext.domain.template.service.dto.result.TemplateUpdateResult;
import com.sniptext.global.response.ApiResponse;
import com.sniptext.global.security.LoginUser;
Expand All @@ -37,6 +39,13 @@ public class TemplateController {
private final TemplateCommandService templateCommandService;
private final TemplateQueryService templateQueryService;

@GetMapping
public ResponseEntity<ApiResponse<TemplateListResponse>> getTemplates(@LoginUser Long userId) {
TemplateListResult result = templateQueryService.getTemplates(userId);

return ApiResponse.success(TemplateSuccessCode.TEMPLATE_LIST_FOUND, TemplateListResponse.from(result));
}

@GetMapping("/{templateId}")
public ResponseEntity<ApiResponse<TemplateDetailResponse>> getTemplate(
@LoginUser Long userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@RequiredArgsConstructor
public enum TemplateSuccessCode implements ResultCode {

TEMPLATE_LIST_FOUND(HttpStatus.OK, "TEMPLATE200_1", "템플릿 목록 조회에 성공했습니다."),
TEMPLATE_FOUND(HttpStatus.OK, "TEMPLATE200_2", "템플릿 조회에 성공했습니다."),
TEMPLATE_UPDATED(HttpStatus.OK, "TEMPLATE200_3", "수정 내용이 저장되었습니다."),
TEMPLATE_CREATED(HttpStatus.CREATED, "TEMPLATE201_1", "템플릿이 저장되었습니다.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.sniptext.domain.template.controller.dto;

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

import com.sniptext.domain.template.service.dto.result.TemplateListItemResult;

/** 내 템플릿 목록의 한 항목 (명세 8번). 시각은 전역 직렬화 설정이 +09:00을 붙여 내보낸다. */
public record TemplateListItemResponse(
Long templateId,
String title,
String shortcut,
String content,
List<String> variables,
LocalDateTime lastUsedAt,
LocalDateTime createdAt,
LocalDateTime editedAt
) {

public static TemplateListItemResponse from(TemplateListItemResult result) {
return new TemplateListItemResponse(
result.templateId(),
result.title(),
result.shortcut(),
result.content(),
result.variables(),
result.lastUsedAt(),
result.createdAt(),
result.editedAt()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sniptext.domain.template.controller.dto;

import java.util.List;

import com.sniptext.domain.template.service.dto.result.TemplateListResult;

/** 명세 8번 내 템플릿 목록. 생성 순, 페이지 나눔 없음 */
public record TemplateListResponse(int totalCount, List<TemplateListItemResponse> templates) {

public static TemplateListResponse from(TemplateListResult result) {
return new TemplateListResponse(
result.totalCount(),
result.templates().stream().map(TemplateListItemResponse::from).toList()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public interface TemplateRepository extends JpaRepository<Template, Long> {
/** 본인 소유 조회. 휴지통 템플릿도 찾으므로 내 휴지통과 다른 회원 것을 호출자가 구분할 수 있다 */
Optional<Template> findByIdAndUserId(Long id, Long userId);

/**
* 휴지통을 제외한 본인 템플릿 전체, 생성 순(ID 오름차순). 목록은 페이지를 나누지 않는다.
* idx_templates_user_deleted (user_id, deleted_at)는 뒤에 기본 키가 붙어 있어 따로 정렬하지 않는다
*/
List<Template> findAllByUserIdAndDeletedAtIsNullOrderByIdAsc(Long userId);

/**
* 본인의 휴지통 템플릿을 비관적 쓰기 잠금으로 찾는다 (SELECT ... FOR UPDATE). 복구가 쓴다.
* 회원을 잠그지 않는 정리 배치·영구 삭제가 먼저 지우고 있으면 기다렸다가 빈 값을 받는다.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sniptext.domain.template.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -8,6 +10,8 @@
import com.sniptext.domain.template.repository.TemplateRepository;
import com.sniptext.domain.template.service.dto.query.TemplateDetailQuery;
import com.sniptext.domain.template.service.dto.result.TemplateDetailResult;
import com.sniptext.domain.template.service.dto.result.TemplateListItemResult;
import com.sniptext.domain.template.service.dto.result.TemplateListResult;
import com.sniptext.global.exception.BusinessException;

import lombok.RequiredArgsConstructor;
Expand All @@ -24,6 +28,15 @@ public long countActive(Long userId) {
return templateRepository.countByUserIdAndDeletedAtIsNull(userId);
}

/** 내 템플릿 목록. 휴지통을 제외한 전체를 생성 순으로 */
public TemplateListResult getTemplates(Long userId) {
List<TemplateListItemResult> templates = templateRepository.findAllByUserIdAndDeletedAtIsNullOrderByIdAsc(userId)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

본문 복호화는 엔티티를 읽을 때 변환기에서 일어나서, 복호화에 실패하는 행이 하나라도 있으면 목록 전체가 500이 될 것 같아요. 그러면 사용자는 대시보드를 아예 못 보고, 그 템플릿을 휴지통으로 보낼 수도 없게 돼요. 이 PR에서 고칠 필요는 없지만, 깨진 행을 어떻게 다룰지(건너뛰고 로그를 남길지, 지금처럼 500으로 둘지) 이슈로 남겨두면 좋을 거 같습니다!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다! 복호화 실패는 키가 다르거나 저장값이 변조된 경우에만 나고, 키 문제라면 행 하나가 아니라 전체가 깨져서 건너뛰어도 의미가 없을 것 같아요. 지금은 500으로 두고, 나중에 키 교체를 도입하면 그때 같이 다루겠습니다 !

.stream()
.map(TemplateListItemResult::from)
.toList();
return TemplateListResult.from(templates);
}

/** 내 템플릿 상세. 없거나 다른 회원 것이면 TEMPLATE404_1, 휴지통에 있으면 TEMPLATE404_2 */
public TemplateDetailResult getTemplate(TemplateDetailQuery query) {
return TemplateDetailResult.from(getActiveTemplate(query.userId(), query.templateId()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.sniptext.domain.template.service.dto.result;

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

import com.sniptext.domain.template.entity.Template;
import com.sniptext.domain.template.support.TemplateVariableExtractor;

/** 내 템플릿 목록의 한 항목. 변수 목록은 저장하지 않고 복호화한 본문에서 뽑는다. */
public record TemplateListItemResult(
Long templateId,
String title,
String shortcut,
String content,
List<String> variables,
LocalDateTime lastUsedAt,
LocalDateTime createdAt,
LocalDateTime editedAt
) {

public static TemplateListItemResult from(Template template) {
String content = template.getContent().getValue();
return new TemplateListItemResult(
template.getId(),
template.getTitle().getValue(),
template.getShortcut().getValue(),
content,
TemplateVariableExtractor.extract(content),
template.getLastUsedAt(),
template.getCreatedAt(),
template.getEditedAt()
);
}

}
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 TemplateListResult(int totalCount, List<TemplateListItemResult> templates) {

public static TemplateListResult from(List<TemplateListItemResult> templates) {
return new TemplateListResult(templates.size(), List.copyOf(templates));
}

}
Loading
Loading