Skip to content

Commit

Permalink
Merge pull request #112 from Team-B1ND/Feature/#111
Browse files Browse the repository at this point in the history
feat#111: 외출 시 급식 수요 조회 및 조사 api
  • Loading branch information
dongchandev authored Sep 18, 2024
2 parents 881d5e9 + f452357 commit 5f92579
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bin/
out/
!**/src/main/**/out/
!**/src/test/**/out/
dodam-application/dodam-rest-api/src/main/resources/dodamdodam-firebase-key.json

### NetBeans ###
/nbproject/private/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import b1nd.dodam.core.util.ZonedDateTimeUtil;
import b1nd.dodam.domain.rds.member.entity.Student;
import b1nd.dodam.domain.rds.member.enumeration.ActiveStatus;
import b1nd.dodam.domain.rds.member.repository.StudentRepository;
import b1nd.dodam.domain.rds.member.repository.TeacherRepository;
import b1nd.dodam.domain.rds.outgoing.entity.OutGoing;
Expand All @@ -11,6 +12,7 @@
import b1nd.dodam.restapi.auth.infrastructure.security.support.MemberAuthenticationHolder;
import b1nd.dodam.restapi.outgoing.application.data.req.ApplyOutGoingReq;
import b1nd.dodam.restapi.outgoing.application.data.req.RejectOutGoingReq;
import b1nd.dodam.restapi.outgoing.application.data.res.OutGoingMealCountRes;
import b1nd.dodam.restapi.outgoing.application.data.res.OutGoingRes;
import b1nd.dodam.restapi.support.data.Response;
import b1nd.dodam.restapi.support.data.ResponseData;
Expand Down Expand Up @@ -48,8 +50,15 @@ public Response cancel(Long id) {
return Response.noContent("외출 취소 성공");
}

@Transactional(readOnly = true)
public ResponseData<OutGoingMealCountRes> getMealDemandDuringOuting(LocalDate date) {
Long nonEatersCount = outGoingService.getTodayCountByDinnerOrNotAndDate(Boolean.FALSE, date);
Long eatersCount = studentRepository.countByMemberStatus(ActiveStatus.ACTIVE) - nonEatersCount;
return ResponseData.ok("외출 중 급식 수요 조회 성공", new OutGoingMealCountRes(eatersCount, nonEatersCount));
}

private void throwExceptionWhenStudentIsNotApplicant(OutGoing outGoing) {
if(outGoing.isNotApplicant(studentRepository.getByMember(memberAuthenticationHolder.current()))) {
if (outGoing.isNotApplicant(studentRepository.getByMember(memberAuthenticationHolder.current()))) {
throw new NotOutGoingApplicantException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@

import java.time.LocalDateTime;

public record ApplyOutGoingReq(@NotBlank String reason, @NotNull LocalDateTime startAt, @NotNull LocalDateTime endAt) {
public record ApplyOutGoingReq(
@NotBlank String reason,
@NotNull LocalDateTime startAt,
@NotNull LocalDateTime endAt,
Boolean dinnerOrNot
) {
public OutGoing toEntity(Student student) {
Boolean dinner = (dinnerOrNot != null) ? dinnerOrNot : true;
return OutGoing.builder()
.reason(reason)
.student(student)
.startAt(startAt)
.endAt(endAt)
.dinnerOrNot(dinner)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package b1nd.dodam.restapi.outgoing.application.data.res;

public record OutGoingMealCountRes(
Long eatersCount,
Long nonEatersCount
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import b1nd.dodam.restapi.outgoing.application.OutGoingUseCase;
import b1nd.dodam.restapi.outgoing.application.data.req.ApplyOutGoingReq;
import b1nd.dodam.restapi.outgoing.application.data.req.RejectOutGoingReq;
import b1nd.dodam.restapi.outgoing.application.data.res.OutGoingMealCountRes;
import b1nd.dodam.restapi.outgoing.application.data.res.OutGoingRes;
import b1nd.dodam.restapi.support.data.Response;
import b1nd.dodam.restapi.support.data.ResponseData;
Expand Down Expand Up @@ -56,4 +57,9 @@ public ResponseData<List<OutGoingRes>> getMy() {
return useCase.getMy();
}

}
@GetMapping("/meal-demand")
public ResponseData<OutGoingMealCountRes> getMealDemand(@RequestParam LocalDate date) {
return useCase.getMealDemandDuringOuting(date);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import b1nd.dodam.domain.rds.member.entity.Member;
import b1nd.dodam.domain.rds.member.entity.Student;
import b1nd.dodam.domain.rds.member.enumeration.ActiveStatus;
import b1nd.dodam.domain.rds.member.exception.StudentNotFoundException;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down Expand Up @@ -36,4 +37,6 @@ default List<Student> getByIds(List<Integer> ids) {
throw new StudentNotFoundException();
}

}
Long countByMemberStatus(ActiveStatus memberStatus);

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@ public class OutGoing extends BaseEntity {

private String rejectReason;

@NotNull
private Boolean dinnerOrNot;

@Builder
public OutGoing(String reason, LocalDateTime startAt, LocalDateTime endAt, Student student) {
public OutGoing(String reason, LocalDateTime startAt, LocalDateTime endAt, Student student, Boolean dinnerOrNot) {
isInvalidPeriod(startAt, endAt);

this.reason = reason;
this.startAt = startAt;
this.endAt = endAt;
this.status = ApprovalStatus.PENDING;
this.student = student;
this.dinnerOrNot = dinnerOrNot;
}

public void modifyStatus(Teacher teacher, ApprovalStatus status, String rejectReason) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import b1nd.dodam.domain.rds.member.entity.Student;
import b1nd.dodam.domain.rds.outgoing.entity.OutGoing;
import b1nd.dodam.domain.rds.outgoing.exception.OutGoingNotFoundException;
import b1nd.dodam.domain.rds.support.enumeration.ApprovalStatus;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -25,4 +26,6 @@ default OutGoing getById(Long id) {
@EntityGraph(attributePaths = {"student.member"})
List<OutGoing> findByStudentAndEndAtGreaterThanEqual(Student student, LocalDateTime now);

@Query("SELECT COUNT(o) FROM OutGoing o WHERE o.dinnerOrNot = :dinnerOrNot AND o.status = :status AND o.startAt BETWEEN :startOfDay AND :endOfDay")
Long countByDinnerOrNotAndStatusAndStartAtBetween(Boolean dinnerOrNot, ApprovalStatus status, LocalDateTime startOfDay, LocalDateTime endOfDay);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import b1nd.dodam.domain.rds.outgoing.entity.OutGoing;
import b1nd.dodam.domain.rds.outgoing.exception.OutGoingNotFoundException;
import b1nd.dodam.domain.rds.outgoing.repository.OutGoingRepository;
import b1nd.dodam.domain.rds.support.enumeration.ApprovalStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
Expand Down Expand Up @@ -37,4 +40,9 @@ public List<OutGoing> getByStudent(Student student, LocalDateTime now) {
return repository.findByStudentAndEndAtGreaterThanEqual(student, now);
}

public Long getTodayCountByDinnerOrNotAndDate(Boolean dinnerOrNot, LocalDate date){
LocalDateTime startOfDay = date.atStartOfDay();
LocalDateTime endOfDay = date.atTime(LocalTime.MAX);
return repository.countByDinnerOrNotAndStatusAndStartAtBetween(dinnerOrNot, ApprovalStatus.ALLOWED, startOfDay, endOfDay);
}
}

0 comments on commit 5f92579

Please sign in to comment.