Commute Domain
@Entity
public class Commute {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "member_id")
private Long memberId;
private LocalDate date;
private LocalTime start;
private LocalTime end;
@Column(name = "workingminutes")
private long workingMinutes;
public Commute(Long memberId) {
this.memberId = memberId;
}
public void updateDate(){
this.date = LocalDate.now();
}
public void updateStart(){
this.start = LocalTime.now();
}
public void updateEnd(){
this.end = LocalTime.now();
}
public Commute() {
}
public void updateWorkingMinutes() {
// start와 end 사이의 Duration을 계산
Duration duration = Duration.between(start, end);
// 계산된 Duration을 분으로 변환
this.workingMinutes = duration.toMinutes();
}
}
CommuteController
@RestController
public class CommuteController {
private CommuteService commuteService;
public CommuteController(CommuteService commuteService) {
this.commuteService = commuteService;
}
@PostMapping("/commute/start")
public void saveStart(@RequestBody Map<String,Long> data) {
Long memberId = data.get("memberId");
commuteService.saveStart(memberId);
}
@PostMapping("/commute/end")
public void saveEnd(@RequestBody Map<String,Long> data){
Long memberId = data.get("memberId");
commuteService.saveEnd(memberId);
}
@GetMapping("/commutes/member")
public CommuteListResponse getCommutesMember(
@RequestParam Long memberId,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM") YearMonth date) {
return commuteService.getCommutesMember(new CommuteGetRequest(memberId,date.atDay(1)));
}
}
getCommutesMember
@DateTimeFormat
를 이용하여 패턴을 YearMonth
객체로 받아 처리하였습니다.date.atDay(1)
는 해당 월의 첫번째 날의 의미하며 MM을 기준을 하기 때문에 1은 딱히 의미는 없다.CommuteService
@Service
public class CommuteService {
private CommuteRepository commuteRepository;
private MemberRepository memberRepository;
public CommuteService(CommuteRepository commuteRepository, MemberRepository memberRepository) {
this.commuteRepository = commuteRepository;
this.memberRepository = memberRepository;
}
@Transactional
public void saveStart(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(IllegalAccessError::new);
Commute commute = commuteRepository.save(new Commute(memberId));
commute.updateDate();
commute.updateStart();
}
@Transactional
public void saveEnd(Long memberId) {
Commute commute = commuteRepository.findByMemberIdAndDate(memberId, LocalDate.now())
.orElseThrow(IllegalAccessError::new);
commute.updateEnd();
commute.updateWorkingMinutes();
}
public CommuteListResponse getCommutesMember(CommuteGetRequest request) {
//List<CommuteDetail> commuteList = commuteRepository.findByIdAndDate(request.getMemberId(), request.getDate());
YearMonth yearMonth = YearMonth.from(request.getDate());
LocalDate startOfMonth = yearMonth.atDay(1);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
List<CommuteDetail> commuteList = commuteRepository.findByMemberIdAndDateBetween(
request.getMemberId(), startOfMonth, endOfMonth);
int sum = 0;
for (CommuteDetail commuteDetail : commuteList){
sum +=commuteDetail.getWorkingMinutes();
}
return new CommuteListResponse(commuteList, sum);
}
}
Commute Response
@Getter
@AllArgsConstructor
public class CommuteDetail {
private LocalDate date;
private Long workingMinutes;
}
@Getter
@AllArgsConstructor
public class CommuteListResponse {
private List<CommuteDetail> detail;
private int sum;
}