-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Feat/#36' of https://github.com/Team-Clody/Clody_server …
…into Feat/#36
- Loading branch information
Showing
6 changed files
with
123 additions
and
73 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/com/donkeys_today/server/application/diary/DiaryReplyUtil.java
This file contains 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,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<LocalDate, Reply> getRepliesByMonth(Long userId, int year, int month) { | ||
LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0); | ||
LocalDateTime end = start.plusMonths(1); | ||
|
||
List<Reply> replies = replyRepository.findByUserIdAndCreatedDateBetween(userId, start.toLocalDate(), | ||
end.toLocalDate()); | ||
|
||
Map<LocalDate, Reply> repliesByDate = replies.stream() | ||
.collect(Collectors.toMap(Reply::getCreatedDate, reply -> reply)); | ||
|
||
return repliesByDate; | ||
} | ||
|
||
public Map<LocalDate, List<Diary>> getDiariesByMonth(Long userId, int year, int month) { | ||
LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0); | ||
LocalDateTime end = start.plusMonths(1); | ||
|
||
List<Diary> diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end); | ||
|
||
Map<LocalDate, List<Diary>> diariesByDate = diaries.stream() | ||
.collect(Collectors.groupingBy(diary -> diary.getCreatedAt().toLocalDate())); | ||
return diariesByDate; | ||
} | ||
|
||
|
||
public List<DiaryContent> getDiaryByDate(Long userId, int year, int month, int day) { | ||
LocalDateTime start = LocalDateTime.of(year, month, day, 0, 0); | ||
LocalDateTime end = start.plusDays(1); | ||
List<DiaryContent> diaries = diaryRepository.findByUserIdAndCreatedAtBetween(userId, start, end).stream() | ||
.map(diary -> new DiaryContent(diary.getContent())) | ||
.collect(Collectors.toList()); | ||
return diaries; | ||
} | ||
} |
This file contains 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
12 changes: 2 additions & 10 deletions
12
src/main/java/com/donkeys_today/server/domain/diary/DiaryRepository.java
This file contains 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 |
---|---|---|
@@ -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<Diary, Long> { | ||
@Query("SELECT d " + | ||
"FROM Diary d WHERE d.user.id = :userId AND YEAR(d.createdAt) = :year AND MONTH(d.createdAt) = :month") | ||
List<Diary> 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<Diary> findContentsByUserIdAndYearAndMonthAndDay(@Param("userId") Long userId, @Param("year") int year, | ||
@Param("month") int month, @Param("day") int day); | ||
List<Diary> findByUserIdAndCreatedAtBetween(Long userId, LocalDateTime start, LocalDateTime end); | ||
} |
9 changes: 4 additions & 5 deletions
9
src/main/java/com/donkeys_today/server/domain/reply/ReplyRepository.java
This file contains 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 |
---|---|---|
@@ -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<Reply, Long> { | ||
@Query("SELECT r FROM Reply r WHERE r.user.id = :userId AND YEAR(r.createdDate) = :year AND MONTH(r.createdDate) = :month") | ||
List<Reply> findRepliesByUserIdAndYearAndMonth(@Param("userId") Long userId, @Param("year") int year, | ||
@Param("month") int month); | ||
|
||
List<Reply> findByUserIdAndCreatedDateBetween(Long userId, LocalDate start, LocalDate end); | ||
|
||
} |
This file contains 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
6 changes: 3 additions & 3 deletions
6
src/main/java/com/donkeys_today/server/presentation/api/DiaryController.java
This file contains 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