From 57ad92483ef052afbc63b4d8210f585f588847be Mon Sep 17 00:00:00 2001 From: gitjiho Date: Tue, 12 Nov 2024 00:08:54 +0900 Subject: [PATCH 1/3] refactor: change diary return with localDate --- .../dailyresponse/entity/DailyResponse.java | 8 ++++++++ .../diary/controller/DiaryController.java | 9 +++++---- .../gdg/kkia/diary/dto/DiaryReadResponse.java | 3 +++ .../gdg/kkia/diary/dto/DiaryWriteRequest.java | 3 +++ .../java/com/gdg/kkia/diary/entity/Diary.java | 20 ++++++++++++++++++- .../diary/repository/DiaryRepository.java | 3 ++- .../gdg/kkia/diary/service/DiaryService.java | 10 ++++++---- 7 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/gdg/kkia/dailyresponse/entity/DailyResponse.java b/src/main/java/com/gdg/kkia/dailyresponse/entity/DailyResponse.java index fb7a1e8..ca3c804 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/entity/DailyResponse.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/entity/DailyResponse.java @@ -9,6 +9,7 @@ import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; +import java.time.LocalDate; import java.time.LocalDateTime; @Entity @@ -27,6 +28,8 @@ public class DailyResponse { @CreatedDate private LocalDateTime responseDateTime; @NotNull + private LocalDate responseDate; + @NotNull private String response; @ManyToOne @JoinColumn(name = "member_id") @@ -45,6 +48,11 @@ public DailyResponse(String response, Member member, DailyQuestion dailyQuestion this.dailyQuestion = dailyQuestion; } + @PrePersist + public void convertToReceivedDate() { + this.responseDate = this.responseDateTime.toLocalDate(); + } + public boolean checkMember(Member member) { return this.member.equals(member); } diff --git a/src/main/java/com/gdg/kkia/diary/controller/DiaryController.java b/src/main/java/com/gdg/kkia/diary/controller/DiaryController.java index 48aa734..fcbebc9 100644 --- a/src/main/java/com/gdg/kkia/diary/controller/DiaryController.java +++ b/src/main/java/com/gdg/kkia/diary/controller/DiaryController.java @@ -11,6 +11,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.time.LocalDate; import java.util.List; @RestController @@ -28,10 +29,10 @@ public ResponseEntity writeDiary(@RequestAttribute("m return ResponseEntity.status(HttpStatus.CREATED).body(new StringTypeMessageResponse("일기가 작성되었습니다.")); } - @Operation(summary = "작성한 모든 일기 조회", description = "사용자가 작성했던 모든 일기를 조회합니다.") - @GetMapping() - public ResponseEntity> getAllDiary(@RequestAttribute("memberId") Long memberId) { - List diaryReadResponses = diaryService.getAllDiaryWrittenByMember(memberId); + @Operation(summary = "유저의 날짜별 작성 일기 조회", description = "localDate에 해당하는 날짜에 사용자가 작성했던 모든 일기를 조회합니다.") + @GetMapping("/{localDate}") + public ResponseEntity> getAllDiary(@RequestAttribute("memberId") Long memberId, @PathVariable("localDate") LocalDate localDate) { + List diaryReadResponses = diaryService.getAllDiaryWrittenByMemberInLocalDate(memberId, localDate); return ResponseEntity.ok().body(diaryReadResponses); } diff --git a/src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java b/src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java index 1d238d6..caf678a 100644 --- a/src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java +++ b/src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java @@ -1,10 +1,13 @@ package com.gdg.kkia.diary.dto; +import com.gdg.kkia.diary.entity.Diary; + import java.time.LocalDateTime; public record DiaryReadResponse( Long diaryId, LocalDateTime localDateTime, + Diary.Type type, String content ) { } diff --git a/src/main/java/com/gdg/kkia/diary/dto/DiaryWriteRequest.java b/src/main/java/com/gdg/kkia/diary/dto/DiaryWriteRequest.java index 84f9173..c084e82 100644 --- a/src/main/java/com/gdg/kkia/diary/dto/DiaryWriteRequest.java +++ b/src/main/java/com/gdg/kkia/diary/dto/DiaryWriteRequest.java @@ -1,6 +1,9 @@ package com.gdg.kkia.diary.dto; +import com.gdg.kkia.diary.entity.Diary; + public record DiaryWriteRequest( + Diary.Type type, String content ) { } diff --git a/src/main/java/com/gdg/kkia/diary/entity/Diary.java b/src/main/java/com/gdg/kkia/diary/entity/Diary.java index a51987c..100a5f4 100644 --- a/src/main/java/com/gdg/kkia/diary/entity/Diary.java +++ b/src/main/java/com/gdg/kkia/diary/entity/Diary.java @@ -10,6 +10,7 @@ import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; +import java.time.LocalDate; import java.time.LocalDateTime; @Entity @@ -29,17 +30,28 @@ public class Diary { @NotNull @CreatedDate private LocalDateTime writtenDatetime; + @NotNull + private LocalDate writtenDate; + @NotNull + @Enumerated(EnumType.STRING) + private Diary.Type type; @ManyToOne @JoinColumn(name = "member_id") @NotNull @OnDelete(action = OnDeleteAction.CASCADE) private Member member; - public Diary(String content, Member member) { + public Diary(Diary.Type type, String content, Member member) { + this.type = type; this.content = content; this.member = member; } + @PrePersist + public void convertToReceivedDate() { + this.writtenDate = this.writtenDatetime.toLocalDate(); + } + public boolean checkMemberIsNotCorrect(Member member) { return !this.member.equals(member); } @@ -50,4 +62,10 @@ public void updateDiary(String content) { } this.content = content; } + + public enum Type { + DAY, + EMOTION, + MEMO + } } diff --git a/src/main/java/com/gdg/kkia/diary/repository/DiaryRepository.java b/src/main/java/com/gdg/kkia/diary/repository/DiaryRepository.java index 83072fb..b21e1f9 100644 --- a/src/main/java/com/gdg/kkia/diary/repository/DiaryRepository.java +++ b/src/main/java/com/gdg/kkia/diary/repository/DiaryRepository.java @@ -3,8 +3,9 @@ import com.gdg.kkia.diary.entity.Diary; import org.springframework.data.jpa.repository.JpaRepository; +import java.time.LocalDate; import java.util.List; public interface DiaryRepository extends JpaRepository { - List findAllByMemberId(Long memberId); + List findAllByMemberIdAndWrittenDate(Long memberId, LocalDate localDate); } diff --git a/src/main/java/com/gdg/kkia/diary/service/DiaryService.java b/src/main/java/com/gdg/kkia/diary/service/DiaryService.java index 13dfba1..64f0c81 100644 --- a/src/main/java/com/gdg/kkia/diary/service/DiaryService.java +++ b/src/main/java/com/gdg/kkia/diary/service/DiaryService.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDate; import java.util.List; import java.util.stream.Collectors; @@ -27,17 +28,18 @@ public void writeDiary(Long memberId, DiaryWriteRequest diaryWriteRequest) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new NotFoundException("id에 해당하는 멤버가 없습니다.")); - Diary diary = new Diary(diaryWriteRequest.content(), member); + Diary diary = new Diary(diaryWriteRequest.type(), diaryWriteRequest.content(), member); diaryRepository.save(diary); } @Transactional(readOnly = true) - public List getAllDiaryWrittenByMember(Long memberId) { - return diaryRepository.findAllByMemberId(memberId) + public List getAllDiaryWrittenByMemberInLocalDate(Long memberId, LocalDate localDate) { + return diaryRepository.findAllByMemberIdAndWrittenDate(memberId, localDate) .stream() .map(Diary -> new DiaryReadResponse( Diary.getId(), Diary.getWrittenDatetime(), + Diary.getType(), Diary.getContent())) .collect(Collectors.toList()); } @@ -54,7 +56,7 @@ public DiaryReadResponse getOneDiaryWrittenByMember(Long memberId, Long diaryId) throw new UnauthorizedException("로그인한 사용자가 작성한 일기가 아닙니다."); } - return new DiaryReadResponse(diary.getId(), diary.getWrittenDatetime(), diary.getContent()); + return new DiaryReadResponse(diary.getId(), diary.getWrittenDatetime(), diary.getType(), diary.getContent()); } @Transactional From 5a0248e6eeb0296b6da06ad516443b0ec62aa99f Mon Sep 17 00:00:00 2001 From: gitjiho Date: Tue, 12 Nov 2024 00:11:52 +0900 Subject: [PATCH 2/3] refactor: change dailyResponse return with localDate --- .../controller/DailyQuestionResponseController.java | 7 ++++--- .../dailyresponse/repository/DailyResponseRepository.java | 3 ++- .../kkia/dailyresponse/service/DailyResponseService.java | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java b/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java index fb04d00..e94544b 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java @@ -14,6 +14,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.time.LocalDate; import java.util.List; @RestController @@ -75,9 +76,9 @@ public ResponseEntity getOneResponseOfDailyQuestion(@Requ } @Operation(summary = "본인이 모든 답변 조회", description = "본인이 작성한 모든 답변을 조회합니다.") - @GetMapping("/response/all") - public ResponseEntity> getAllResponseOfDailyQuestion(@RequestAttribute("memberId") Long memberId) { - List dailyResponseResponses = dailyResponseService.getAllResponseOfDailyQuestion(memberId); + @GetMapping("/response/{localDate}") + public ResponseEntity> getAllResponseOfDailyQuestion(@RequestAttribute("memberId") Long memberId, @PathVariable("localDate") LocalDate localDate) { + List dailyResponseResponses = dailyResponseService.getAllResponseOfDailyQuestionInLocalDate(memberId, localDate); return ResponseEntity.ok().body(dailyResponseResponses); } diff --git a/src/main/java/com/gdg/kkia/dailyresponse/repository/DailyResponseRepository.java b/src/main/java/com/gdg/kkia/dailyresponse/repository/DailyResponseRepository.java index 6c01d38..1fb40b6 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/repository/DailyResponseRepository.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/repository/DailyResponseRepository.java @@ -4,10 +4,11 @@ import com.gdg.kkia.member.entity.Member; import org.springframework.data.jpa.repository.JpaRepository; +import java.time.LocalDate; import java.util.List; public interface DailyResponseRepository extends JpaRepository { - List findAllByMember(Member member); + List findAllByMemberAndResponseDate(Member member, LocalDate localDate); } diff --git a/src/main/java/com/gdg/kkia/dailyresponse/service/DailyResponseService.java b/src/main/java/com/gdg/kkia/dailyresponse/service/DailyResponseService.java index 5b79606..941ae55 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/service/DailyResponseService.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/service/DailyResponseService.java @@ -14,6 +14,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.time.LocalDate; import java.util.List; import java.util.stream.Collectors; @@ -53,11 +54,11 @@ public DailyResponseResponse getOneResponseOfDailyQuestion(Long memberId, Long d return new DailyResponseResponse(dailyResponse.getId(), dailyResponse.getDailyQuestion().getQuestion(), dailyResponse.getResponse()); } - public List getAllResponseOfDailyQuestion(Long memberId) { + public List getAllResponseOfDailyQuestionInLocalDate(Long memberId, LocalDate localDate) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new NotFoundException("memberId에 해당하는 멤버가 없습니다.")); - return dailyResponseRepository.findAllByMember(member) + return dailyResponseRepository.findAllByMemberAndResponseDate(member, localDate) .stream() .map(DailyResponse -> new DailyResponseResponse( DailyResponse.getId(), From fbd97862c083db188910a175f1b0c04ef7e2a3b5 Mon Sep 17 00:00:00 2001 From: gitjiho Date: Tue, 12 Nov 2024 00:32:27 +0900 Subject: [PATCH 3/3] refactor: change memo and refactor --- .../controller/DailyQuestionResponseController.java | 4 ++-- .../com/gdg/kkia/diary/controller/DiaryController.java | 8 ++++---- .../java/com/gdg/kkia/diary/dto/DiaryReadResponse.java | 2 +- .../java/com/gdg/kkia/diary/service/DiaryService.java | 8 +++++--- .../com/gdg/kkia/member/controller/MemberController.java | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java b/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java index e94544b..355148b 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java @@ -75,8 +75,8 @@ public ResponseEntity getOneResponseOfDailyQuestion(@Requ return ResponseEntity.ok().body(dailyResponseResponse); } - @Operation(summary = "본인이 모든 답변 조회", description = "본인이 작성한 모든 답변을 조회합니다.") - @GetMapping("/response/{localDate}") + @Operation(summary = "날짜별 본인이 모든 답변 조회", description = "localDate에 본인이 작성한 모든 답변을 조회합니다.") + @GetMapping("/response/all/{localDate}") public ResponseEntity> getAllResponseOfDailyQuestion(@RequestAttribute("memberId") Long memberId, @PathVariable("localDate") LocalDate localDate) { List dailyResponseResponses = dailyResponseService.getAllResponseOfDailyQuestionInLocalDate(memberId, localDate); return ResponseEntity.ok().body(dailyResponseResponses); diff --git a/src/main/java/com/gdg/kkia/diary/controller/DiaryController.java b/src/main/java/com/gdg/kkia/diary/controller/DiaryController.java index fcbebc9..299bd24 100644 --- a/src/main/java/com/gdg/kkia/diary/controller/DiaryController.java +++ b/src/main/java/com/gdg/kkia/diary/controller/DiaryController.java @@ -22,15 +22,15 @@ public class DiaryController { private final DiaryService diaryService; - @Operation(summary = "일기 작성", description = "사용자가 일기를 작성합니다.") + @Operation(summary = "일기 작성", description = "사용자가 일기를 작성합니다. 리스트형태로 여러개를 넣을 수 있습니다.") @PostMapping - public ResponseEntity writeDiary(@RequestAttribute("memberId") Long memberId, @RequestBody DiaryWriteRequest diaryWriteRequest) { - diaryService.writeDiary(memberId, diaryWriteRequest); + public ResponseEntity writeDiary(@RequestAttribute("memberId") Long memberId, @RequestBody List diaryWriteRequests) { + diaryService.writeDiary(memberId, diaryWriteRequests); return ResponseEntity.status(HttpStatus.CREATED).body(new StringTypeMessageResponse("일기가 작성되었습니다.")); } @Operation(summary = "유저의 날짜별 작성 일기 조회", description = "localDate에 해당하는 날짜에 사용자가 작성했던 모든 일기를 조회합니다.") - @GetMapping("/{localDate}") + @GetMapping("/all/{localDate}") public ResponseEntity> getAllDiary(@RequestAttribute("memberId") Long memberId, @PathVariable("localDate") LocalDate localDate) { List diaryReadResponses = diaryService.getAllDiaryWrittenByMemberInLocalDate(memberId, localDate); return ResponseEntity.ok().body(diaryReadResponses); diff --git a/src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java b/src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java index caf678a..42a436c 100644 --- a/src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java +++ b/src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java @@ -6,7 +6,7 @@ public record DiaryReadResponse( Long diaryId, - LocalDateTime localDateTime, + LocalDateTime writtenDateTime, Diary.Type type, String content ) { diff --git a/src/main/java/com/gdg/kkia/diary/service/DiaryService.java b/src/main/java/com/gdg/kkia/diary/service/DiaryService.java index 64f0c81..b0fdf80 100644 --- a/src/main/java/com/gdg/kkia/diary/service/DiaryService.java +++ b/src/main/java/com/gdg/kkia/diary/service/DiaryService.java @@ -24,12 +24,14 @@ public class DiaryService { private final MemberRepository memberRepository; @Transactional - public void writeDiary(Long memberId, DiaryWriteRequest diaryWriteRequest) { + public void writeDiary(Long memberId, List diaryWriteRequests) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new NotFoundException("id에 해당하는 멤버가 없습니다.")); - Diary diary = new Diary(diaryWriteRequest.type(), diaryWriteRequest.content(), member); - diaryRepository.save(diary); + for (DiaryWriteRequest diaryWriteRequest : diaryWriteRequests) { + Diary diary = new Diary(diaryWriteRequest.type(), diaryWriteRequest.content(), member); + diaryRepository.save(diary); + } } @Transactional(readOnly = true) diff --git a/src/main/java/com/gdg/kkia/member/controller/MemberController.java b/src/main/java/com/gdg/kkia/member/controller/MemberController.java index dc4d3a0..9e5559b 100644 --- a/src/main/java/com/gdg/kkia/member/controller/MemberController.java +++ b/src/main/java/com/gdg/kkia/member/controller/MemberController.java @@ -14,7 +14,7 @@ @RestController @RequiredArgsConstructor @RequestMapping("/api") -@Tag(name = "인증", description = "인증 관련 API") +@Tag(name = "회원 관리", description = "회원 관리 관련 API") public class MemberController { private final MemberService memberService;