diff --git a/dodam-api/src/main/java/b1nd/dodamapi/outgoing/OutGoingController.java b/dodam-api/src/main/java/b1nd/dodamapi/outgoing/handler/OutGoingController.java similarity index 56% rename from dodam-api/src/main/java/b1nd/dodamapi/outgoing/OutGoingController.java rename to dodam-api/src/main/java/b1nd/dodamapi/outgoing/handler/OutGoingController.java index dd8086e4..0e3029ab 100644 --- a/dodam-api/src/main/java/b1nd/dodamapi/outgoing/OutGoingController.java +++ b/dodam-api/src/main/java/b1nd/dodamapi/outgoing/handler/OutGoingController.java @@ -1,11 +1,10 @@ -package b1nd.dodamapi.outgoing; +package b1nd.dodamapi.outgoing.handler; import b1nd.dodamapi.common.response.Response; import b1nd.dodamapi.common.response.ResponseData; +import b1nd.dodamapi.outgoing.usecase.OutGoingUseCase; import b1nd.dodamcore.outgoing.application.dto.req.RejectOutGoingReq; import b1nd.dodamcore.outgoing.application.dto.res.OutGoingRes; -import b1nd.dodamcore.outgoing.domain.enums.OutGoingStatus; -import b1nd.dodamcore.outgoing.application.OutGoingService; import b1nd.dodamcore.outgoing.application.dto.req.ApplyOutGoingReq; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; @@ -20,51 +19,41 @@ @RequiredArgsConstructor public class OutGoingController { - private final OutGoingService outGoingService; + private final OutGoingUseCase useCase; @PostMapping public Response apply(@RequestBody @Valid ApplyOutGoingReq req) { - outGoingService.apply(req); - - return Response.created("외출 신청 성공"); + return useCase.apply(req); } @PatchMapping("/{id}/allow") public Response allow(@PathVariable Long id) { - outGoingService.modifyStatus(id, OutGoingStatus.ALLOWED, null); - - return Response.noContent("외출 승인 성공"); + return useCase.allow(id); } @PatchMapping("/{id}/reject") public Response reject(@PathVariable Long id, @RequestBody Optional req) { - outGoingService.modifyStatus(id, OutGoingStatus.REJECTED, req.map(RejectOutGoingReq::rejectReason).orElse(null)); - - return Response.noContent("외출 거절 성공"); + return useCase.reject(id, req); } @PatchMapping("/{id}/revert") public Response revert(@PathVariable Long id) { - outGoingService.modifyStatus(id, OutGoingStatus.PENDING, null); - - return Response.noContent("외출 대기 성공"); + return useCase.revert(id); } @DeleteMapping("/{id}") public Response cancel(@PathVariable Long id) { - outGoingService.cancel(id); - - return Response.noContent("외출 취소 완료"); + return useCase.cancel(id); } @GetMapping public ResponseData> getByDate(@RequestParam LocalDate date) { - return ResponseData.ok("날짜별 외출 조회 성공", outGoingService.getByDate(date)); + return useCase.getByDate(date); } @GetMapping("/my") public ResponseData> getMy() { - return ResponseData.ok("내 외출 조회 성공", outGoingService.getMy()); + return useCase.getMy(); } } \ No newline at end of file diff --git a/dodam-api/src/main/java/b1nd/dodamapi/outgoing/usecase/OutGoingUseCase.java b/dodam-api/src/main/java/b1nd/dodamapi/outgoing/usecase/OutGoingUseCase.java new file mode 100644 index 00000000..7d2c5449 --- /dev/null +++ b/dodam-api/src/main/java/b1nd/dodamapi/outgoing/usecase/OutGoingUseCase.java @@ -0,0 +1,78 @@ +package b1nd.dodamapi.outgoing.usecase; + +import b1nd.dodamapi.common.response.Response; +import b1nd.dodamapi.common.response.ResponseData; +import b1nd.dodamcore.common.util.ZonedDateTimeUtil; +import b1nd.dodamcore.member.application.MemberService; +import b1nd.dodamcore.member.domain.entity.Student; +import b1nd.dodamcore.outgoing.application.OutGoingService; +import b1nd.dodamcore.outgoing.application.dto.req.ApplyOutGoingReq; +import b1nd.dodamcore.outgoing.application.dto.req.RejectOutGoingReq; +import b1nd.dodamcore.outgoing.application.dto.res.OutGoingRes; +import b1nd.dodamcore.outgoing.domain.entity.OutGoing; +import b1nd.dodamcore.outgoing.domain.enums.OutGoingStatus; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.List; +import java.util.Optional; + +@Component +@Transactional(rollbackFor = Exception.class) +@RequiredArgsConstructor +public class OutGoingUseCase { + + private final OutGoingService outGoingService; + private final MemberService memberService; + + public Response apply(ApplyOutGoingReq req) { + OutGoing outGoing = req.toEntity(memberService.getStudentFromSession()); + outGoingService.save(outGoing); + return Response.created("외출 신청 성공"); + } + + public Response cancel(Long id) { + OutGoing outGoing = outGoingService.getById(id); + outGoingService.delete(outGoing); + return Response.noContent("외출 취소 성공"); + } + + public Response allow(Long id) { + modifyStatus(id, OutGoingStatus.ALLOWED, null); + return Response.noContent("외출 승인 성공"); + } + + public Response reject(Long id, Optional req) { + modifyStatus(id, OutGoingStatus.REJECTED, req.map(RejectOutGoingReq::rejectReason).orElse(null)); + return Response.noContent("외출 거절 성공"); + } + + public Response revert(Long id) { + modifyStatus(id, OutGoingStatus.PENDING, null); + return Response.noContent("외출 대기 성공"); + } + + private void modifyStatus(Long id, OutGoingStatus status, String rejectReason) { + OutGoing outGoing = outGoingService.getById(id); + outGoing.modifyStatus(memberService.getTeacherFromSession(), status, rejectReason); + } + + @Transactional(readOnly = true) + public ResponseData> getByDate(LocalDate date) { + LocalDateTime startDay = LocalDateTime.of(date, LocalTime.MIN); + LocalDateTime endDay = LocalDateTime.of(date, LocalTime.MAX); + return ResponseData.ok("날짜별 외출 조회 성공", OutGoingRes.of(outGoingService.getByDate(startDay, endDay))); + } + + @Transactional(readOnly = true) + public ResponseData> getMy() { + Student student = memberService.getStudentFromSession(); + LocalDateTime now = ZonedDateTimeUtil.nowToLocalDateTime(); + return ResponseData.ok("내 외출 조회 성공", OutGoingRes.of(outGoingService.getByStudent(student, now))); + } + +} diff --git a/dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/OutGoingService.java b/dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/OutGoingService.java index 0d912a68..c88b47ae 100644 --- a/dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/OutGoingService.java +++ b/dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/OutGoingService.java @@ -1,74 +1,40 @@ package b1nd.dodamcore.outgoing.application; -import b1nd.dodamcore.common.util.ZonedDateTimeUtil; -import b1nd.dodamcore.outgoing.application.dto.res.OutGoingRes; -import b1nd.dodamcore.outgoing.domain.enums.OutGoingStatus; -import b1nd.dodamcore.member.application.MemberSessionHolder; import b1nd.dodamcore.member.domain.entity.Student; -import b1nd.dodamcore.member.domain.entity.Teacher; -import b1nd.dodamcore.member.repository.StudentRepository; -import b1nd.dodamcore.member.repository.TeacherRepository; -import b1nd.dodamcore.outgoing.application.dto.req.ApplyOutGoingReq; import b1nd.dodamcore.outgoing.domain.entity.OutGoing; +import b1nd.dodamcore.outgoing.domain.exception.OutGoingNotFoundException; import b1nd.dodamcore.outgoing.repository.OutGoingRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import java.time.LocalDate; import java.time.LocalDateTime; -import java.time.LocalTime; import java.util.List; @Service @RequiredArgsConstructor public class OutGoingService { - private final OutGoingRepository outGoingRepository; - private final StudentRepository studentRepository; - private final TeacherRepository teacherRepository; - private final MemberSessionHolder memberSessionHolder; + private final OutGoingRepository repository; - @Transactional - public void apply(ApplyOutGoingReq req) { - Student student = studentRepository.getByMember(memberSessionHolder.current()); - OutGoing outGoing = req.toEntity(student); - - outGoingRepository.save(outGoing); + public void save(OutGoing outGoing) { + repository.save(outGoing); } - @Transactional - public void modifyStatus(Long id, OutGoingStatus status, String rejectReason) { - Teacher teacher = teacherRepository.getByMember(memberSessionHolder.current()); - OutGoing outGoing = outGoingRepository.getById(id); - - outGoing.modifyStatus(teacher, status, rejectReason); + public void delete(OutGoing outGoing) { + repository.delete(outGoing); } - @Transactional - public void cancel(Long id) { - OutGoing outGoing = outGoingRepository.getById(id); - Student student = studentRepository.getByMember(memberSessionHolder.current()); - - outGoing.isApplicant(student); - - outGoingRepository.delete(outGoing); + public OutGoing getById(Long id) { + return repository.findById(id) + .orElseThrow(OutGoingNotFoundException::new); } - @Transactional(readOnly = true) - public List getByDate(LocalDate date) { - LocalDateTime startOfTheDay = LocalDateTime.of(date, LocalTime.MIN); - LocalDateTime endOfTheDay = LocalDateTime.of(date, LocalTime.MAX); - - return OutGoingRes.of(outGoingRepository.findByDate(startOfTheDay, endOfTheDay)); + public List getByDate(LocalDateTime startDay, LocalDateTime endDay) { + return repository.findByDate(startDay, endDay); } - @Transactional(readOnly = true) - public List getMy() { - Student student = studentRepository.getByMember(memberSessionHolder.current()); - LocalDateTime now = ZonedDateTimeUtil.nowToLocalDateTime(); - - return OutGoingRes.of(outGoingRepository.findByStudentAndEndAtGreaterThanEqual(student, now)); + public List getByStudent(Student student, LocalDateTime now) { + return repository.findByStudentAndEndAtGreaterThanEqual(student, now); } -} \ No newline at end of file +} diff --git a/dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/dto/res/OutGoingRes.java b/dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/dto/res/OutGoingRes.java index 5ca70415..4ca9ef8c 100644 --- a/dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/dto/res/OutGoingRes.java +++ b/dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/dto/res/OutGoingRes.java @@ -10,7 +10,7 @@ public record OutGoingRes(Long id, String reason, OutGoingStatus status, LocalDateTime startAt, LocalDateTime endAt, StudentRes student, String rejectReason, LocalDateTime createdAt, LocalDateTime modifiedAt) { public static List of(List oList) { - return oList.stream() + return oList.parallelStream() .map(OutGoingRes::of) .toList(); }