Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 예약 취소 요청 구현 #42

Merged
merged 11 commits into from
Mar 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import com.SMWU.CarryUsServer.domain.member.entity.Member;
import com.SMWU.CarryUsServer.domain.member.service.MemberService;
import com.SMWU.CarryUsServer.domain.reservation.controller.request.ReservationCancelRequestDTO;
import com.SMWU.CarryUsServer.domain.reservation.controller.request.ReviewCreateRequestDTO;
import com.SMWU.CarryUsServer.domain.reservation.controller.response.MemberReservationDefaultInfoResponseDTO;
import com.SMWU.CarryUsServer.domain.reservation.controller.response.ReservationIdResponseDTO;
import com.SMWU.CarryUsServer.domain.reservation.controller.response.ReviewIdResponseDTO;
import com.SMWU.CarryUsServer.domain.reservation.service.ReservationCancelService;
import com.SMWU.CarryUsServer.domain.reservation.service.ReservationReviewService;
import com.SMWU.CarryUsServer.global.response.SuccessResponse;
import com.SMWU.CarryUsServer.global.security.AuthMember;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static com.SMWU.CarryUsServer.domain.reservation.exception.ReservationSuccessType.RESERVATION_CANCEL_SUCCESS;
import static com.SMWU.CarryUsServer.domain.reservation.exception.ReservationSuccessType.RESERVATION_MEMBER_DEFAULT_INFO_GET_SUCCESS;
import static com.SMWU.CarryUsServer.domain.reservation.exception.ReviewSuccessType.REVIEW_CREATE_SUCCESS;

Expand All @@ -21,6 +25,7 @@
public class ReservationController {
private final ReservationReviewService reservationReviewService;
private final MemberService memberService;
private final ReservationCancelService reservationCancelService;

@PostMapping("/{reservationId}/review")
public ResponseEntity<SuccessResponse<ReviewIdResponseDTO>> createReview(@PathVariable("reservationId") final Long reservationId, @RequestBody final ReviewCreateRequestDTO requestDTO, @AuthMember final Member member) {
Expand All @@ -33,4 +38,10 @@ public ResponseEntity<SuccessResponse<MemberReservationDefaultInfoResponseDTO>>
final MemberReservationDefaultInfoResponseDTO responseDTO = memberService.getMemberReservationDefaultInfo(member);
return ResponseEntity.ok(SuccessResponse.of(RESERVATION_MEMBER_DEFAULT_INFO_GET_SUCCESS, responseDTO));
}

@PostMapping("")
public ResponseEntity<SuccessResponse<ReservationIdResponseDTO>> cancelReservation(@RequestBody final ReservationCancelRequestDTO request) {
final ReservationIdResponseDTO responseDTO = reservationCancelService.cancelReservation(request);
return ResponseEntity.ok(SuccessResponse.of(RESERVATION_CANCEL_SUCCESS, responseDTO));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.SMWU.CarryUsServer.domain.reservation.controller.request;

public record ReservationCancelRequestDTO(long reservationId, String cancelReason) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.SMWU.CarryUsServer.domain.reservation.controller.response;

public record ReservationIdResponseDTO(long reservationId) {
public static ReservationIdResponseDTO of(long reservationId) {
return new ReservationIdResponseDTO(reservationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ public String getReservationInfo(){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm 예약");
return reservationStartTime.format(formatter);
}

public void cancelReservation(){
this.reservationType = ReservationType.CANCELED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.SMWU.CarryUsServer.domain.reservation.entity;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "RESERVATIONS_CANCEL_REASONS")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class ReservationCancelReason {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long reservationCancelReasonId;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "reservation_id")
private Reservation reservation;

private String cancelReason;

@Builder
public ReservationCancelReason(Reservation reservation, String cancelReason) {
this.reservation = reservation;
this.cancelReason = cancelReason;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

@RequiredArgsConstructor
public enum ReservationSuccessType implements SuccessType {
RESERVATION_MEMBER_DEFAULT_INFO_GET_SUCCESS(HttpStatus.OK, "회원의 기본 예약 정보 조회에 성공하였습니다.");
RESERVATION_MEMBER_DEFAULT_INFO_GET_SUCCESS(HttpStatus.OK, "회원의 기본 예약 정보 조회에 성공하였습니다."),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUCCESS_GET_MEMBER_DEFAULT_RESERVATION
SUCCESS_CANCLE_RESERVATION

이런 변수명은 어떤지,,(사실 취향 차이같아요 ㅋㅋ) 보다보니 궁금한 게 회원 기본 예약 정보 조회와 예약 취소 성공은 같이 상태 구분에 쓰이는 케이스가 아니고 그냥 단지 성공했다는 공통점만 존재하는 것 같은데 왜 enum 클래스로 묶으신 건지 궁금해요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SuccessType을 구현하는 클래스로 따로 만든 이유는 HttpStatus가 달라질 수도 있기 때문입니다!! 같은 성공이더라고 httpstatus가 달라질 수 있거든요! 대부분 200 OK와 201 created로 지정해서 더욱 중복되어 보이는 것 같긴합니다 ㅎㅎ 변수명은 다른 패키지의 SuccessType과 ExceptionType 모두 한번 리팩토링 진행하면서 통일하겠습니다!
image

RESERVATION_CANCEL_SUCCESS(HttpStatus.OK, "예약 취소에 성공하였습니다."),;

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.SMWU.CarryUsServer.domain.reservation.repository;

import com.SMWU.CarryUsServer.domain.reservation.entity.ReservationCancelReason;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ReservationCancelReasonRepository extends JpaRepository<ReservationCancelReason, Long>{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.SMWU.CarryUsServer.domain.reservation.service;

import com.SMWU.CarryUsServer.domain.reservation.controller.request.ReservationCancelRequestDTO;
import com.SMWU.CarryUsServer.domain.reservation.controller.response.ReservationIdResponseDTO;
import com.SMWU.CarryUsServer.domain.reservation.entity.Reservation;
import com.SMWU.CarryUsServer.domain.reservation.entity.ReservationCancelReason;
import com.SMWU.CarryUsServer.domain.reservation.exception.ReservationException;
import com.SMWU.CarryUsServer.domain.reservation.repository.ReservationCancelReasonRepository;
import com.SMWU.CarryUsServer.domain.reservation.repository.ReservationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.SMWU.CarryUsServer.domain.reservation.exception.ReservationExceptionType.NOT_FOUND_RESERVATION;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ReservationCancelService {
private final ReservationCancelReasonRepository reservationCancelReasonRepository;
private final ReservationRepository reservationRepository;

@Transactional
public ReservationIdResponseDTO cancelReservation(final ReservationCancelRequestDTO request) {
final Reservation reservation = reservationRepository.findById(request.reservationId()).orElseThrow(() -> new ReservationException(NOT_FOUND_RESERVATION));
reservation.cancelReservation();
final ReservationCancelReason reservationCancelReason = ReservationCancelReason.builder()
.reservation(reservation)
.cancelReason(request.cancelReason())
.build();
reservationCancelReasonRepository.save(reservationCancelReason);
return ReservationIdResponseDTO.of(reservation.getReservationId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SecurityConfig {

public static final String[] AUTH_WHITELIST = {
"/", "/error",
"/favicon.ico",
"/favicon.ico.",
"/actuator/health", "/check/profile"
};

Expand Down
Loading