Skip to content

Commit

Permalink
Merge pull request #17 from Ha-dam/feat/11-monthlyDiary
Browse files Browse the repository at this point in the history
feat: monthly diary inquiry API
  • Loading branch information
ziiyouth authored Dec 11, 2023
2 parents 6148cb1 + ad29e26 commit 6248bb0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/hadam/hadam/controller/DiaryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ public ResponseEntity<BaseResponse<?>> getMonthlyDiary(@RequestParam Long member
.body(BaseResponse.of(SuccessCode.OK, diaryService.getMonthlyDiary(memberId, year, month)));
}

@GetMapping("/monthly/new")
public ResponseEntity<BaseResponse<?>> getMonthlyAllDiaryNew(@RequestParam Long memberId, @RequestParam int year, @RequestParam int month){
return ResponseEntity.status(HttpStatus.OK)
.body(BaseResponse.of(SuccessCode.OK, diaryService.getMonthlyAllDiaryNew(memberId, year, month)));
}

@GetMapping("/monthly/old")
public ResponseEntity<BaseResponse<?>> getMonthlyAllDiaryOld(@RequestParam Long memberId, @RequestParam int year, @RequestParam int month){
return ResponseEntity.status(HttpStatus.OK)
.body(BaseResponse.of(SuccessCode.OK, diaryService.getMonthlyAllDiaryOld(memberId, year, month)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hadam.hadam.dto.response;

public record MonthlyListReq(
Long id,
String img,
String title,
String date
) {
}
51 changes: 51 additions & 0 deletions src/main/java/com/hadam/hadam/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.hadam.hadam.domain.DiaryInfo;
import com.hadam.hadam.domain.Member;
import com.hadam.hadam.dto.request.UpdateDiaryReq;
import com.hadam.hadam.dto.response.MonthlyListReq;
import com.hadam.hadam.dto.response.MonthlyRepresentRes;
import com.hadam.hadam.global.error.exception.EntityNotFoundException;
import com.hadam.hadam.global.error.exception.ErrorCode;
Expand All @@ -12,6 +13,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.stream.Collectors;
import java.util.Comparator;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -78,6 +81,54 @@ public static String formatYearMonth(int year, int month) {
return year + " " + month + "월";
}

@Transactional(readOnly = true)
public List<MonthlyListReq> getMonthlyAllDiaryNew(Long memberId, int year, int month) {
List<Diary> diaries = diaryRepository.findDiariesByMemberIdAndYearMonth(
memberId,
year,
month
);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d");

return diaries.stream()
.map(diary -> new MonthlyListReq(
diary.getId(),
diary.getImg(),
truncateContent(diary.getContent()), // truncateContent 메서드 사용
diary.getDate().format(formatter)
))
.sorted(Comparator.comparing(MonthlyListReq::date).reversed())
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<MonthlyListReq> getMonthlyAllDiaryOld(Long memberId, int year, int month) {
List<Diary> diaries = diaryRepository.findDiariesByMemberIdAndYearMonth(
memberId,
year,
month
);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d");

return diaries.stream()
.map(diary -> new MonthlyListReq(
diary.getId(),
diary.getImg(),
truncateContent(diary.getContent()), // truncateContent 메서드 사용
diary.getDate().format(formatter)
))
.sorted(Comparator.comparing(MonthlyListReq::date))
.collect(Collectors.toList());
}

// TODO : 제목 넣을지에 따라 수정
private String truncateContent(String content) {
int maxLength = 15;
if (content.length() > maxLength) {
return content.substring(0, maxLength) + "...";
}
return content;
}


}

0 comments on commit 6248bb0

Please sign in to comment.