From ff28ef1106c4bbbeaf2ea462b670bd4d34936294 Mon Sep 17 00:00:00 2001 From: hyuk jin Kim Date: Thu, 11 Jul 2024 14:12:21 +0900 Subject: [PATCH 1/6] =?UTF-8?q?Refactor:=20JPQL=20=EC=9D=84=20jpa=20query?= =?UTF-8?q?=20method=20=EB=A1=9C=20=EB=B3=80=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/diary/DiaryRepository.java | 12 ++---------- .../server/domain/reply/ReplyRepository.java | 9 ++++----- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/donkeys_today/server/domain/diary/DiaryRepository.java b/src/main/java/com/donkeys_today/server/domain/diary/DiaryRepository.java index 4a34a85..c672278 100644 --- a/src/main/java/com/donkeys_today/server/domain/diary/DiaryRepository.java +++ b/src/main/java/com/donkeys_today/server/domain/diary/DiaryRepository.java @@ -1,20 +1,12 @@ package com.donkeys_today.server.domain.diary; +import java.time.LocalDateTime; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface DiaryRepository extends JpaRepository { - @Query("SELECT d " + - "FROM Diary d WHERE d.user.id = :userId AND YEAR(d.createdAt) = :year AND MONTH(d.createdAt) = :month") - List findContentsByUserIdAndYearAndMonth(@Param("userId") Long userId, @Param("year") int year, - @Param("month") int month); - @Query("SELECT d " + - "FROM Diary d WHERE d.user.id = :userId AND YEAR(d.createdAt) = :year AND MONTH(d.createdAt) = :month AND DAY(d.createdAt) = :day") - List findContentsByUserIdAndYearAndMonthAndDay(@Param("userId") Long userId, @Param("year") int year, - @Param("month") int month, @Param("day") int day); + List findByUserIdAndCreatedAtBetween(Long userId, LocalDateTime start, LocalDateTime end); } diff --git a/src/main/java/com/donkeys_today/server/domain/reply/ReplyRepository.java b/src/main/java/com/donkeys_today/server/domain/reply/ReplyRepository.java index 4f81249..7975b50 100644 --- a/src/main/java/com/donkeys_today/server/domain/reply/ReplyRepository.java +++ b/src/main/java/com/donkeys_today/server/domain/reply/ReplyRepository.java @@ -1,14 +1,13 @@ package com.donkeys_today.server.domain.reply; -import io.lettuce.core.dynamic.annotation.Param; +import java.time.LocalDate; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @Repository public interface ReplyRepository extends JpaRepository { - @Query("SELECT r FROM Reply r WHERE r.user.id = :userId AND YEAR(r.createdDate) = :year AND MONTH(r.createdDate) = :month") - List findRepliesByUserIdAndYearAndMonth(@Param("userId") Long userId, @Param("year") int year, - @Param("month") int month); + + List findByUserIdAndCreatedDateBetween(Long userId, LocalDate start, LocalDate end); + } From 9001ab3403488592be0d665a61857b5887b4cb25 Mon Sep 17 00:00:00 2001 From: hyuk jin Kim Date: Thu, 11 Jul 2024 14:13:39 +0900 Subject: [PATCH 2/6] =?UTF-8?q?Refactor:=20jpa=20query=20method=20?= =?UTF-8?q?=EB=A1=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/diary/DiaryService.java | 46 +++++++++++-------- .../Diary/DiaryControllerImpl.java | 8 ++-- .../presentation/api/DiaryController.java | 6 +-- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java index f0f07f7..40aa2c3 100644 --- a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java +++ b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java @@ -12,6 +12,7 @@ import com.donkeys_today.server.presentation.Diary.dto.response.DiaryResponse; import com.donkeys_today.server.presentation.Diary.dto.response.DiarySimpleInfo; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -19,9 +20,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContext; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; @Service @@ -32,11 +30,15 @@ public class DiaryService { private final ReplyRepository replyRepository; public DiaryListResponse getDiaryList(int year, int month) { - SecurityContext context = SecurityContextHolder.getContext(); - Authentication authentication = context.getAuthentication(); - Long userId = Long.valueOf(authentication.getName()); - List diaries = diaryRepository.findContentsByUserIdAndYearAndMonth(userId, year, month); - List replies = replyRepository.findRepliesByUserIdAndYearAndMonth(userId, year, month); +// SecurityContext context = SecurityContextHolder.getContext(); +// Authentication authentication = context.getAuthentication(); +// Long userId = Long.valueOf(authentication.getName()); + Long userId = 2L; + LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0); + LocalDateTime end = start.plusMonths(1); + List diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end); + List replies = replyRepository.findByUserIdAndCreatedDateBetween(userId, start.toLocalDate(), + end.toLocalDate()); Map repliesByDate = replies.stream() .collect(Collectors.toMap(Reply::getCreatedDate, reply -> reply)); @@ -72,11 +74,15 @@ public DiaryListResponse getDiaryList(int year, int month) { } public DiaryCalenderResponse getDiaryCalender(int year, int month) { - SecurityContext context = SecurityContextHolder.getContext(); - Authentication authentication = context.getAuthentication(); - Long userId = Long.valueOf(authentication.getName()); - List diaries = diaryRepository.findContentsByUserIdAndYearAndMonth(userId, year, month); - List replies = replyRepository.findRepliesByUserIdAndYearAndMonth(userId, year, month); +// SecurityContext context = SecurityContextHolder.getContext(); +// Authentication authentication = context.getAuthentication(); +// Long userId = Long.valueOf(authentication.getName()); + Long userId = 2L; + LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0); + LocalDateTime end = start.plusMonths(1); + List diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end); + List replies = replyRepository.findByUserIdAndCreatedDateBetween(userId, start.toLocalDate(), + end.toLocalDate()); Map repliesByDate = replies.stream() .collect(Collectors.toMap(Reply::getCreatedDate, reply -> reply)); @@ -111,12 +117,16 @@ public DiaryCalenderResponse getDiaryCalender(int year, int month) { public DiaryResponse getDiary(int year, int month, int day) { - SecurityContext context = SecurityContextHolder.getContext(); - Authentication authentication = context.getAuthentication(); - Long userId = Long.valueOf(authentication.getName()); - List diaries = diaryRepository.findContentsByUserIdAndYearAndMonthAndDay( - userId, year, month, day).stream().map(diary -> new DiaryContent(diary.getContent())) +// SecurityContext context = SecurityContextHolder.getContext(); +// Authentication authentication = context.getAuthentication(); +// Long userId = Long.valueOf(authentication.getName()); + Long userId = 2L; + LocalDateTime start = LocalDateTime.of(year, month, day, 0, 0); + LocalDateTime end = start.plusDays(1); + List diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end).stream() + .map(diary -> new DiaryContent(diary.getContent())) .collect(Collectors.toList()); + return DiaryResponse.of(diaries); } } diff --git a/src/main/java/com/donkeys_today/server/presentation/Diary/DiaryControllerImpl.java b/src/main/java/com/donkeys_today/server/presentation/Diary/DiaryControllerImpl.java index ea44258..caf08f3 100644 --- a/src/main/java/com/donkeys_today/server/presentation/Diary/DiaryControllerImpl.java +++ b/src/main/java/com/donkeys_today/server/presentation/Diary/DiaryControllerImpl.java @@ -1,9 +1,9 @@ package com.donkeys_today.server.presentation.Diary; import com.donkeys_today.server.application.diary.DiaryService; -import com.donkeys_today.server.presentation.Diary.dto.DiaryCalenderResponse; -import com.donkeys_today.server.presentation.Diary.dto.DiaryListResponse; -import com.donkeys_today.server.presentation.Diary.dto.DiaryResponse; +import com.donkeys_today.server.presentation.Diary.dto.response.DiaryCalenderResponse; +import com.donkeys_today.server.presentation.Diary.dto.response.DiaryListResponse; +import com.donkeys_today.server.presentation.Diary.dto.response.DiaryResponse; import com.donkeys_today.server.presentation.api.DiaryController; import com.donkeys_today.server.support.dto.ApiResponse; import com.donkeys_today.server.support.dto.type.SuccessType; @@ -46,5 +46,5 @@ public ResponseEntity> getDiary(int year, int month, final DiaryResponse response = diaryService.getDiary(year, month, day); return ResponseEntity.status(HttpStatus.OK).body(ApiResponse.success(SuccessType.OK_SUCCESS, response)); } - + } \ No newline at end of file diff --git a/src/main/java/com/donkeys_today/server/presentation/api/DiaryController.java b/src/main/java/com/donkeys_today/server/presentation/api/DiaryController.java index a2ddd7f..da6bb7f 100644 --- a/src/main/java/com/donkeys_today/server/presentation/api/DiaryController.java +++ b/src/main/java/com/donkeys_today/server/presentation/api/DiaryController.java @@ -1,8 +1,8 @@ package com.donkeys_today.server.presentation.api; -import com.donkeys_today.server.presentation.Diary.dto.DiaryCalenderResponse; -import com.donkeys_today.server.presentation.Diary.dto.DiaryListResponse; -import com.donkeys_today.server.presentation.Diary.dto.DiaryResponse; +import com.donkeys_today.server.presentation.Diary.dto.response.DiaryCalenderResponse; +import com.donkeys_today.server.presentation.Diary.dto.response.DiaryListResponse; +import com.donkeys_today.server.presentation.Diary.dto.response.DiaryResponse; import com.donkeys_today.server.support.dto.ApiResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; From eff3b29298acaa2f198ce999d35a46b1516782bc Mon Sep 17 00:00:00 2001 From: hyuk jin Kim Date: Thu, 11 Jul 2024 14:44:33 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Refactor:=20DiaryReplyUtil=20=EC=A0=9C?= =?UTF-8?q?=EC=9E=91=EC=9C=BC=EB=A1=9C=20=EC=A4=91=EB=B3=B5=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/diary/DiaryReplyUtil.java | 56 +++++++++++++++++++ .../application/diary/DiaryService.java | 53 ++++-------------- 2 files changed, 68 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/donkeys_today/server/application/diary/DiaryReplyUtil.java diff --git a/src/main/java/com/donkeys_today/server/application/diary/DiaryReplyUtil.java b/src/main/java/com/donkeys_today/server/application/diary/DiaryReplyUtil.java new file mode 100644 index 0000000..7f39fea --- /dev/null +++ b/src/main/java/com/donkeys_today/server/application/diary/DiaryReplyUtil.java @@ -0,0 +1,56 @@ +package com.donkeys_today.server.application.diary; + +import com.donkeys_today.server.domain.diary.Diary; +import com.donkeys_today.server.domain.diary.DiaryRepository; +import com.donkeys_today.server.domain.reply.Reply; +import com.donkeys_today.server.domain.reply.ReplyRepository; +import com.donkeys_today.server.presentation.Diary.dto.response.DiaryContent; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; + +@Controller +@RequiredArgsConstructor +public class DiaryReplyUtil { + + private final DiaryRepository diaryRepository; + private final ReplyRepository replyRepository; + + public Map getRepliesByMonth(Long userId, int year, int month) { + LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0); + LocalDateTime end = start.plusMonths(1); + + List replies = replyRepository.findByUserIdAndCreatedDateBetween(userId, start.toLocalDate(), + end.toLocalDate()); + + Map repliesByDate = replies.stream() + .collect(Collectors.toMap(Reply::getCreatedDate, reply -> reply)); + + return repliesByDate; + } + + public Map> getDiariesByMonth(Long userId, int year, int month) { + LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0); + LocalDateTime end = start.plusMonths(1); + + List diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end); + + Map> diariesByDate = diaries.stream() + .collect(Collectors.groupingBy(diary -> diary.getCreatedAt().toLocalDate())); + return diariesByDate; + } + + + public List getDiaryByDate(Long userId, int year, int month, int day) { + LocalDateTime start = LocalDateTime.of(year, month, day, 0, 0); + LocalDateTime end = start.plusDays(1); + List diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end).stream() + .map(diary -> new DiaryContent(diary.getContent())) + .collect(Collectors.toList()); + return diaries; + } +} diff --git a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java index 40aa2c3..849cff3 100644 --- a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java +++ b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java @@ -1,10 +1,8 @@ package com.donkeys_today.server.application.diary; import com.donkeys_today.server.domain.diary.Diary; -import com.donkeys_today.server.domain.diary.DiaryRepository; import com.donkeys_today.server.domain.diary.ReplyStatus; import com.donkeys_today.server.domain.reply.Reply; -import com.donkeys_today.server.domain.reply.ReplyRepository; import com.donkeys_today.server.presentation.Diary.dto.response.DiaryCalenderResponse; import com.donkeys_today.server.presentation.Diary.dto.response.DiaryContent; import com.donkeys_today.server.presentation.Diary.dto.response.DiaryFullInfo; @@ -12,13 +10,11 @@ import com.donkeys_today.server.presentation.Diary.dto.response.DiaryResponse; import com.donkeys_today.server.presentation.Diary.dto.response.DiarySimpleInfo; import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -26,29 +22,15 @@ @RequiredArgsConstructor public class DiaryService { - private final DiaryRepository diaryRepository; - private final ReplyRepository replyRepository; + private final DiaryReplyUtil diaryReplyUtil; public DiaryListResponse getDiaryList(int year, int month) { -// SecurityContext context = SecurityContextHolder.getContext(); -// Authentication authentication = context.getAuthentication(); -// Long userId = Long.valueOf(authentication.getName()); - Long userId = 2L; - LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0); - LocalDateTime end = start.plusMonths(1); - List diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end); - List replies = replyRepository.findByUserIdAndCreatedDateBetween(userId, start.toLocalDate(), - end.toLocalDate()); - Map repliesByDate = replies.stream() - .collect(Collectors.toMap(Reply::getCreatedDate, reply -> reply)); - Map> diariesByDate = diaries.stream() - .collect(Collectors.groupingBy(diary -> diary.getCreatedAt().toLocalDate())); + Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(getUserId(), year, month); + Map repliesByDate = diaryReplyUtil.getRepliesByMonth(getUserId(), year, month); AtomicInteger totalMonthlyCount = new AtomicInteger(); - List diaryData = new ArrayList<>(); - diariesByDate.forEach((k, v) -> { ReplyStatus replyStatus; if (repliesByDate.containsKey(k)) { @@ -74,20 +56,9 @@ public DiaryListResponse getDiaryList(int year, int month) { } public DiaryCalenderResponse getDiaryCalender(int year, int month) { -// SecurityContext context = SecurityContextHolder.getContext(); -// Authentication authentication = context.getAuthentication(); -// Long userId = Long.valueOf(authentication.getName()); - Long userId = 2L; - LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0); - LocalDateTime end = start.plusMonths(1); - List diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end); - List replies = replyRepository.findByUserIdAndCreatedDateBetween(userId, start.toLocalDate(), - end.toLocalDate()); - Map repliesByDate = replies.stream() - .collect(Collectors.toMap(Reply::getCreatedDate, reply -> reply)); - Map> diariesByDate = diaries.stream() - .collect(Collectors.groupingBy(diary -> diary.getCreatedAt().toLocalDate())); + Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(getUserId(), year, month); + Map repliesByDate = diaryReplyUtil.getRepliesByMonth(getUserId(), year, month); int daysInMonth = LocalDate.of(year, month, 1).lengthOfMonth(); AtomicInteger totalMonthlyCount = new AtomicInteger(); @@ -117,16 +88,16 @@ public DiaryCalenderResponse getDiaryCalender(int year, int month) { public DiaryResponse getDiary(int year, int month, int day) { + List diaries = diaryReplyUtil.getDiaryByDate(getUserId(), year, month, day); + return DiaryResponse.of(diaries); + } + + private Long getUserId() { // SecurityContext context = SecurityContextHolder.getContext(); // Authentication authentication = context.getAuthentication(); // Long userId = Long.valueOf(authentication.getName()); - Long userId = 2L; - LocalDateTime start = LocalDateTime.of(year, month, day, 0, 0); - LocalDateTime end = start.plusDays(1); - List diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end).stream() - .map(diary -> new DiaryContent(diary.getContent())) - .collect(Collectors.toList()); +// return userId; + return 1L; - return DiaryResponse.of(diaries); } } From 0ff4ca9e9dfd2b10ff92b6f986f06134e3c8c3e9 Mon Sep 17 00:00:00 2001 From: hyuk jin Kim Date: Fri, 12 Jul 2024 04:47:45 +0900 Subject: [PATCH 4/6] =?UTF-8?q?Refactor:=20DiaryService=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=84=B0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/diary/DiaryService.java | 64 +++++++++++++------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java index 849cff3..fad8081 100644 --- a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java +++ b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java @@ -31,12 +31,12 @@ public DiaryListResponse getDiaryList(int year, int month) { AtomicInteger totalMonthlyCount = new AtomicInteger(); List diaryData = new ArrayList<>(); - diariesByDate.forEach((k, v) -> { + diariesByDate.forEach((date, dairies) -> { ReplyStatus replyStatus; - if (repliesByDate.containsKey(k)) { - Reply reply = repliesByDate.get(k); - if (reply.getIs_read()) { - totalMonthlyCount.addAndGet(1); + if (isReplyReady(date, repliesByDate)) { + Reply reply = getReply(date, repliesByDate); + if (isRead(reply)) { + plusCount(totalMonthlyCount); replyStatus = ReplyStatus.READY_READ; } else { // 준비됐으나 안읽음 replyStatus = ReplyStatus.READY_NOT_READ; @@ -44,11 +44,7 @@ public DiaryListResponse getDiaryList(int year, int month) { } else { replyStatus = ReplyStatus.UNREADY; } - - diaryData.add( - DiaryFullInfo.of(v.size(), replyStatus, k, - v.stream().map(diary -> new DiaryContent(diary.getContent())) - .toList())); + diaryData.add(getDiaryFullInfo(date, dairies, replyStatus)); }); diaryData.sort(Comparator.comparing(DiaryFullInfo::date)); @@ -66,12 +62,13 @@ public DiaryCalenderResponse getDiaryCalender(int year, int month) { for (int i = 0; i < daysInMonth; i++) { diaryData.add(DiarySimpleInfo.of(0, ReplyStatus.UNREADY)); // 빈 요소 추가 } - diariesByDate.forEach((k, v) -> { + + diariesByDate.forEach((date, diaries) -> { ReplyStatus replyStatus; - if (repliesByDate.containsKey(k)) { - Reply reply = repliesByDate.get(k); - if (reply.getIs_read()) { - totalMonthlyCount.addAndGet(1); + if (isReplyReady(date, repliesByDate)) { + Reply reply = getReply(date, repliesByDate); + if (isRead(reply)) { + plusCount(totalMonthlyCount); replyStatus = ReplyStatus.READY_READ; } else { // 준비됐으나 안읽음 replyStatus = ReplyStatus.READY_NOT_READ; @@ -79,13 +76,42 @@ public DiaryCalenderResponse getDiaryCalender(int year, int month) { } else { replyStatus = ReplyStatus.UNREADY; } - int dayOfMonth = k.getDayOfMonth(); - DiarySimpleInfo diarySimpleInfo = DiarySimpleInfo.of(v.size(), replyStatus); - diaryData.set(dayOfMonth - 1, diarySimpleInfo); + + int day = date.getDayOfMonth(); + setDiarySimpleInfo(day, diaries, replyStatus, diaryData); }); return DiaryCalenderResponse.of(totalMonthlyCount.get(), diaryData); } + private static DiaryFullInfo getDiaryFullInfo(LocalDate date, List dairies, ReplyStatus replyStatus) { + return DiaryFullInfo.of(dairies.size(), replyStatus, date, + dairies.stream().map(diary -> new DiaryContent(diary.getContent())) + .toList()); + } + + private static void plusCount(AtomicInteger totalMonthlyCount) { + totalMonthlyCount.addAndGet(1); + } + + private static Boolean isRead(Reply reply) { + return reply.getIs_read(); + } + + private static Reply getReply(LocalDate date, Map repliesByDate) { + return repliesByDate.get(date); + } + + private static boolean isReplyReady(LocalDate date, Map repliesByDate) { + return repliesByDate.containsKey(date); + } + + private static void setDiarySimpleInfo(int day, List diaries, ReplyStatus replyStatus, + List diaryData) { + + DiarySimpleInfo diarySimpleInfo = DiarySimpleInfo.of(diaries.size(), replyStatus); + diaryData.set(day - 1, diarySimpleInfo); + } + public DiaryResponse getDiary(int year, int month, int day) { List diaries = diaryReplyUtil.getDiaryByDate(getUserId(), year, month, day); @@ -97,7 +123,7 @@ private Long getUserId() { // Authentication authentication = context.getAuthentication(); // Long userId = Long.valueOf(authentication.getName()); // return userId; - return 1L; + return 2L; } } From 62c156637566f9feba4cc9e8a69d2066579096d4 Mon Sep 17 00:00:00 2001 From: hyuk jin Kim Date: Fri, 12 Jul 2024 16:07:48 +0900 Subject: [PATCH 5/6] =?UTF-8?q?Fix:=20UserId=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/diary/DiaryService.java | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java index fad8081..2842c5c 100644 --- a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java +++ b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java @@ -26,8 +26,8 @@ public class DiaryService { public DiaryListResponse getDiaryList(int year, int month) { - Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(getUserId(), year, month); - Map repliesByDate = diaryReplyUtil.getRepliesByMonth(getUserId(), year, month); + Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(userId, year, month); + Map repliesByDate = diaryReplyUtil.getRepliesByMonth(userId, year, month); AtomicInteger totalMonthlyCount = new AtomicInteger(); List diaryData = new ArrayList<>(); @@ -53,8 +53,8 @@ public DiaryListResponse getDiaryList(int year, int month) { public DiaryCalenderResponse getDiaryCalender(int year, int month) { - Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(getUserId(), year, month); - Map repliesByDate = diaryReplyUtil.getRepliesByMonth(getUserId(), year, month); + Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(userId, year, month); + Map repliesByDate = diaryReplyUtil.getRepliesByMonth(userId, year, month); int daysInMonth = LocalDate.of(year, month, 1).lengthOfMonth(); AtomicInteger totalMonthlyCount = new AtomicInteger(); @@ -114,16 +114,7 @@ private static void setDiarySimpleInfo(int day, List diaries, ReplyStatus public DiaryResponse getDiary(int year, int month, int day) { - List diaries = diaryReplyUtil.getDiaryByDate(getUserId(), year, month, day); + List diaries = diaryReplyUtil.getDiaryByDate(userId, year, month, day); return DiaryResponse.of(diaries); } - - private Long getUserId() { -// SecurityContext context = SecurityContextHolder.getContext(); -// Authentication authentication = context.getAuthentication(); -// Long userId = Long.valueOf(authentication.getName()); -// return userId; - return 2L; - - } } From 28a4cafe6c8833e3861aa98d2f8efcc0e516a6cc Mon Sep 17 00:00:00 2001 From: hyuk jin Kim Date: Fri, 12 Jul 2024 16:22:38 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=EC=9E=84=EC=8B=9C=20getUserId=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/application/diary/DiaryService.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java index 2842c5c..59dbc57 100644 --- a/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java +++ b/src/main/java/com/donkeys_today/server/application/diary/DiaryService.java @@ -16,6 +16,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; @Service @@ -26,8 +27,8 @@ public class DiaryService { public DiaryListResponse getDiaryList(int year, int month) { - Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(userId, year, month); - Map repliesByDate = diaryReplyUtil.getRepliesByMonth(userId, year, month); + Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(getUserId(), year, month); + Map repliesByDate = diaryReplyUtil.getRepliesByMonth(getUserId(), year, month); AtomicInteger totalMonthlyCount = new AtomicInteger(); List diaryData = new ArrayList<>(); @@ -53,8 +54,8 @@ public DiaryListResponse getDiaryList(int year, int month) { public DiaryCalenderResponse getDiaryCalender(int year, int month) { - Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(userId, year, month); - Map repliesByDate = diaryReplyUtil.getRepliesByMonth(userId, year, month); + Map> diariesByDate = diaryReplyUtil.getDiariesByMonth(getUserId(), year, month); + Map repliesByDate = diaryReplyUtil.getRepliesByMonth(getUserId(), year, month); int daysInMonth = LocalDate.of(year, month, 1).lengthOfMonth(); AtomicInteger totalMonthlyCount = new AtomicInteger(); @@ -114,7 +115,11 @@ private static void setDiarySimpleInfo(int day, List diaries, ReplyStatus public DiaryResponse getDiary(int year, int month, int day) { - List diaries = diaryReplyUtil.getDiaryByDate(userId, year, month, day); + List diaries = diaryReplyUtil.getDiaryByDate(getUserId(), year, month, day); return DiaryResponse.of(diaries); } + + public Long getUserId() { + return Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getName()); + } }