diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/dto/MainPageDTO.java b/src/main/java/com/trendithon/timetris/domain/mainpage/dto/MainPageDTO.java index 7dd10dd..5f47239 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/dto/MainPageDTO.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/dto/MainPageDTO.java @@ -28,9 +28,6 @@ public static MainPageDTO from(String userName, List planList, List do List seeViewDTO1 = seeList.stream() .map(SeeViewDTO::of) .toList(); - return new MainPageDTO(planViewDTOS, doViewDTOS, seeViewDTO1, userName); - } - } diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/repository/DateRepository.java b/src/main/java/com/trendithon/timetris/domain/mainpage/repository/DateRepository.java index 6c5a74c..10082fe 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/repository/DateRepository.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/repository/DateRepository.java @@ -2,9 +2,15 @@ import com.trendithon.timetris.domain.mainpage.domain.Date; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import java.time.LocalDate; +import java.time.Month; +import java.time.Year; +import java.util.List; public interface DateRepository extends JpaRepository { Date findByDate(LocalDate localDate); + @Query("SELECT d FROM Date d WHERE YEAR(d.date) = :year AND MONTH(d.date) = :month") + List findAllByYearAndMonth(int year, int month); } diff --git a/src/main/java/com/trendithon/timetris/domain/past/controller/.dummy b/src/main/java/com/trendithon/timetris/domain/past/controller/.dummy deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/trendithon/timetris/domain/past/controller/PastController.java b/src/main/java/com/trendithon/timetris/domain/past/controller/PastController.java new file mode 100644 index 0000000..bbb3990 --- /dev/null +++ b/src/main/java/com/trendithon/timetris/domain/past/controller/PastController.java @@ -0,0 +1,31 @@ +package com.trendithon.timetris.domain.past.controller; + +import com.trendithon.timetris.domain.past.dto.PastViewDTO; +import com.trendithon.timetris.domain.past.service.PastService; +import com.trendithon.timetris.global.auth.jwt.TokenProvider; +import com.trendithon.timetris.global.exception.ApiResponse; +import com.trendithon.timetris.global.exception.enums.SuccessStatus; +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/past") +public class PastController { + + private final PastService pastService; + private final TokenProvider tokenProvider; + + @GetMapping("") + public ApiResponse> readPastsController(HttpServletRequest request){ + long userId = tokenProvider.getUserId(request); + List pastViewDTOList = pastService.readPastsAll(userId); + return ApiResponse.success(SuccessStatus.OK, pastViewDTOList); + } + +} diff --git a/src/main/java/com/trendithon/timetris/domain/past/dto/PastViewDTO.java b/src/main/java/com/trendithon/timetris/domain/past/dto/PastViewDTO.java new file mode 100644 index 0000000..148adb5 --- /dev/null +++ b/src/main/java/com/trendithon/timetris/domain/past/dto/PastViewDTO.java @@ -0,0 +1,40 @@ +package com.trendithon.timetris.domain.past.dto; + +import com.trendithon.timetris.domain.mainpage.domain.Date; +import com.trendithon.timetris.domain.mainpage.domain.Do; +import com.trendithon.timetris.domain.mainpage.domain.Plan; +import com.trendithon.timetris.domain.mainpage.domain.See; +import com.trendithon.timetris.domain.mainpage.dto.DoViewDTO; +import com.trendithon.timetris.domain.mainpage.dto.MainPageDTO; +import com.trendithon.timetris.domain.mainpage.dto.PlanViewDTO; +import com.trendithon.timetris.domain.mainpage.dto.SeeViewDTO; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; +import java.util.List; + +@Getter +@AllArgsConstructor @NoArgsConstructor +public class PastViewDTO { + + LocalDate localDate; + List planViewDTOList; + List doViewDTOList; + List seeViewDTO; + + public static PastViewDTO from(Date date, List planList, List doList, List seeList){ + LocalDate localDate1 = date.getDate(); + List planViewDTOS = planList.stream() + .map(PlanViewDTO::of) + .toList(); + List doViewDTOS = doList.stream() + .map(DoViewDTO::of) + .toList(); + List seeViewDTO1 = seeList.stream() + .map(SeeViewDTO::of) + .toList(); + return new PastViewDTO(localDate1, planViewDTOS, doViewDTOS, seeViewDTO1); + } +} diff --git a/src/main/java/com/trendithon/timetris/domain/past/service/PastService.java b/src/main/java/com/trendithon/timetris/domain/past/service/PastService.java new file mode 100644 index 0000000..f3f596d --- /dev/null +++ b/src/main/java/com/trendithon/timetris/domain/past/service/PastService.java @@ -0,0 +1,10 @@ +package com.trendithon.timetris.domain.past.service; + +import com.trendithon.timetris.domain.past.dto.PastViewDTO; +import org.springframework.stereotype.Service; +import java.util.List; + +@Service +public interface PastService { + List readPastsAll(long userId); +} diff --git a/src/main/java/com/trendithon/timetris/domain/past/service/PathServiceImpl.java b/src/main/java/com/trendithon/timetris/domain/past/service/PathServiceImpl.java new file mode 100644 index 0000000..ceae39f --- /dev/null +++ b/src/main/java/com/trendithon/timetris/domain/past/service/PathServiceImpl.java @@ -0,0 +1,51 @@ +package com.trendithon.timetris.domain.past.service; + +import com.trendithon.timetris.domain.mainpage.domain.*; +import com.trendithon.timetris.domain.mainpage.dto.MainPageDTO; +import com.trendithon.timetris.domain.mainpage.repository.*; +import com.trendithon.timetris.domain.member.domain.User; +import com.trendithon.timetris.domain.member.repository.UserRepository; +import com.trendithon.timetris.domain.past.dto.PastViewDTO; +import com.trendithon.timetris.global.exception.CustomException; +import com.trendithon.timetris.global.exception.enums.ErrorStatus; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; +import java.time.Month; +import java.time.Year; +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class PathServiceImpl implements PastService{ + + private final DateRepository dateRepository; + private final UserRepository userRepository; + private final UserDateRepository userDateRepository; + private final PlanRepository planRepository; + private final DoRepository doRepository; + private final SeeRepository seeRepository; + + @Override + public List readPastsAll(long userId) { + LocalDate localDate = LocalDate.now(); + int year = localDate.getYear(); + int month = localDate.getMonth().getValue(); + User user = userRepository.findById(userId) + .orElseThrow(() -> new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR)); + List dateList = dateRepository.findAllByYearAndMonth(year, month); + List result = new ArrayList<>(); + dateList.forEach(date -> { + UserDate userDate = userDateRepository.findByUser_IdAndDate_Id(userId, date.getId()); + List planList = planRepository.findAllByUserDate(userDate); + List doList = doRepository.findAllByUserDate(userDate); + List see = seeRepository.findByUserDate(userDate); + result.add(PastViewDTO.from(date, planList, doList, see)); + }); + return result; + } +}