Skip to content

Commit

Permalink
fix : 예약 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
urinaner committed Jan 9, 2025
1 parent bc8b790 commit a8c907b
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.example.backend.admin.exception.AdminExceptionType.ALREADY_EXIST_LOGIN_ID;
import static org.example.backend.admin.exception.AdminExceptionType.INVALID_ACCESS_TOKEN;
import static org.example.backend.user.exception.UserExceptionType.NOT_FOUND_USER;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,7 +10,6 @@
import org.example.backend.admin.domain.entity.Admin;
import org.example.backend.admin.exception.AdminException;
import org.example.backend.admin.repository.AdminRepository;
import org.example.backend.user.exception.UserException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.backend.reservation.domain.dto.ReservationReqDto;
import org.example.backend.reservation.domain.dto.ReservationCreateDto;
import org.example.backend.reservation.domain.dto.ReservationDeleteRequest;
import org.example.backend.reservation.domain.dto.ReservationResDto;
import org.example.backend.reservation.service.ReservationService;
import org.example.backend.user.domain.entity.User;
import org.example.backend.user.exception.UserException;
import org.example.backend.user.exception.UserExceptionType;
import org.example.backend.user.repository.UserRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -27,15 +24,12 @@
@RequestMapping("/api/room")
public class ReservationController {
private final ReservationService reservationService;
private final UserRepository userRepository;

@PostMapping("/{roomId}/reservation")
public ResponseEntity<List<ReservationResDto>> createReservation(
@PathVariable(value = "roomId") Long roomId,
@RequestBody @Valid ReservationReqDto reqDto) {
User user = userRepository.findById(1L)
.orElseThrow(() -> new UserException(UserExceptionType.NOT_FOUND_USER));
List<ReservationResDto> resDtos = reservationService.createReservation(roomId, reqDto, user);
@RequestBody @Valid ReservationCreateDto reqDto) {
List<ReservationResDto> resDtos = reservationService.createReservation(roomId, reqDto);
log.debug("Reservation created successfully: {}", resDtos);
return ResponseEntity.ok(resDtos);
}
Expand All @@ -44,7 +38,7 @@ public ResponseEntity<List<ReservationResDto>> createReservation(
@GetMapping("/{roomId}/reservation")
public ResponseEntity<List<ReservationResDto>> getReservationsByRoomAndDate(
@PathVariable(name = "roomId") Long roomId,
@RequestParam(value = "date") String date
@RequestParam(value = "date", required = false) String date
) {
List<ReservationResDto> reservations = reservationService.getReservationsByRoomAndDate(roomId, date);
return ResponseEntity.ok(reservations);
Expand Down Expand Up @@ -75,8 +69,10 @@ public ResponseEntity<ReservationResDto> getReservation(
@DeleteMapping("/{roomId}/reservation/{reservationId}")
public ResponseEntity<Void> deleteReservation(
@PathVariable(value = "roomId") Long roomId,
@PathVariable(value = "reservationId") Long reservationId) {
reservationService.deleteReservation(reservationId);
@PathVariable(value = "reservationId") Long reservationId,
@RequestBody ReservationDeleteRequest request) {
reservationService.deleteReservation(reservationId, request.getPassword());
return ResponseEntity.ok().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.backend.common.domain.BaseEntity;
import org.example.backend.common.utils.TimeParsingUtils;
import org.example.backend.reservation.domain.dto.ReservationReqDto;
import org.example.backend.reservation.domain.dto.ReservationCreateDto;
import org.example.backend.room.domain.Room;
import org.example.backend.user.domain.entity.User;

@Entity
@Getter
Expand Down Expand Up @@ -49,29 +47,37 @@ public class Reservation extends BaseEntity {
@JoinColumn(name = "room_id")
private Room room;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@Column(name = "user_name")
private String username;

@Column(name = "password")
private String password;

public boolean isPasswordMatch(String inputPassword) {
return this.password.equals(inputPassword);
}

@Builder
private Reservation(LocalDateTime startTime, LocalDateTime endTime, ReservationPurpose purpose,
String etc, Room room, User user) {
String etc, Room room, String username, String password) {
this.startTime = startTime;
this.endTime = endTime;
this.purpose = purpose;
this.etc = etc;
this.room = room;
this.user = user;
this.username = username;
this.password = password;
}

public static Reservation of(ReservationReqDto dto, Room room, User user) {
public static Reservation of(ReservationCreateDto dto, Room room) {
return Reservation.builder()
.startTime(TimeParsingUtils.formatterLocalDateTime(String.valueOf(dto.getStartTime())))
.endTime(TimeParsingUtils.formatterLocalDateTime(String.valueOf(dto.getEndTime())))
.startTime(dto.getStartTime())
.endTime(dto.getEndTime())
.purpose(ReservationPurpose.valueOf(dto.getPurpose()))
.etc(dto.getEtc())
.room(room)
.user(user)
.username(dto.getUserName())
.password(dto.getPassword())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ReservationReqDto {
public class ReservationCreateDto {

@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
@NotNull(message = "시작 시간은 필수 입력값입니다.")
private String startTime;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime startTime;

@NotNull(message = "종료 시간은 필수 입력값입니다.")
private String endTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime endTime;

@NotBlank(message = "목적은 필수 입력값입니다.")
@Size(max = 100, message = "목적은 최대 100자까지 입력 가능합니다.")
Expand All @@ -27,18 +28,22 @@ public class ReservationReqDto {
@Size(max = 200, message = "기타 내용은 최대 200자까지 입력 가능합니다.")
private String etc;

private String userName;

private String password;

@Builder
private ReservationReqDto(String startTime, String endTime,
String purpose, String etc) {
private ReservationCreateDto(LocalDateTime startTime, LocalDateTime endTime,
String purpose, String etc) {
this.startTime = startTime;
this.endTime = endTime;
this.purpose = purpose;
this.etc = etc;
}

public static ReservationReqDto of(String startTime, String endTime,
String purpose, String etc) {
return ReservationReqDto.builder()
public static ReservationCreateDto of(LocalDateTime startTime, LocalDateTime endTime,
String purpose, String etc) {
return ReservationCreateDto.builder()
.startTime(startTime)
.endTime(endTime)
.purpose(purpose)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.example.backend.reservation.domain.dto;

import lombok.Data;

@Data
public class ReservationDeleteRequest {
private String password;
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
package org.example.backend.reservation.domain.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.backend.common.utils.TimeParsingUtils;
import org.example.backend.reservation.domain.Reservation;
import org.example.backend.reservation.domain.ReservationPurpose;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ReservationResDto {
private Long id;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private String startTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private String endTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime startTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime endTime;
private ReservationPurpose purpose;
private String etc;
private Long roomId;
private Long userId;
private String userName;
private String password;

@Builder
private ReservationResDto(Long id, String startTime, String endTime,
ReservationPurpose purpose, String etc,
Long roomId, Long userId) {
public ReservationResDto(Long id, LocalDateTime startTime, LocalDateTime endTime,
ReservationPurpose purpose, String etc,
Long roomId, String userName, String password) {
this.id = id;
this.startTime = startTime;
this.endTime = endTime;
this.purpose = purpose;
this.etc = etc;
this.roomId = roomId;
this.userId = userId;
this.userName = userName;
this.password = password;
}

public static ReservationResDto of(Reservation reservation) {
return ReservationResDto.builder()
.id(reservation.getId())
.startTime(TimeParsingUtils.formatterString(reservation.getStartTime()))
.endTime(TimeParsingUtils.formatterString(reservation.getEndTime()))
.purpose(reservation.getPurpose())
.etc(reservation.getEtc())
.roomId(reservation.getRoom().getId())
.userId(reservation.getUser().getId())
.build();
return new ReservationResDto(
reservation.getId(),
reservation.getStartTime(),
reservation.getEndTime(),
reservation.getPurpose(),
reservation.getEtc(),
reservation.getRoom().getId(),
reservation.getUsername(),
reservation.getPassword()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public enum ReservationExceptionType implements BaseExceptionType {

NOT_FOUND_RESERVATION(NOT_FOUND, "예약을 찾을 수 없습니다"),
NOT_VALID_RESERVATION_PASSWORD(BAD_REQUEST, "비밀번호가 일치하지 않습니다."),
EXIST_ALREADY_RESERVATION(BAD_REQUEST, "해당 시간에 이미 승인된 예약이 존재합니다."),
CONFLICT_TIMETABLE(BAD_REQUEST, "해당 시간에 정기 수업이 있어 예약이 불가능합니다."),
INVALID_TIME_ORDER(BAD_REQUEST, "시작 시간은 종료 시간보다 이후일 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@
import static org.example.backend.room.exception.RoomExceptionType.NOT_FOUND_SEMINAR_ROOM;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import lombok.RequiredArgsConstructor;
import org.example.backend.common.utils.TimeParsingUtils;
import org.example.backend.reservation.domain.ReservationPurpose;
import org.example.backend.reservation.exception.ReservationException;
import org.example.backend.room.domain.Room;
import org.example.backend.room.exception.RoomException;
import org.example.backend.room.repository.RoomRepository;
import org.example.backend.reservation.domain.Reservation;
import org.example.backend.reservation.domain.dto.ReservationReqDto;
import org.example.backend.reservation.domain.dto.ReservationCreateDto;
import org.example.backend.reservation.domain.dto.ReservationResDto;
import org.example.backend.reservation.repository.ReservationRepository;
import org.example.backend.user.domain.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
Expand All @@ -32,12 +27,12 @@ public class ReservationService {
private final RoomRepository roomRepository;

@Transactional
public List<ReservationResDto> createReservation(Long seminarRoomId, ReservationReqDto reqDto, User user) {
public List<ReservationResDto> createReservation(Long seminarRoomId, ReservationCreateDto reqDto) {
Room room = getSeminarRoomById(seminarRoomId);

validateReservationOverlap(reqDto, seminarRoomId);

Reservation reservation = Reservation.of(reqDto, room, user);
Reservation reservation = Reservation.of(reqDto, room);
reservationRepository.save(reservation);
return List.of(ReservationResDto.of(reservation));

Expand Down Expand Up @@ -77,16 +72,21 @@ public List<ReservationResDto> getReservationsByRoomAndDate(Long seminarRoomId,


@Transactional
public void deleteReservation(Long id) {
public void deleteReservation(Long id, String password) {
Reservation reservation = getReservationById(id);
System.out.println(reservation.getPassword());
if (!reservation.isPasswordMatch(password)) {
throw new ReservationException(NOT_VALID_RESERVATION_PASSWORD);
}

reservationRepository.delete(reservation);
}

private void validateReservationOverlap(ReservationReqDto reqDto, Long seminarRoomId) {
private void validateReservationOverlap(ReservationCreateDto reqDto, Long seminarRoomId) {
boolean hasReservationConflict = reservationRepository.existsByTimePeriod(
seminarRoomId,
TimeParsingUtils.formatterLocalDateTime(reqDto.getStartTime()),
TimeParsingUtils.formatterLocalDateTime(reqDto.getEndTime())
reqDto.getStartTime(),
reqDto.getEndTime()
);

if (hasReservationConflict) {
Expand Down

0 comments on commit a8c907b

Please sign in to comment.