Skip to content

Commit

Permalink
Merge pull request #42 from UMC-HKT-Team-R/feat/38
Browse files Browse the repository at this point in the history
[Feat] 캘린더 월별, 일별 조회 api 구현
  • Loading branch information
Ssamssamukja authored Jan 11, 2025
2 parents b078ec4 + 2768b04 commit 1b0fa99
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum ErrorStatus implements BaseErrorCode {


//calendar
CALENDAR_NOT_FOUND(HttpStatus.NOT_FOUND,"CALENDAR_404", " 해당 기록을 찾으 수 없습니다.");
CALENDAR_NOT_FOUND(HttpStatus.NOT_FOUND,"CALENDAR_404", " 해당 기록을 찾을 수 없습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.Midnight.Snacker.apiPayload.exception;

import com.example.Midnight.Snacker.apiPayload.code.BaseErrorCode;

public class CalendarException extends GeneralException {

public CalendarException(BaseErrorCode code) {
super(code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;


public interface CalendarRepository extends JpaRepository<Calendar, Long> {
List<Calendar> findAllByDateBetweenOrderByDateAsc(LocalDateTime startDate, LocalDateTime endDate);
List<Calendar> findAllByMemberAndDateBetweenOrderByDateAsc(Member member, LocalDateTime startDate, LocalDateTime endDate);

List<Calendar> findAllByMember(Member member);

Expand All @@ -24,4 +25,8 @@ public interface CalendarRepository extends JpaRepository<Calendar, Long> {

// 특정 멤버와 날짜 범위로 필터링하여 전체 개수 가져오기
int countByMemberAndDateBetween(Member member, LocalDateTime startDate, LocalDateTime endDate);

List<Calendar> findByDateBetweenAndMember(LocalDateTime startDate, LocalDateTime endDate, Member member);

Optional<Calendar> findByDateAndMember(LocalDateTime date, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public interface CalendarService {

//Long addRecord(Member member, Category category, Color color, MultipartFile image, RegisterRequestDTO request);
Long addRecord(Member member, CategoryE categoryE, Color color, MultipartFile image, LocalDateTime date, String detailFood);
CalendarResponseDTO.CalendarResponseMonthlyListDTO getMonthlyRecords(int year, int month, Member member);

List<CalendarInfoDTO> getCalendarInfo(LocalDate localDate);
CalendarResponseDTO getRecord(LocalDate date, Member member);
List<CalendarResponseDTO.CalendarResponseDailyDTO> getDailyRecord(LocalDate date, Member member);
void deleteRecord(Member member, Long calendarId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.example.Midnight.Snacker.service.CalendarService;

import com.example.Midnight.Snacker.apiPayload.code.status.ErrorStatus;
import com.example.Midnight.Snacker.apiPayload.exception.CalendarException;
import com.example.Midnight.Snacker.apiPayload.exception.CustomException;
import com.example.Midnight.Snacker.converter.CalenderConverter;
import com.example.Midnight.Snacker.domain.Calendar;
import com.example.Midnight.Snacker.domain.Member;
import com.example.Midnight.Snacker.domain.Post;
import com.example.Midnight.Snacker.domain.enums.CategoryE;
import com.example.Midnight.Snacker.domain.enums.Color;
import com.example.Midnight.Snacker.repository.CalendarRepository;
Expand All @@ -19,7 +21,9 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -58,43 +62,37 @@ public Long addRecord(Member member, CategoryE categoryE, Color color, Multipart
}

@Override
@Transactional
public List<CalendarInfoDTO> getCalendarInfo(LocalDate localDate){
//LocalDate를 LocalDateTime의 범위로 변환
LocalDateTime startDateTime = localDate.atTime(0,0,0);
LocalDateTime endDateTime = localDate.atTime(23, 59, 59);
public CalendarResponseDTO.CalendarResponseMonthlyListDTO getMonthlyRecords(int year, int month, Member member) {
LocalDate startOfMonth = YearMonth.of(year, month).atDay(1);
LocalDate endOfMonth = YearMonth.of(year, month).atEndOfMonth();

//repository로 찾기
List<Calendar> calendars = calendarRepository.findAllByDateBetweenOrderByDateAsc(startDateTime, endDateTime);
List<Calendar> calendars = calendarRepository.findByDateBetweenAndMember(startOfMonth.atStartOfDay(), endOfMonth.atStartOfDay(), member);

return calendars.stream()
.map(calendar -> new CalendarInfoDTO(
calendar.getId(),
calendar.getCategoryE(),
calendar.getColor(),
calendar.getDetailFood(),
calendar.getImageUrl()
)).toList();
int blackCount = (int) calendars.stream().filter(calendar -> calendar.getColor() == Color.BLACK).count();
int whiteCount = (int) calendars.stream().filter(calendar -> calendar.getColor() == Color.WHITE).count();

List<CalendarResponseDTO.CalendarResponseMonthlyDTO> calendarInfoDTOS = calendars.stream()
.map(calendar -> new CalendarResponseDTO.CalendarResponseMonthlyDTO(calendar.getDate().toLocalDate(), calendar.getColor()))
.collect(Collectors.toList());

return new CalendarResponseDTO.CalendarResponseMonthlyListDTO(blackCount, whiteCount, calendarInfoDTOS);
}

@Override
@Transactional
//월일별 기록 조회 method
public CalendarResponseDTO getRecord(LocalDate localDate,Member member){
List<CalendarInfoDTO> calendarInfos = getCalendarInfo(localDate);

int blackCount =(int) calendarInfos.stream()
.filter(dto ->
"BLACK".equalsIgnoreCase(
String.valueOf(dto.getColor())))
.count();
int whiteCount = (int) calendarInfos.stream()
.filter(dto ->
"WHITE".equalsIgnoreCase(
String.valueOf(dto.getColor())))
.count();

return new CalendarResponseDTO(blackCount, whiteCount, calendarInfos);
public List<CalendarResponseDTO.CalendarResponseDailyDTO> getDailyRecord(LocalDate date, Member member) {
LocalDateTime startDateTime = date.atTime(0,0,0);
LocalDateTime endDateTime = date.atTime(23, 59, 59);
List<CalendarResponseDTO.CalendarResponseDailyDTO> calendars = calendarRepository.findAllByMemberAndDateBetweenOrderByDateAsc(member, startDateTime, endDateTime)
.stream().map(calendar -> new CalendarResponseDTO.CalendarResponseDailyDTO(
calendar.getColor(),
calendar.getImageUrl(),
calendar.getDetailFood(),
calendar.getCategoryE(),
calendar.getId()
))
.collect(Collectors.toList());

return calendars;
}

//기록 삭제 method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class PostServiceImpl implements PostService {

private final PostRepository postRepository;
private final MemberRepository memberRepository;

@Override
public Post AddPost(String title, String body, String imageUrl, LocalDateTime date, Member member) {
Post newPost = Post.builder()
Expand All @@ -37,6 +37,7 @@ public Post AddPost(String title, String body, String imageUrl, LocalDateTime da
Post savedPost = postRepository.save(newPost);

return savedPost;

} //게시글 등록

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,31 @@ public ApiResponse<Long> registerCalendar(
return ApiResponse.of(SuccessStatus.ADD_CALENDAR_OK, calendarId);
}

@GetMapping
@GetMapping("/api/calender/monthly")
@Operation(
summary = "모든 기록 조회 API")
public ApiResponse<CalendarResponseDTO> showCalendar(
summary = "월별 기록 조회 API")
public ApiResponse<CalendarResponseDTO.CalendarResponseMonthlyListDTO> getMonthlyCalendar(
@Parameter(name = "user", hidden = true) @AuthUser Member member,
@RequestParam(value = "date") LocalDate date){
@RequestParam(value = "year") int year,
@RequestParam(value = "month") int month){

CalendarResponseDTO response = calendarService.getRecord(date, member);
CalendarResponseDTO.CalendarResponseMonthlyListDTO response = calendarService.getMonthlyRecords(year, month, member);

return ApiResponse.of(SuccessStatus.INQUERY_MONTH_CALENDAR_OK,response);
}

@GetMapping("/api/calender/count/daily")
@Operation(
summary = "일별 기록 조회 API")
public ApiResponse<List<CalendarResponseDTO.CalendarResponseDailyDTO>> getDailyCalendar(
@Parameter(name = "user", hidden = true) @AuthUser Member member,
@RequestParam(value = "date") LocalDate date){

List<CalendarResponseDTO.CalendarResponseDailyDTO> response = calendarService.getDailyRecord(date, member);

return ApiResponse.of(SuccessStatus.INQUERY_DATE_CALENDAR_OK,response);
}

@DeleteMapping("/{calendarId}")
@Operation(summary = "기록 삭제")
public ApiResponse<Void> deleteCalendar(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.Midnight.Snacker.web.dto.CalendarDTO;

import com.example.Midnight.Snacker.domain.enums.CategoryE;
import com.example.Midnight.Snacker.domain.enums.Color;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -8,12 +10,40 @@
import java.time.LocalDate;
import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor

public class CalendarResponseDTO {
private int blackCount;
private int whiteCount;
private List<CalendarInfoDTO> calendarInfoDTOS;

private List<CalendarResponseDailyDTO> dailyRecords;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class CalendarResponseDailyDTO {
private Color color;
private String ImgUrl;
private String foodName;
private CategoryE category;
private Long foodId;

}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class CalendarResponseMonthlyListDTO {
private int blackCount;
private int whiteCount;
private List<CalendarResponseMonthlyDTO> calendarInfoDTOS;
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class CalendarResponseMonthlyDTO {
private LocalDate date;
private Color color;

}
}

0 comments on commit 1b0fa99

Please sign in to comment.