Skip to content

Commit

Permalink
refactor: OutGoing UseCase 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
suw0n committed May 16, 2024
1 parent a5a21ed commit 0b2e080
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<RejectOutGoingReq> 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<List<OutGoingRes>> getByDate(@RequestParam LocalDate date) {
return ResponseData.ok("날짜별 외출 조회 성공", outGoingService.getByDate(date));
return useCase.getByDate(date);
}

@GetMapping("/my")
public ResponseData<List<OutGoingRes>> getMy() {
return ResponseData.ok("내 외출 조회 성공", outGoingService.getMy());
return useCase.getMy();
}

}
Original file line number Diff line number Diff line change
@@ -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<RejectOutGoingReq> 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<List<OutGoingRes>> 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<List<OutGoingRes>> getMy() {
Student student = memberService.getStudentFromSession();
LocalDateTime now = ZonedDateTimeUtil.nowToLocalDateTime();
return ResponseData.ok("내 외출 조회 성공", OutGoingRes.of(outGoingService.getByStudent(student, now)));
}

}
Original file line number Diff line number Diff line change
@@ -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<OutGoingRes> 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<OutGoing> getByDate(LocalDateTime startDay, LocalDateTime endDay) {
return repository.findByDate(startDay, endDay);
}

@Transactional(readOnly = true)
public List<OutGoingRes> getMy() {
Student student = studentRepository.getByMember(memberSessionHolder.current());
LocalDateTime now = ZonedDateTimeUtil.nowToLocalDateTime();

return OutGoingRes.of(outGoingRepository.findByStudentAndEndAtGreaterThanEqual(student, now));
public List<OutGoing> getByStudent(Student student, LocalDateTime now) {
return repository.findByStudentAndEndAtGreaterThanEqual(student, now);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutGoingRes> of(List<OutGoing> oList) {
return oList.stream()
return oList.parallelStream()
.map(OutGoingRes::of)
.toList();
}
Expand Down

0 comments on commit 0b2e080

Please sign in to comment.