Skip to content

Commit

Permalink
Merge pull request #24 from Timetris-Trendithon/past
Browse files Browse the repository at this point in the history
✨Feat: 이번달 지난 기록 조회 기능 추가
  • Loading branch information
Zena0128 authored Feb 20, 2024
2 parents 9265937 + ef9110a commit 53cf64a
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public static MainPageDTO from(String userName, List<Plan> planList, List<Do> do
List<SeeViewDTO> seeViewDTO1 = seeList.stream()
.map(SeeViewDTO::of)
.toList();

return new MainPageDTO(planViewDTOS, doViewDTOS, seeViewDTO1, userName);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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, Long> {
Date findByDate(LocalDate localDate);
@Query("SELECT d FROM Date d WHERE YEAR(d.date) = :year AND MONTH(d.date) = :month")
List<Date> findAllByYearAndMonth(int year, int month);
}
Empty file.
Original file line number Diff line number Diff line change
@@ -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<List<PastViewDTO>> readPastsController(HttpServletRequest request){
long userId = tokenProvider.getUserId(request);
List<PastViewDTO> pastViewDTOList = pastService.readPastsAll(userId);
return ApiResponse.success(SuccessStatus.OK, pastViewDTOList);
}

}
Original file line number Diff line number Diff line change
@@ -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<PlanViewDTO> planViewDTOList;
List<DoViewDTO> doViewDTOList;
List<SeeViewDTO> seeViewDTO;

public static PastViewDTO from(Date date, List<Plan> planList, List<Do> doList, List<See> seeList){
LocalDate localDate1 = date.getDate();
List<PlanViewDTO> planViewDTOS = planList.stream()
.map(PlanViewDTO::of)
.toList();
List<DoViewDTO> doViewDTOS = doList.stream()
.map(DoViewDTO::of)
.toList();
List<SeeViewDTO> seeViewDTO1 = seeList.stream()
.map(SeeViewDTO::of)
.toList();
return new PastViewDTO(localDate1, planViewDTOS, doViewDTOS, seeViewDTO1);
}
}
Original file line number Diff line number Diff line change
@@ -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<PastViewDTO> readPastsAll(long userId);
}
Original file line number Diff line number Diff line change
@@ -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<PastViewDTO> 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<Date> dateList = dateRepository.findAllByYearAndMonth(year, month);
List<PastViewDTO> result = new ArrayList<>();
dateList.forEach(date -> {
UserDate userDate = userDateRepository.findByUser_IdAndDate_Id(userId, date.getId());
List<Plan> planList = planRepository.findAllByUserDate(userDate);
List<Do> doList = doRepository.findAllByUserDate(userDate);
List<See> see = seeRepository.findByUserDate(userDate);
result.add(PastViewDTO.from(date, planList, doList, see));
});
return result;
}
}

0 comments on commit 53cf64a

Please sign in to comment.