Skip to content

Commit

Permalink
Merge pull request #44 from 9oormthonUniv-seoultech/Fix/Classroombug
Browse files Browse the repository at this point in the history
[FIX] 관리자 강의실 조회 버그 �수정
  • Loading branch information
chaeyoungeee authored Nov 9, 2024
2 parents 090be53 + 450a122 commit 7a17647
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.groomiz.billage.classroom.dto;

import com.groomiz.billage.classroom.entity.Classroom;
import com.groomiz.billage.global.dto.PageRequestDto;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class AdminReservationSearchByClassroomCond extends PageRequestDto {

private Classroom classroom;
private LocalDate applyDate;

@Builder
public AdminReservationSearchByClassroomCond(Classroom classroom, LocalDate applyDate) {
this.classroom = classroom;
this.applyDate = applyDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.LocalTime;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.groomiz.billage.classroom.entity.Classroom;
import com.groomiz.billage.member.entity.Member;
import com.groomiz.billage.reservation.entity.Reservation;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class AdminClassroomDetailResponse {
private List<ReservationDetail> reservations;

@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor
@Schema(description = "예약 상세 정보")
public static class ReservationDetail {

Expand All @@ -49,10 +50,12 @@ public static class ReservationDetail {
@Schema(description = "예약 날짜", example = "2024-09-04")
private LocalDate date;

@Schema(description = "시작 시간", example = "09:00")
@Schema(description = "시작 시간", example = "09:00", type = "string")
@JsonFormat(pattern = "HH:mm")
private LocalTime startTime;

@Schema(description = "종료 시간", example = "10:00")
@Schema(description = "종료 시간", example = "10:00", type = "string")
@JsonFormat(pattern = "HH:mm")
private LocalTime endTime;

@Schema(description = "인원 수", example = "10")
Expand All @@ -63,35 +66,6 @@ public static class ReservationDetail {

@Schema(description = "예약자 학번", example = "20201234")
private String studentNumber;

@Builder
public ReservationDetail(Long reservationId, ReservationStatusType status, LocalDate date, LocalTime startTime, LocalTime endTime,
Integer headcount,
String memberName, String studentNumber) {
this.reservationId = reservationId;
this.status = status;
this.date = date;
this.startTime = startTime;
this.endTime = endTime;
this.headcount = headcount;
this.memberName = memberName;
this.studentNumber = studentNumber;
}

public static ReservationDetail from(Reservation reservation) {
Member requester = reservation.getReservationStatus().getRequester();

return ReservationDetail.builder()
.reservationId(reservation.getId())
.status(reservation.getReservationStatus().getStatus())
.date(reservation.getApplyDate())
.startTime(reservation.getStartTime())
.endTime(reservation.getEndTime())
.headcount(reservation.getHeadcount())
.memberName(requester.getUsername())
.studentNumber(requester.getStudentNumber())
.build();
}
}

@Builder
Expand All @@ -104,18 +78,13 @@ public AdminClassroomDetailResponse(LocalDate date, String buildingName, Long fl
this.reservations = reservations;
}

public static AdminClassroomDetailResponse from(Classroom classroom, LocalDate date) {
List<Reservation> reservations = classroom.getReservations();


public static AdminClassroomDetailResponse from(Classroom classroom, List<ReservationDetail> reservations, LocalDate date) {
return AdminClassroomDetailResponse.builder()
.date(date)
.buildingName(classroom.getBuilding().getName())
.floor(classroom.getFloor())
.classroomName(classroom.getName())
.reservations(reservations.stream()
.map(ReservationDetail::from)
.toList())
.reservations(reservations)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@
import com.groomiz.billage.classroom.entity.Classroom;

public interface ClassroomRepository extends JpaRepository<Classroom, Long> {

@Query("SELECT c FROM Classroom c "
+ "JOIN FETCH c.reservations r "
+ "JOIN FETCH r.reservationStatus rs "
+ "WHERE c.id = :classroomId "
+ "AND r.applyDate = :date "
+ "AND rs.status in ('PENDING', 'APPROVED')")
Optional<Classroom> findClassroomByIdAndDate(Long classroomId, LocalDate date);

Optional<Classroom> findClassroomById(Long classroomId);

List<Classroom> findClassroomByBuildingIdAndFloorAndCapacityGreaterThanEqual(Long buildingId, Long floor, Integer capacity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;
import java.util.stream.Collectors;

import com.groomiz.billage.classroom.dto.AdminReservationSearchByClassroomCond;
import com.groomiz.billage.classroom.dto.response.AdminClassroomDetailResponse.ReservationDetail;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -33,10 +35,17 @@ public class AdminClassroomService {
@Transactional(readOnly = true)
public AdminClassroomDetailResponse findClassroomByIdAndDate(Long classroomId, LocalDate date) {

Classroom classroom = classroomRepository.findClassroomByIdAndDate(classroomId, date)
.orElseThrow(() -> new ClassroomException(ClassroomErrorCode.CLASSROOM_NOT_FOUND));
Classroom classroom = classroomRepository.findClassroomById(classroomId)
.orElseThrow(() -> new ClassroomException(ClassroomErrorCode.CLASSROOM_NOT_FOUND));

return AdminClassroomDetailResponse.from(classroom, date);
AdminReservationSearchByClassroomCond cond = AdminReservationSearchByClassroomCond.builder()
.classroom(classroom)
.applyDate(date)
.build();

List<ReservationDetail> reservations = reservationRepository.searchPendingAndApprovedReservationByClassroom(cond);

return AdminClassroomDetailResponse.from(classroom, reservations, date);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import com.groomiz.billage.classroom.exception.ClassroomException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
Expand Down Expand Up @@ -131,6 +132,18 @@ public ResponseEntity<ErrorResponse> handleAdminReservationException(Reservation
.body(errorResponse);
}

@ExceptionHandler(ClassroomException.class)
public ResponseEntity<ErrorResponse> handleCLassroomException(ClassroomException ex, HttpServletRequest request) {

log.info("ClassroomException: {}", ex.getMessage());
ErrorReason reason = ex.getErrorReason();
ErrorResponse errorResponse =
new ErrorResponse(ex.getErrorReason(), request.getRequestURL().toString());

return ResponseEntity.status(HttpStatus.valueOf(reason.getStatus()))
.body(errorResponse);
}

@ExceptionHandler(AuthException.class)
public ResponseEntity<ErrorResponse> handleAuthException(AuthException ex, HttpServletRequest request) {
ErrorReason reason = ex.getErrorCode().getErrorReason();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.groomiz.billage.reservation.repository;

import com.groomiz.billage.classroom.dto.response.AdminClassroomDetailResponse.ReservationDetail;
import com.groomiz.billage.classroom.dto.AdminReservationSearchByClassroomCond;
import org.springframework.data.domain.Page;

import com.groomiz.billage.reservation.dto.AdminReservationSearchCond;
import com.groomiz.billage.reservation.dto.ReservationSearchCond;
import com.groomiz.billage.reservation.dto.response.AdminReservationStatusListResponse.ReservationInfo;
import com.groomiz.billage.reservation.dto.response.ReservationStatusListResponse;

import java.util.List;

public interface ReservationRepositoryCustom {

Page<ReservationInfo> searchPendingReservationPageByBuilding(AdminReservationSearchCond adminReservationSearchCond);
Expand All @@ -15,4 +19,6 @@ Page<ReservationInfo> searchRejectedAndCanceledReservationPageByAdmin(
AdminReservationSearchCond adminReservationSearchCond);
Page<ReservationStatusListResponse.ReservationInfo> searchUpcomingReservationPageByRequester(ReservationSearchCond reservationSearchCond);
Page<ReservationStatusListResponse.ReservationInfo> searchPastReservationPageByRequester(ReservationSearchCond reservationSearchCond);

List<ReservationDetail> searchPendingAndApprovedReservationByClassroom(AdminReservationSearchByClassroomCond reservationSearchCond);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.util.Collection;
import java.util.List;

import com.groomiz.billage.classroom.dto.response.AdminClassroomDetailResponse.ReservationDetail;
import com.groomiz.billage.classroom.entity.Classroom;
import com.groomiz.billage.classroom.dto.AdminReservationSearchByClassroomCond;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
Expand Down Expand Up @@ -88,6 +91,33 @@ public Page<ReservationStatusListResponse.ReservationInfo> searchPastReservation
);
}

@Override
public List<ReservationDetail> searchPendingAndApprovedReservationByClassroom(AdminReservationSearchByClassroomCond reservationSearchCond) {
return queryFactory
.select(
Projections.fields(ReservationDetail.class,
reservation.id.as("reservationId"),
reservationStatus.status,
reservation.applyDate,
reservation.startTime,
reservation.endTime,
reservation.headcount,
member.username.as("memberName"),
member.studentNumber
)
).from(reservation)
.join(reservation.reservationStatus, reservationStatus)
.join(reservation.classroom, classroom)
.join(reservationStatus.requester, member)
.where(
statusIn(List.of(ReservationStatusType.APPROVED, ReservationStatusType.PENDING)),
classroomEq(reservationSearchCond.getClassroom()),
applyDateEq(reservationSearchCond.getApplyDate())
)
.fetch();
}


private Page<ReservationInfo> fetchAdminReservationPage(Pageable pageable, BooleanExpression... conditions) {
List<ReservationInfo> content = queryFactory
.select(
Expand Down Expand Up @@ -188,4 +218,13 @@ private BooleanExpression applyDateGoe(LocalDate today) {
private BooleanExpression applyDateLt(LocalDate today) {
return today != null ? reservation.applyDate.lt(today) : null;
}

private BooleanExpression classroomEq(Classroom classroomCond) {
return classroomCond != null ? classroom.eq(classroomCond) : null;
}

private BooleanExpression applyDateEq(LocalDate date) {
return date != null ? reservation.applyDate.eq(date) : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Long reserveClassroom(ClassroomReservationRequest request, String student
try {
fcmService.sendMessage(admin.getStudentNumber(), title, body);
} catch (FirebaseMessagingException e) {
throw new RuntimeException(e);
log.error("FCM 전송 실패");
}
});
return savedReservation.getId();
Expand Down

0 comments on commit 7a17647

Please sign in to comment.