-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
dodam-api/src/main/java/b1nd/dodamapi/outgoing/usecase/OutGoingUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
|
||
} |
62 changes: 14 additions & 48 deletions
62
dodam-core/src/main/java/b1nd/dodamcore/outgoing/application/OutGoingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters