|
10 | 10 | import com.grepp.teamnotfound.app.model.schedule.repository.ScheduleRepository; |
11 | 11 | import com.grepp.teamnotfound.app.model.user.entity.User; |
12 | 12 | import com.grepp.teamnotfound.app.model.user.repository.UserRepository; |
| 13 | +import com.grepp.teamnotfound.app.model.vaccination.code.VaccineName; |
13 | 14 | import com.grepp.teamnotfound.infra.error.exception.PetException; |
14 | 15 | import com.grepp.teamnotfound.infra.error.exception.ScheduleException; |
15 | 16 | import com.grepp.teamnotfound.infra.error.exception.UserException; |
|
21 | 22 | import java.time.YearMonth; |
22 | 23 | import java.util.ArrayList; |
23 | 24 | import java.util.List; |
| 25 | +import java.util.Optional; |
| 26 | + |
24 | 27 | import lombok.RequiredArgsConstructor; |
25 | 28 | import org.springframework.stereotype.Service; |
26 | 29 | import org.springframework.transaction.annotation.Transactional; |
@@ -217,4 +220,34 @@ public void deleteVaccinationSchedule(Pet pet, String keywords) { |
217 | 220 | schedules.forEach(schedule -> schedule.setDeletedAt(OffsetDateTime.now())); |
218 | 221 | scheduleRepository.saveAll(schedules); |
219 | 222 | } |
| 223 | + |
| 224 | + @Transactional |
| 225 | + public List<ScheduleDto> getNextVaccination(Long userId, Long petId) { |
| 226 | + Pet pet = petRepository.findById(petId).orElseThrow(() -> new PetException(PetErrorCode.PET_NOT_FOUND)); |
| 227 | + userRepository.findById(userId).orElseThrow(() -> new UserException(UserErrorCode.USER_NOT_FOUND)); |
| 228 | + |
| 229 | + List<Schedule> schedules = new ArrayList<>(); |
| 230 | + for (VaccineName name: VaccineName.values()){ |
| 231 | + Optional<Schedule> schedule = scheduleRepository.findTop1ByPetAndNameContainingAndDeletedAtNullAndScheduleDateAfterOrderByScheduleDateAsc(pet, name.name(), LocalDate.now()); |
| 232 | + if (schedule.isPresent()){ |
| 233 | + schedules.add(schedule.get()); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + List<ScheduleDto> scheduleDtos = new ArrayList<>(); |
| 238 | + |
| 239 | + schedules.forEach(schedule -> |
| 240 | + scheduleDtos.add(ScheduleDto.builder() |
| 241 | + .scheduleId(schedule.getScheduleId()) |
| 242 | + .date(schedule.getScheduleDate()) |
| 243 | + .name(schedule.getName()) |
| 244 | + .cycle(schedule.getCycle()) |
| 245 | + .cycleEnd(schedule.getCycleEnd()) |
| 246 | + .isDone(schedule.getIsDone()) |
| 247 | + .petName(schedule.getPet().getName()) |
| 248 | + .petId(schedule.getPet().getPetId()).build()) |
| 249 | + ); |
| 250 | + |
| 251 | + return scheduleDtos; |
| 252 | + } |
220 | 253 | } |
0 commit comments