Skip to content

Commit

Permalink
🐛 2일 이상 일정인 경우, 시작일자 기준으로만 카운팅되던 버그 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
swandevson committed Sep 6, 2024
1 parent 4ae7f9d commit d5e944a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package com.demo.chattodo.domain.controller;


import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import com.demo.chattodo.domain.dto.request.ScheduleUpdateDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo.chattodo.domain.dto.request.ScheduleCreateDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import com.demo.chattodo.domain.dto.request.ScheduleUpdateDTO;
import com.demo.chattodo.domain.dto.response.ScheduleCountResponseDTO;
import com.demo.chattodo.domain.service.ScheduleService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
Expand All @@ -29,28 +31,41 @@
public class ScheduleController {
private final ScheduleService scheduleService;


@GetMapping
public List<ScheduleCountResponseDTO> countScheduleOfEachDay(
@RequestHeader("member_id")String memberId,
@RequestParam("start_date") LocalDate startDate,
@RequestParam("end_date")LocalDate endDate) {
@RequestHeader("member_id") String memberId,
@RequestParam("start_date") LocalDate startDate,
@RequestParam(value = "start_time", required = false) LocalTime startTime,
@RequestParam("end_date") LocalDate endDate,
@RequestParam(value = "end_time", required = false) LocalTime endTime) {

if (startTime == null) {
startTime = LocalTime.MIN;
}

if (endTime == null) {
endTime = LocalTime.MAX;
}

return scheduleService.countScheduleOfEachDay(memberId, startDate, endDate);
return scheduleService.countScheduleOfEachDay(
memberId,
startDate.atTime(startTime),
endDate.atTime(endTime)
);
}

@PostMapping
public ResponseEntity<?> createSchedule(
@RequestHeader("member_id") String memberId,
@RequestBody ScheduleCreateDTO dto) {
@RequestHeader("member_id") String memberId,
@RequestBody ScheduleCreateDTO dto) {

return ResponseEntity.ok().body(scheduleService.saveSchedule(memberId, dto));
}

@DeleteMapping("/{scheduleId}")
public ResponseEntity<?> deleteSchedule(
@RequestHeader("member_id") String memberId,
@PathVariable Long scheduleId) {
@RequestHeader("member_id") String memberId,
@PathVariable Long scheduleId) {

if (scheduleService.deleteSchedule(memberId, scheduleId)) {
return ResponseEntity.ok().build();
Expand All @@ -61,9 +76,9 @@ public ResponseEntity<?> deleteSchedule(

@PutMapping("/{scheduleId}")
public ResponseEntity<?> updateSchedule(
@RequestHeader("member_id") String memberId,
@PathVariable Long scheduleId,
@RequestBody ScheduleUpdateDTO dto) {
@RequestHeader("member_id") String memberId,
@PathVariable Long scheduleId,
@RequestBody ScheduleUpdateDTO dto) {

scheduleService.updateSchedule(memberId, scheduleId, dto);
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import java.time.LocalDate;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@AllArgsConstructor
@Builder
public class ScheduleCountResponseDTO {
private LocalDate date;
private Long count;
private LocalDate date;
private Long count;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

public interface ScheduleRepository extends JpaRepository<Schedule, Long> {
// 유저의 일정기간내 모든 일정 가져오기
@Query("SELECT s FROM Schedule s WHERE s.memberId = :memberId AND s.startDateTime >= :startDate AND s.endDateTime < :endDate")
List<Schedule> findAllByDateRangeAndMemberId(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate, @Param("memberId") String memberId);
@Query("SELECT s FROM Schedule s WHERE s.memberId = :memberId AND s.startDateTime >= :startDate AND s.endDateTime <= :endDate ORDER BY s.startDateTime ASC ")
List<Schedule> findAllByDateRangeAndMemberId(@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate, @Param("memberId") String memberId);

Optional<Schedule> findByIdAndMemberId(Long id, String memberId);
}
71 changes: 33 additions & 38 deletions src/main/java/com/demo/chattodo/domain/service/ScheduleService.java
Original file line number Diff line number Diff line change
@@ -1,73 +1,68 @@
package com.demo.chattodo.domain.service;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


import com.demo.chattodo.domain.dto.request.ScheduleCreateDTO;
import com.demo.chattodo.domain.dto.request.ScheduleUpdateDTO;
import com.demo.chattodo.domain.entity.Schedule;
import com.demo.chattodo.domain.utils.DateTimeUtil;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.chattodo.domain.dto.request.ScheduleCreateDTO;
import com.demo.chattodo.domain.dto.request.ScheduleUpdateDTO;
import com.demo.chattodo.domain.dto.response.ScheduleCountResponseDTO;
import com.demo.chattodo.domain.entity.Schedule;
import com.demo.chattodo.domain.repository.ScheduleRepository;
import com.demo.chattodo.domain.utils.DateTimeUtil;

import lombok.RequiredArgsConstructor;



@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ScheduleService {
private final ScheduleRepository scheduleRepository;


public List<ScheduleCountResponseDTO> countScheduleOfEachDay(String memberId, LocalDate startDate, LocalDate endDate) {
public List<ScheduleCountResponseDTO> countScheduleOfEachDay(String memberId, LocalDateTime startDateTime,
LocalDateTime endDateTime) {
List<Schedule> schedules = scheduleRepository.findAllByDateRangeAndMemberId(
LocalDateTime.of(startDate, LocalTime.MIN),
LocalDateTime.of(endDate, LocalTime.MIN).plusDays(1),
startDateTime,
endDateTime,
memberId
);

List<ScheduleCountResponseDTO> response = new ArrayList<>();
for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
LocalDate finalDate = date;
long count = schedules.stream()
.filter(schedule -> schedule.getStartDateTime().toLocalDate().equals(finalDate))
.count();

if (count == 0) {
continue;
}
Map<LocalDate, Long> countMap = new HashMap<>();
for (Schedule schedule : schedules) {
LocalDate startDate = schedule.getStartDateTime().toLocalDate();
LocalDate endDate = schedule.getEndDateTime().toLocalDate();

response.add(ScheduleCountResponseDTO.builder()
.date(date)
.count(count)
.build());
for (LocalDate date = startDate; date.isBefore(endDate) || date.isEqual(endDate); date = date.plusDays(1)) {
countMap.put(date, countMap.getOrDefault(date, 0L) + 1L);
}
}

return response;
// countMap의 요소를 key값으로 정렬하여 이를 DTO List로 만들어 return
List<ScheduleCountResponseDTO> response = new ArrayList<>();

countMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(entry -> response.add(new ScheduleCountResponseDTO(entry.getKey(), entry.getValue())));

return response;
}

@Transactional
public Long saveSchedule(String memberId, ScheduleCreateDTO dto) {

Schedule schedule = new Schedule(
memberId,
dto.getTitle(),
DateTimeUtil.getStartLocalDateTime(dto.getStartDate(), dto.getStartTime()),
DateTimeUtil.getEndLocalDateTime(dto.getEndDate(), dto.getEndTime()),
dto.getPlace()
memberId,
dto.getTitle(),
DateTimeUtil.getStartLocalDateTime(dto.getStartDate(), dto.getStartTime()),
DateTimeUtil.getEndLocalDateTime(dto.getEndDate(), dto.getEndTime()),
dto.getPlace()
);

scheduleRepository.save(schedule);
Expand All @@ -90,9 +85,9 @@ public boolean deleteSchedule(String memberId, Long scheduleId) {
@Transactional
public void updateSchedule(String memberId, Long scheduleId, ScheduleUpdateDTO dto) {
scheduleRepository.findByIdAndMemberId(scheduleId, memberId)
.ifPresent(schedule -> schedule.update(dto.getTitle(),
DateTimeUtil.getStartLocalDateTime(dto.getStartDate(), dto.getStartTime()),
DateTimeUtil.getEndLocalDateTime(dto.getEndDate(), dto.getEndTime()),
dto.getPlace()));
.ifPresent(schedule -> schedule.update(dto.getTitle(),
DateTimeUtil.getStartLocalDateTime(dto.getStartDate(), dto.getStartTime()),
DateTimeUtil.getEndLocalDateTime(dto.getEndDate(), dto.getEndTime()),
dto.getPlace()));
}
}

0 comments on commit d5e944a

Please sign in to comment.