Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into Feat/#36
  • Loading branch information
hyukjinKimm committed Jul 12, 2024
2 parents 53469b9 + 28a4caf commit 8c4071f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 73 deletions.
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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,106 +15,111 @@
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.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
@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());
List<Diary> diaries = diaryRepository.findContentsByUserIdAndYearAndMonth(userId, year, month);
List<Reply> replies = replyRepository.findRepliesByUserIdAndYearAndMonth(userId, year, month);

Map<LocalDate, Reply> repliesByDate = replies.stream()
.collect(Collectors.toMap(Reply::getCreatedDate, reply -> reply));
Map<LocalDate, List<Diary>> diariesByDate = diaries.stream()
.collect(Collectors.groupingBy(diary -> diary.getCreatedAt().toLocalDate()));
Map<LocalDate, List<Diary>> diariesByDate = diaryReplyUtil.getDiariesByMonth(getUserId(), year, month);
Map<LocalDate, Reply> repliesByDate = diaryReplyUtil.getRepliesByMonth(getUserId(), year, month);

AtomicInteger totalMonthlyCount = new AtomicInteger();

List<DiaryFullInfo> 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;
}
} 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));
return DiaryListResponse.of(totalMonthlyCount.get(), diaryData);
}

public DiaryCalenderResponse getDiaryCalender(int year, int month) {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
Long userId = Long.valueOf(authentication.getName());
List<Diary> diaries = diaryRepository.findContentsByUserIdAndYearAndMonth(userId, year, month);
List<Reply> replies = replyRepository.findRepliesByUserIdAndYearAndMonth(userId, year, month);

Map<LocalDate, Reply> repliesByDate = replies.stream()
.collect(Collectors.toMap(Reply::getCreatedDate, reply -> reply));
Map<LocalDate, List<Diary>> diariesByDate = diaries.stream()
.collect(Collectors.groupingBy(diary -> diary.getCreatedAt().toLocalDate()));
Map<LocalDate, List<Diary>> diariesByDate = diaryReplyUtil.getDiariesByMonth(getUserId(), year, month);
Map<LocalDate, Reply> repliesByDate = diaryReplyUtil.getRepliesByMonth(getUserId(), year, month);

int daysInMonth = LocalDate.of(year, month, 1).lengthOfMonth();
AtomicInteger totalMonthlyCount = new AtomicInteger();
List<DiarySimpleInfo> diaryData = new ArrayList<>();
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;
}
} 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<Diary> 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<LocalDate, Reply> repliesByDate) {
return repliesByDate.get(date);
}

private static boolean isReplyReady(LocalDate date, Map<LocalDate, Reply> repliesByDate) {
return repliesByDate.containsKey(date);
}

private static void setDiarySimpleInfo(int day, List<Diary> diaries, ReplyStatus replyStatus,
List<DiarySimpleInfo> diaryData) {

DiarySimpleInfo diarySimpleInfo = DiarySimpleInfo.of(diaries.size(), replyStatus);
diaryData.set(day - 1, diarySimpleInfo);
}

public DiaryResponse getDiary(int year, int month, int day) {

SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
Long userId = Long.valueOf(authentication.getName());
List<DiaryContent> diaries = diaryRepository.findContentsByUserIdAndYearAndMonthAndDay(
userId, year, month, day).stream().map(diary -> new DiaryContent(diary.getContent()))
.collect(Collectors.toList());
List<DiaryContent> diaries = diaryReplyUtil.getDiaryByDate(getUserId(), year, month, day);
return DiaryResponse.of(diaries);
}

public Long getUserId() {
return Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getName());
}
}
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);
}
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);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -46,5 +46,5 @@ public ResponseEntity<ApiResponse<DiaryResponse>> 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));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 8c4071f

Please sign in to comment.