Skip to content

Commit

Permalink
kahluaband#69 Feat: 예약 우선권 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
woogieon8on committed Nov 9, 2024
1 parent f48da62 commit f0ea027
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package kahlua.KahluaProject.controller;

import kahlua.KahluaProject.dto.reservation.request.ReservationProceedRequest;
import kahlua.KahluaProject.dto.reservation.request.ReservationRequest;
import kahlua.KahluaProject.dto.reservation.response.ReservationResponse;
import kahlua.KahluaProject.service.ReservationService;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequiredArgsConstructor
public class ReservationController {

private final ReservationService reservationService;

// 예약 시간 선택한 사람에게 우선권 부여
@MessageMapping("/reserve.proceed/{date}")
@SendTo("/topic/public/{date}")
public ReservationResponse proceed(@DestinationVariable String reservationDate,
@Header("simpSessionAttributes") Map<String, Object> simpSessionAttributes,
@Payload ReservationProceedRequest reservationProceedRequest) {

return reservationService.proceed(reservationProceedRequest, reservationDate, simpSessionAttributes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kahlua.KahluaProject.converter;

import kahlua.KahluaProject.domain.reservation.Reservation;
import kahlua.KahluaProject.dto.reservation.request.ReservationRequest;
import kahlua.KahluaProject.dto.reservation.response.ReservationResponse;

public class ReservationConverter {

public static ReservationResponse toReservationResponse(Reservation reservation, String email) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kahlua.KahluaProject.dto.reservation.request;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalTime;

public record ReservationProceedRequest(
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm", timezone = "Asia/Seoul")
LocalTime startTime,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm", timezone = "Asia/Seoul")
LocalTime endTime
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kahlua.KahluaProject.dto.reservation.request;

import com.fasterxml.jackson.annotation.JsonFormat;
import kahlua.KahluaProject.domain.reservation.ReservationType;

import java.time.LocalTime;

public record ReservationRequest(
ReservationType type,
String clubroomUsername,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm", timezone = "Asia/Seoul")
LocalTime startTime,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm", timezone = "Asia/Seoul")
LocalTime endTime
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kahlua.KahluaProject.dto.reservation.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import kahlua.KahluaProject.domain.reservation.ReservationStatus;
import kahlua.KahluaProject.domain.reservation.ReservationType;
import lombok.Builder;

import java.time.LocalDate;
import java.time.LocalTime;

@Builder
public record ReservationResponse(
Long reservationId,
String email,
ReservationType type,
String clubroomUsername,

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
LocalDate reservationDate,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm", timezone = "Asia/Seoul")
LocalTime startTime,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm", timezone = "Asia/Seoul")
LocalTime endTime,

ReservationStatus status
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kahlua.KahluaProject.repository;

import kahlua.KahluaProject.domain.reservation.Reservation;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ReservationRepository extends JpaRepository<Reservation, Long> {
}
52 changes: 52 additions & 0 deletions src/main/java/kahlua/KahluaProject/service/ReservationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package kahlua.KahluaProject.service;

import kahlua.KahluaProject.converter.ReservationConverter;
import kahlua.KahluaProject.domain.reservation.Reservation;
import kahlua.KahluaProject.domain.reservation.ReservationStatus;
import kahlua.KahluaProject.dto.reservation.request.ReservationProceedRequest;
import kahlua.KahluaProject.dto.reservation.request.ReservationRequest;
import kahlua.KahluaProject.dto.reservation.response.ReservationResponse;
import kahlua.KahluaProject.repository.ReservationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ReservationService {

private final ReservationRepository reservationRepository;

public ReservationResponse proceed(ReservationProceedRequest reservationProceedRequest, String reservationDate, Map<String, Object> header) {

String email = getValueFromHeader(header, "email");

return ReservationResponse.builder()
.email(email)
.reservationDate(toLocalDate(reservationDate))
.startTime(reservationProceedRequest.startTime())
.endTime(reservationProceedRequest.endTime())
.status(ReservationStatus.PROCEEDING)
.build();
}

// String to LocalDateTime
private LocalDate toLocalDate(String date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return LocalDate.parse(date, formatter);
}

private ReservationResponse toReservationResponse(Reservation reservation, Map<String, Object> header) {
String email = getValueFromHeader(header, "email");

return ReservationConverter.toReservationResponse(reservation, email);
}

private String getValueFromHeader(Map<String, Object> header, String key) {
return (String)header.get(key);
}

}

0 comments on commit f0ea027

Please sign in to comment.