Skip to content

Commit

Permalink
Merge pull request #82 from c0smosaur/fix/#81-available-space-api
Browse files Browse the repository at this point in the history
[FIX] Fix/#81 available space api
  • Loading branch information
c0smosaur authored Jun 7, 2024
2 parents c9c1593 + d22cee2 commit 4c3218f
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


import com.core.linkup.office.entity.SeatSpace;
import com.core.linkup.office.entity.enums.SeatSpaceType;
import com.core.linkup.reservation.reservation.entity.Reservation;
import com.querydsl.core.Tuple;

import java.time.LocalDate;
Expand Down Expand Up @@ -32,4 +34,8 @@ public interface ReservationRepositoryCustom {

// 잔여 좌석 조회
List<SeatSpace> findAllSeatSpacesByOfficeIdAndType(Long officeId, String type, LocalDateTime start, LocalDateTime end);

// 잔여 공간 조회
List<Reservation> findAllReservationsBySeatIdAndDateAndType(Long seatId, LocalDateTime startDate, SeatSpaceType type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import com.core.linkup.reservation.membership.company.entity.QCompanyMembership;
import com.core.linkup.reservation.membership.individual.entity.QIndividualMembership;
import com.core.linkup.reservation.reservation.entity.QReservation;
import com.core.linkup.reservation.reservation.entity.Reservation;
import com.core.linkup.reservation.reservation.entity.enums.ReservationStatus;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

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

@Repository
Expand Down Expand Up @@ -179,5 +182,25 @@ public List<SeatSpace> findAllSeatSpacesByOfficeIdAndType(
.fetch();
}

// 잔여 공간 조회
@Override
public List<Reservation> findAllReservationsBySeatIdAndDateAndType(
Long seatId, LocalDateTime startDate, SeatSpaceType type) {

QReservation qReservation = QReservation.reservation;
QSeatSpace qSeatSpace = QSeatSpace.seatSpace;

BooleanExpression datePredicate = qReservation.startDate.between(
startDate.toLocalDate().atStartOfDay(),
startDate.toLocalDate().atTime(LocalTime.MAX)
);

return jpaQueryFactory.selectFrom(qReservation)
.join(qSeatSpace).on(qReservation.seatId.eq(qSeatSpace.id))
.where(qReservation.seatId.eq(seatId)
.and(datePredicate)
.and(qSeatSpace.type.eq(type)))
.fetch();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Builder
@Getter
public class SeatSpaceResponse {
private Long id;
private String type;
private String code;
private boolean isAvailable;
private List<String> am;
private List<String> pm;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
Expand Down Expand Up @@ -168,21 +171,82 @@ public List<SeatSpaceResponse> getAvailableSeatSpaces(

private List<SeatSpaceResponse> getSeatSpacesFromDate(
Long officeId, String type, LocalDateTime startDate, LocalDateTime endDate) {
List<SeatSpace> allSeatSpaces =
seatSpaceRepository.findAllByOfficeIdAndType(officeId, SeatSpaceType.fromKor(type));
List<SeatSpace> availableSeatSpaces =
reservationRepository.findAllSeatSpacesByOfficeIdAndType(
officeId, String.valueOf(SeatSpaceType.fromKor(type)), startDate, endDate);

// 전체 좌석 리스트와 잔여 좌석 리스트
// 잔여 좌석 리스트에 있으면 해당 좌석 true
return allSeatSpaces.stream().map(
seatSpace -> SeatSpaceResponse.builder()

if (type.equals(SeatSpaceType.CONF4.getTypeName())
|| type.equals(SeatSpaceType.CONF8.getTypeName())
|| type.equals(SeatSpaceType.CONFERENCE_ROOM.getTypeName())
|| type.equals(SeatSpaceType.STUDIO.getTypeName())){

List<SeatSpaceResponse> responses = new ArrayList<>();
// 해당 건물의 공간 조회
List<SeatSpace> allSeatSpaces =
seatSpaceRepository.findAllByOfficeIdAndType(officeId, SeatSpaceType.fromKor(type));

for (SeatSpace seatSpace : allSeatSpaces){
// 한 공간에 대한 특정 날짜의 예약들
List<Reservation> reservations =
reservationRepository.findAllReservationsBySeatIdAndDateAndType(
seatSpace.getId(), startDate, SeatSpaceType.fromKor(type));

List<LocalTime> reservedTimes = reservations.stream()
.flatMap(reservation -> {
LocalTime startTime = reservation.getStartDate().toLocalTime();
LocalTime endTime = reservation.getEndDate().toLocalTime();
return Stream.iterate(startTime, time -> time.plusMinutes(30))
.limit(Duration.between(startTime, endTime).toMinutes() / 30);
}).toList();

List<String> am = new ArrayList<>();
List<String> pm = new ArrayList<>();
LocalTime time = LocalTime.of(8,0);

while (time.isBefore(LocalTime.of(21,30))){
if (!reservedTimes.contains(time)){
if (time.isBefore(LocalTime.NOON)){
am.add(time.toString());
} else {
pm.add(time.toString());
}
}
time = time.plusMinutes(30);

}
if (!reservedTimes.contains(LocalTime.of(21,30))){
pm.add(LocalTime.of(21,30).toString());
}

SeatSpaceResponse response = SeatSpaceResponse.builder()
.id(seatSpace.getId())
.code(seatSpace.getCode())
.type(seatSpace.getType().getTypeName())
.isAvailable(availableSeatSpaces.contains(seatSpace))
.build()
).toList();
.code(seatSpace.getCode())
.isAvailable(true)
.am(am)
.pm(pm)
.build();

responses.add(response);
}

return responses;

} else {
// 좌석
List<SeatSpace> allSeatSpaces =
seatSpaceRepository.findAllByOfficeIdAndType(officeId, SeatSpaceType.fromKor(type));
List<SeatSpace> availableSeatSpaces =
reservationRepository.findAllSeatSpacesByOfficeIdAndType(
officeId, String.valueOf(SeatSpaceType.fromKor(type)), startDate, endDate);

// 전체 좌석 리스트와 잔여 좌석 리스트
// 잔여 좌석 리스트에 있으면 해당 좌석 true
return allSeatSpaces.stream().map(
seatSpace -> SeatSpaceResponse.builder()
.id(seatSpace.getId())
.code(seatSpace.getCode())
.type(seatSpace.getType().getTypeName())
.isAvailable(availableSeatSpaces.contains(seatSpace))
.build()
).toList();
}
}
}

0 comments on commit 4c3218f

Please sign in to comment.