From b52de37237af22e8f2a35b98291b1444e205efda Mon Sep 17 00:00:00 2001 From: kuj7373 Date: Mon, 2 Jun 2025 19:30:48 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[feat/#39]=20=EC=9B=94=EB=B3=84=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/8.13/fileHashes/fileHashes.bin | Bin 32097 -> 32097 bytes .gradle/8.13/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes .../history/api/HistoryRestController.java | 38 +++++++++++ .../history/application/HistoryService.java | 7 ++ .../application/HistoryServiceImpl.java | 63 ++++++++++++++++++ .../history/converter/HistoryConverter.java | 4 ++ .../domain/repository/HistoryRepository.java | 4 ++ .../domain/history/dto/HistoryRequestDTO.java | 4 ++ .../history/dto/HistoryResponseDTO.java | 30 +++++++++ .../history/exception/HistoryException.java | 7 ++ .../global/error/code/status/ErrorStatus.java | 5 +- .../error/code/status/SuccessStatus.java | 5 +- 14 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 src/main/java/study/goorm/domain/history/api/HistoryRestController.java create mode 100644 src/main/java/study/goorm/domain/history/application/HistoryService.java create mode 100644 src/main/java/study/goorm/domain/history/application/HistoryServiceImpl.java create mode 100644 src/main/java/study/goorm/domain/history/converter/HistoryConverter.java create mode 100644 src/main/java/study/goorm/domain/history/dto/HistoryRequestDTO.java create mode 100644 src/main/java/study/goorm/domain/history/dto/HistoryResponseDTO.java create mode 100644 src/main/java/study/goorm/domain/history/exception/HistoryException.java diff --git a/.gradle/8.13/executionHistory/executionHistory.lock b/.gradle/8.13/executionHistory/executionHistory.lock index 98bfadcf3e64090ffe83117591d8c5d8a888b7c4..b9b91a5fce3adc1b2340cebdb8a4e5594f1182f6 100644 GIT binary patch literal 17 UcmZQRtXo+5@vdep0|Z0@05xp{ivR!s literal 17 UcmZQRtXo+5@vdep0|bNu05xd@hX4Qo diff --git a/.gradle/8.13/fileHashes/fileHashes.bin b/.gradle/8.13/fileHashes/fileHashes.bin index c136885282891757af45dcefa97aa16191161bc0..24c7a4ad93253cdaab9a311e6e409ed1d764f36b 100644 GIT binary patch delta 401 zcmaF(i}B$v#tn@zEHb>ZNt0V+5+ID1F?tY&bSzjTb#gt7@i-PNl`>g0PLBtuE0qBZ zm=q^7W(!Zw@)6!#8W+b1(w#E-di*yC<6;6-6LTV%kv!QH#z==TX2KY!A&ivCe<6&7 z$@)oPS0qlJoCMaMF!?Bq@g2(8teL!y2V`0L}i0%qnn@ByOHutpPSNaq|8euE}VELIrs~d z@c2=*Jr2*pw0IDOmBqzDxD2b6&v)O`tMBLg{(Rqsl29lKI}0k4FH$0sYJl0VeoyeO zDug&g-C%%8`Qr%Y33k)9g4aPBUGG^S`4xxVoNTaEj@1QWq6VR!8ZGMg3rxQ2wc%l? zNTIY2GeDu-`VPcl!c9{O-U|LkG0tI@U$9ZR>3zf{$Ix7h8~z7n$CsJ_ diff --git a/.gradle/8.13/fileHashes/fileHashes.lock b/.gradle/8.13/fileHashes/fileHashes.lock index 354f994b9daf51dcfcac3d451b7bc93c422c3db9..b5c3f1df6cfb33ae101d2e5ab1ffeef6ae29fd81 100644 GIT binary patch literal 17 VcmZR6%*1`2-RO=50~j!`0RSh{1IGXW literal 17 VcmZR6%*1`2-RO=50~j#Z0stqZ1DF5+ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 123655ebbcdfcff20bf976210f6c6d57e61ce39e..e1e93d12640ea22c04927d5cd45686389acf7659 100644 GIT binary patch literal 17 UcmZSfny)7yTG{!G0Sp*L03|*HJpcdz literal 17 UcmZSfny)7yTG{!G0Sp+~03|R3F8}}l diff --git a/src/main/java/study/goorm/domain/history/api/HistoryRestController.java b/src/main/java/study/goorm/domain/history/api/HistoryRestController.java new file mode 100644 index 0000000..4a202ef --- /dev/null +++ b/src/main/java/study/goorm/domain/history/api/HistoryRestController.java @@ -0,0 +1,38 @@ +package study.goorm.domain.history.api; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +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 study.goorm.domain.history.application.HistoryService; +import study.goorm.domain.history.dto.HistoryResponseDTO; +import study.goorm.global.common.response.BaseResponse; +import study.goorm.global.error.code.status.SuccessStatus; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/histories") +@Validated +public class HistoryRestController { + + private final HistoryService historyService; + + @GetMapping("/monthly") + @Operation(summary = "월별 기록 조회 API", description = "Query Parameter로 clokeyId와 month(YYYY-MM)를 전달해주세요. clokeyId를 생략하면 본인 기록을 조회합니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "HISTORY_200", description = "기록이 성공적으로 조회되었습니다."), + }) + public BaseResponse getMonthlyHistories( + @RequestParam(required = false) Long clokeyId, + @RequestParam String month + ) { + HistoryResponseDTO.MonthlyHistoryViewResult result = historyService.getMonthlyHistories(clokeyId, month); + return BaseResponse.onSuccess(SuccessStatus.HISTORY_VIEW_SUCCESS, result); + } +} \ No newline at end of file diff --git a/src/main/java/study/goorm/domain/history/application/HistoryService.java b/src/main/java/study/goorm/domain/history/application/HistoryService.java new file mode 100644 index 0000000..1ddafbc --- /dev/null +++ b/src/main/java/study/goorm/domain/history/application/HistoryService.java @@ -0,0 +1,7 @@ +package study.goorm.domain.history.application; + +import study.goorm.domain.history.dto.HistoryResponseDTO; + +public interface HistoryService { + HistoryResponseDTO.MonthlyHistoryViewResult getMonthlyHistories(Long clokeyId, String month); +} diff --git a/src/main/java/study/goorm/domain/history/application/HistoryServiceImpl.java b/src/main/java/study/goorm/domain/history/application/HistoryServiceImpl.java new file mode 100644 index 0000000..80b617e --- /dev/null +++ b/src/main/java/study/goorm/domain/history/application/HistoryServiceImpl.java @@ -0,0 +1,63 @@ +package study.goorm.domain.history.application; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import study.goorm.domain.history.domain.entity.History; +import study.goorm.domain.history.domain.repository.HistoryRepository; +import study.goorm.domain.history.dto.HistoryResponseDTO; +import study.goorm.domain.history.exception.HistoryException; + +import java.time.YearMonth; +import java.time.format.DateTimeParseException; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class HistoryServiceImpl implements HistoryService { + + private final HistoryRepository historyRepository; + + + @Override + public HistoryResponseDTO.MonthlyHistoryViewResult getMonthlyHistories(Long clokeyId, String month) { + // 1. 날짜 포맷 검증 + YearMonth yearMonth; + try { + yearMonth = YearMonth.parse(month); + } catch (DateTimeParseException e) { + throw new HistoryException(HistoryErrorCode.INVALID_MONTH_FORMAT); + } + + // 2. clokeyId가 null이면 본인 ID 사용 + Long targetClokeyId = (clokeyId != null) ? clokeyId : clokeyService.getCurrentClokeyId(); + + // 3. 유효한 사용자 여부 확인 + ClokeyMember member = clokeyService.findById(targetClokeyId) + .orElseThrow(() -> new HistoryException(HistoryErrorCode.CLOKEY_NOT_FOUND)); + + // 4. 기록 조회 + List histories = historyRepository.findByClokeyIdAndMonth(targetClokeyId, yearMonth); + + // 5. 응답 변환 + List historyDTOs = histories.stream() + .map(history -> { + String imageUrl = history.getPhotos().isEmpty() + ? null + : Optional.ofNullable(history.getPhotos().get(0).getImageUrl()).orElse("비공개입니다"); + return new HistoryResponseDTO.HistoryInfo( + history.getId(), + history.getDate().toString(), + imageUrl + ); + }) + .collect(Collectors.toList()); + + // 6. 최종 결과 생성 + return new HistoryResponseDTO.MonthlyHistoryViewResult( + member.getId(), + member.getNickName(), + historyDTOs + ); +} diff --git a/src/main/java/study/goorm/domain/history/converter/HistoryConverter.java b/src/main/java/study/goorm/domain/history/converter/HistoryConverter.java new file mode 100644 index 0000000..8f901d0 --- /dev/null +++ b/src/main/java/study/goorm/domain/history/converter/HistoryConverter.java @@ -0,0 +1,4 @@ +package study.goorm.domain.history.converter; + +public class HistoryConverter { +} diff --git a/src/main/java/study/goorm/domain/history/domain/repository/HistoryRepository.java b/src/main/java/study/goorm/domain/history/domain/repository/HistoryRepository.java index 5f66987..3c82380 100644 --- a/src/main/java/study/goorm/domain/history/domain/repository/HistoryRepository.java +++ b/src/main/java/study/goorm/domain/history/domain/repository/HistoryRepository.java @@ -3,5 +3,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import study.goorm.domain.history.domain.entity.History; +import java.time.YearMonth; +import java.util.List; + public interface HistoryRepository extends JpaRepository { + List findByClokeyIdAndMonth(Long clokeyId, YearMonth month); } diff --git a/src/main/java/study/goorm/domain/history/dto/HistoryRequestDTO.java b/src/main/java/study/goorm/domain/history/dto/HistoryRequestDTO.java new file mode 100644 index 0000000..87594c7 --- /dev/null +++ b/src/main/java/study/goorm/domain/history/dto/HistoryRequestDTO.java @@ -0,0 +1,4 @@ +package study.goorm.domain.history.dto; + +public class HistoryRequestDTO { +} diff --git a/src/main/java/study/goorm/domain/history/dto/HistoryResponseDTO.java b/src/main/java/study/goorm/domain/history/dto/HistoryResponseDTO.java new file mode 100644 index 0000000..5e1fbcb --- /dev/null +++ b/src/main/java/study/goorm/domain/history/dto/HistoryResponseDTO.java @@ -0,0 +1,30 @@ +package study.goorm.domain.history.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; +import java.util.List; + +public class HistoryResponseDTO { + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class MonthlyHistoryViewResult { + private Long memberId; + private String nickName; + private List histories; + + @Getter + @AllArgsConstructor + public static class HistoryItem { + private Long historyId; + private LocalDate date; + private String imageUrl; + } + } +} diff --git a/src/main/java/study/goorm/domain/history/exception/HistoryException.java b/src/main/java/study/goorm/domain/history/exception/HistoryException.java new file mode 100644 index 0000000..2d02e80 --- /dev/null +++ b/src/main/java/study/goorm/domain/history/exception/HistoryException.java @@ -0,0 +1,7 @@ +package study.goorm.domain.history.exception; + +public class HistoryException extends RuntimeException { + public HistoryException(String message) { + super(message); + } +} diff --git a/src/main/java/study/goorm/global/error/code/status/ErrorStatus.java b/src/main/java/study/goorm/global/error/code/status/ErrorStatus.java index 694cecd..7eb078b 100644 --- a/src/main/java/study/goorm/global/error/code/status/ErrorStatus.java +++ b/src/main/java/study/goorm/global/error/code/status/ErrorStatus.java @@ -25,7 +25,10 @@ public enum ErrorStatus implements BaseErrorCode { // Page PAGE_UNDER_ONE(HttpStatus.BAD_REQUEST,"PAGE_4001","페이지는 1이상으로 입력해야 합니다."), - PAGE_SIZE_UNDER_ONE(HttpStatus.BAD_REQUEST,"PAGE_4002","페이지 사이즈는 1이상으로 입력해야 합니다.") + PAGE_SIZE_UNDER_ONE(HttpStatus.BAD_REQUEST,"PAGE_4002","페이지 사이즈는 1이상으로 입력해야 합니다."), + + //History + NO_SUCH_HISTORY(HttpStatus.BAD_REQUEST, "HISTORY_4001","기록이 존재하지 않습니다"), ; diff --git a/src/main/java/study/goorm/global/error/code/status/SuccessStatus.java b/src/main/java/study/goorm/global/error/code/status/SuccessStatus.java index 103f989..39f3088 100644 --- a/src/main/java/study/goorm/global/error/code/status/SuccessStatus.java +++ b/src/main/java/study/goorm/global/error/code/status/SuccessStatus.java @@ -14,7 +14,10 @@ public enum SuccessStatus implements BaseCode { //Cloth CLOTH_VIEW_SUCCESS(HttpStatus.OK,"CLOTH_200","옷이 성공적으로 조회되었습니다."), CLOTH_CREATED(HttpStatus.CREATED, "CLOTH_201"," 옷이 성공적으로 생성되었습니다."), - CLOTH_DELETED(HttpStatus.NO_CONTENT,"CLOTH_202","옷이 성공적으로 삭제되었습니다") + CLOTH_DELETED(HttpStatus.NO_CONTENT,"CLOTH_202","옷이 성공적으로 삭제되었습니다"), + + //History + HISTORY_VIEW_SUCCESS(HttpStatus.OK,"HISTORY_200","기록이 성공적으로 조회되었습니다."), ; From d98fa107fc0cc92403c5b831428cb066c39ea5b0 Mon Sep 17 00:00:00 2001 From: kuj7373 Date: Tue, 24 Jun 2025 19:58:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[feat/#40]=20=EC=9B=94=EB=B3=84=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/8.13/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes .../history/api/HistoryRestController.java | 9 ++- .../history/application/HistoryService.java | 5 +- .../application/HistoryServiceImpl.java | 70 ++++++++++-------- .../history/converter/HistoryConverter.java | 22 ++++++ .../domain/history/domain/entity/History.java | 5 ++ .../domain/repository/HistoryRepository.java | 12 ++- .../history/dto/HistoryResponseDTO.java | 16 ++-- .../history/exception/HistoryException.java | 9 ++- .../domain/exception/MemberException.java | 1 + .../global/error/code/status/ErrorStatus.java | 3 + 13 files changed, 105 insertions(+), 47 deletions(-) diff --git a/.gradle/8.13/executionHistory/executionHistory.lock b/.gradle/8.13/executionHistory/executionHistory.lock index b9b91a5fce3adc1b2340cebdb8a4e5594f1182f6..84c866aebbb05ddf0171b83692c7a8a63ff5237c 100644 GIT binary patch literal 17 UcmZQRtXo+5@vdep0|dkY05x(1kN^Mx literal 17 UcmZQRtXo+5@vdep0|Z0@05xp{ivR!s diff --git a/.gradle/8.13/fileHashes/fileHashes.lock b/.gradle/8.13/fileHashes/fileHashes.lock index b5c3f1df6cfb33ae101d2e5ab1ffeef6ae29fd81..56a5e43bfbe382b4bd1151bfd84ec048b1096aef 100644 GIT binary patch literal 17 VcmZR6%*1`2-RO=50~oL{0RSf}10DbX literal 17 VcmZR6%*1`2-RO=50~j!`0RSh{1IGXW diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index e1e93d12640ea22c04927d5cd45686389acf7659..92ca3d2d04fd33807cdaa46c7bdb9ad57745e2a8 100644 GIT binary patch literal 17 UcmZSfny)7yTG{!G0Sp)w0VPWVN&o-= literal 17 UcmZSfny)7yTG{!G0Sp*L03|*HJpcdz diff --git a/src/main/java/study/goorm/domain/history/api/HistoryRestController.java b/src/main/java/study/goorm/domain/history/api/HistoryRestController.java index 4a202ef..a4afa85 100644 --- a/src/main/java/study/goorm/domain/history/api/HistoryRestController.java +++ b/src/main/java/study/goorm/domain/history/api/HistoryRestController.java @@ -1,10 +1,9 @@ package study.goorm.domain.history.api; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -15,6 +14,8 @@ import study.goorm.global.common.response.BaseResponse; import study.goorm.global.error.code.status.SuccessStatus; +import java.time.YearMonth; + @RestController @RequiredArgsConstructor @RequestMapping("/histories") @@ -29,8 +30,8 @@ public class HistoryRestController { @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "HISTORY_200", description = "기록이 성공적으로 조회되었습니다."), }) public BaseResponse getMonthlyHistories( - @RequestParam(required = false) Long clokeyId, - @RequestParam String month + @RequestParam(value = "clokeyId", required = false) String clokeyId, + @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") YearMonth month ) { HistoryResponseDTO.MonthlyHistoryViewResult result = historyService.getMonthlyHistories(clokeyId, month); return BaseResponse.onSuccess(SuccessStatus.HISTORY_VIEW_SUCCESS, result); diff --git a/src/main/java/study/goorm/domain/history/application/HistoryService.java b/src/main/java/study/goorm/domain/history/application/HistoryService.java index 1ddafbc..b3ce6b9 100644 --- a/src/main/java/study/goorm/domain/history/application/HistoryService.java +++ b/src/main/java/study/goorm/domain/history/application/HistoryService.java @@ -2,6 +2,9 @@ import study.goorm.domain.history.dto.HistoryResponseDTO; +import java.time.LocalDate; +import java.time.YearMonth; + public interface HistoryService { - HistoryResponseDTO.MonthlyHistoryViewResult getMonthlyHistories(Long clokeyId, String month); + HistoryResponseDTO.MonthlyHistoryViewResult getMonthlyHistories(String clokeyId, YearMonth historyDate); } diff --git a/src/main/java/study/goorm/domain/history/application/HistoryServiceImpl.java b/src/main/java/study/goorm/domain/history/application/HistoryServiceImpl.java index 80b617e..1cc9b85 100644 --- a/src/main/java/study/goorm/domain/history/application/HistoryServiceImpl.java +++ b/src/main/java/study/goorm/domain/history/application/HistoryServiceImpl.java @@ -1,14 +1,18 @@ package study.goorm.domain.history.application; import lombok.RequiredArgsConstructor; +import org.apache.tomcat.util.net.openssl.ciphers.Authentication; import org.springframework.stereotype.Service; +import study.goorm.domain.history.converter.HistoryConverter; import study.goorm.domain.history.domain.entity.History; import study.goorm.domain.history.domain.repository.HistoryRepository; import study.goorm.domain.history.dto.HistoryResponseDTO; -import study.goorm.domain.history.exception.HistoryException; +import study.goorm.domain.member.domain.entity.Member; +import study.goorm.domain.member.domain.exception.MemberException; +import study.goorm.domain.member.domain.repository.MemberRepository; +import study.goorm.global.error.code.status.ErrorStatus; import java.time.YearMonth; -import java.time.format.DateTimeParseException; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -18,46 +22,50 @@ public class HistoryServiceImpl implements HistoryService { private final HistoryRepository historyRepository; + private final MemberRepository memberRepository; @Override - public HistoryResponseDTO.MonthlyHistoryViewResult getMonthlyHistories(Long clokeyId, String month) { - // 1. 날짜 포맷 검증 - YearMonth yearMonth; - try { - yearMonth = YearMonth.parse(month); - } catch (DateTimeParseException e) { - throw new HistoryException(HistoryErrorCode.INVALID_MONTH_FORMAT); - } + public HistoryResponseDTO.MonthlyHistoryViewResult getMonthlyHistories(String clokeyId, YearMonth month) { + YearMonth yearMonth = YearMonth.from(month); - // 2. clokeyId가 null이면 본인 ID 사용 - Long targetClokeyId = (clokeyId != null) ? clokeyId : clokeyService.getCurrentClokeyId(); + if (clokeyId == null) { +// clokeyId = SecurityUtil.getCurrentClokeyId(); + } - // 3. 유효한 사용자 여부 확인 - ClokeyMember member = clokeyService.findById(targetClokeyId) - .orElseThrow(() -> new HistoryException(HistoryErrorCode.CLOKEY_NOT_FOUND)); + Member member = memberRepository.findByClokeyId(clokeyId) + .orElseThrow(() -> new MemberException(ErrorStatus.NO_SUCH_MEMBER)); - // 4. 기록 조회 - List histories = historyRepository.findByClokeyIdAndMonth(targetClokeyId, yearMonth); + List histories = historyRepository.findByClokeyIdAndMonth( + clokeyId, yearMonth.getYear(), yearMonth.getMonthValue() + ); - // 5. 응답 변환 - List historyDTOs = histories.stream() + List historyDTOs = histories.stream() .map(history -> { - String imageUrl = history.getPhotos().isEmpty() + String imageUrl = (history.getPhotos() == null || history.getPhotos().isEmpty()) ? null : Optional.ofNullable(history.getPhotos().get(0).getImageUrl()).orElse("비공개입니다"); - return new HistoryResponseDTO.HistoryInfo( - history.getId(), - history.getDate().toString(), - imageUrl - ); + + return HistoryConverter.toHistoryItem(history, imageUrl); }) .collect(Collectors.toList()); - // 6. 최종 결과 생성 - return new HistoryResponseDTO.MonthlyHistoryViewResult( - member.getId(), - member.getNickName(), - historyDTOs - ); + return HistoryConverter.toMonthlyHistoryViewResult(member, historyDTOs); + } + + } + +//public class SecurityUtil { +// +// public static String getCurrentClokeyId() { +// Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); +// +//// if (authentication == null || !authentication.isAuthenticated()) { +//// throw new MemberException(ErrorStatus.UNAUTHORIZED); +//// } +// +// return authentication.getName(); // 또는 ((CustomUserDetails) authentication.getPrincipal()).getClokeyId(); +// } +//} + diff --git a/src/main/java/study/goorm/domain/history/converter/HistoryConverter.java b/src/main/java/study/goorm/domain/history/converter/HistoryConverter.java index 8f901d0..87e4e29 100644 --- a/src/main/java/study/goorm/domain/history/converter/HistoryConverter.java +++ b/src/main/java/study/goorm/domain/history/converter/HistoryConverter.java @@ -1,4 +1,26 @@ package study.goorm.domain.history.converter; +import study.goorm.domain.history.domain.entity.History; +import study.goorm.domain.history.dto.HistoryResponseDTO; +import study.goorm.domain.member.domain.entity.Member; + +import java.util.List; + public class HistoryConverter { + + public static HistoryResponseDTO.MonthlyHistoryViewResult toMonthlyHistoryViewResult(Member member, List historyItems) { + return HistoryResponseDTO.MonthlyHistoryViewResult.builder() + .memberId(member.getId()) + .nickName(member.getNickname()) + .histories(historyItems) + .build(); + } + + public static HistoryResponseDTO.HistoryItem toHistoryItem(History history, String imageUrl) { + return HistoryResponseDTO.HistoryItem.builder() + .historyId(history.getId()) + .date(history.getHistoryDate()) + .imageUrl(imageUrl) + .build(); + } } diff --git a/src/main/java/study/goorm/domain/history/domain/entity/History.java b/src/main/java/study/goorm/domain/history/domain/entity/History.java index 0180b18..26c6a1c 100644 --- a/src/main/java/study/goorm/domain/history/domain/entity/History.java +++ b/src/main/java/study/goorm/domain/history/domain/entity/History.java @@ -9,6 +9,8 @@ import study.goorm.domain.model.entity.BaseEntity; import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; @Entity @Getter @@ -37,5 +39,8 @@ public class History extends BaseEntity { @JoinColumn(name = "member_id", nullable = false) private Member member; + @OneToMany(mappedBy = "history", cascade = CascadeType.ALL, orphanRemoval = true) + private List photos = new ArrayList<>(); + } diff --git a/src/main/java/study/goorm/domain/history/domain/repository/HistoryRepository.java b/src/main/java/study/goorm/domain/history/domain/repository/HistoryRepository.java index 3c82380..d42635c 100644 --- a/src/main/java/study/goorm/domain/history/domain/repository/HistoryRepository.java +++ b/src/main/java/study/goorm/domain/history/domain/repository/HistoryRepository.java @@ -1,11 +1,21 @@ package study.goorm.domain.history.domain.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import study.goorm.domain.history.domain.entity.History; import java.time.YearMonth; import java.util.List; public interface HistoryRepository extends JpaRepository { - List findByClokeyIdAndMonth(Long clokeyId, YearMonth month); + + @Query("SELECT h FROM History h " + + "JOIN h.member m " + + "WHERE m.clokeyId = :clokeyId AND " + + "YEAR(h.historyDate) = :year AND MONTH(h.historyDate) = :month") + List findByClokeyIdAndMonth(@Param("clokeyId") String clokeyId, + @Param("year") int year, + @Param("month") int month); } + diff --git a/src/main/java/study/goorm/domain/history/dto/HistoryResponseDTO.java b/src/main/java/study/goorm/domain/history/dto/HistoryResponseDTO.java index 5e1fbcb..e64e770 100644 --- a/src/main/java/study/goorm/domain/history/dto/HistoryResponseDTO.java +++ b/src/main/java/study/goorm/domain/history/dto/HistoryResponseDTO.java @@ -6,6 +6,7 @@ import lombok.NoArgsConstructor; import java.time.LocalDate; +import java.time.YearMonth; import java.util.List; public class HistoryResponseDTO { @@ -18,13 +19,14 @@ public static class MonthlyHistoryViewResult { private Long memberId; private String nickName; private List histories; + } - @Getter - @AllArgsConstructor - public static class HistoryItem { - private Long historyId; - private LocalDate date; - private String imageUrl; - } + @Builder + @Getter + @AllArgsConstructor + public static class HistoryItem { + private Long historyId; + private LocalDate date; + private String imageUrl; } } diff --git a/src/main/java/study/goorm/domain/history/exception/HistoryException.java b/src/main/java/study/goorm/domain/history/exception/HistoryException.java index 2d02e80..b2b5304 100644 --- a/src/main/java/study/goorm/domain/history/exception/HistoryException.java +++ b/src/main/java/study/goorm/domain/history/exception/HistoryException.java @@ -1,7 +1,10 @@ package study.goorm.domain.history.exception; -public class HistoryException extends RuntimeException { - public HistoryException(String message) { - super(message); +import study.goorm.global.error.code.status.BaseErrorCode; +import study.goorm.global.exception.GeneralException; + +public class HistoryException extends GeneralException { + public HistoryException(BaseErrorCode code) { + super(code); } } diff --git a/src/main/java/study/goorm/domain/member/domain/exception/MemberException.java b/src/main/java/study/goorm/domain/member/domain/exception/MemberException.java index ed93df3..e783dfe 100644 --- a/src/main/java/study/goorm/domain/member/domain/exception/MemberException.java +++ b/src/main/java/study/goorm/domain/member/domain/exception/MemberException.java @@ -6,6 +6,7 @@ public class MemberException extends GeneralException { public MemberException(BaseErrorCode code) { + super(code); } } \ No newline at end of file diff --git a/src/main/java/study/goorm/global/error/code/status/ErrorStatus.java b/src/main/java/study/goorm/global/error/code/status/ErrorStatus.java index 7eb078b..181ae74 100644 --- a/src/main/java/study/goorm/global/error/code/status/ErrorStatus.java +++ b/src/main/java/study/goorm/global/error/code/status/ErrorStatus.java @@ -30,6 +30,9 @@ public enum ErrorStatus implements BaseErrorCode { //History NO_SUCH_HISTORY(HttpStatus.BAD_REQUEST, "HISTORY_4001","기록이 존재하지 않습니다"), + //Date + INVALID_MONTH_FORMAT(HttpStatus.BAD_REQUEST, "DATE_4001", "날짜 형식이 올바르지 않습니다"), + ; private final HttpStatus httpStatus;