diff --git a/src/main/java/com/example/pnuunivmiryangcampus/librarySeat/repository/LibrarySeatRepository.java b/src/main/java/com/example/pnuunivmiryangcampus/librarySeat/repository/LibrarySeatRepository.java index faf44bf..97a8dd5 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/librarySeat/repository/LibrarySeatRepository.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/librarySeat/repository/LibrarySeatRepository.java @@ -1,15 +1,17 @@ package com.example.pnuunivmiryangcampus.librarySeat.repository; import com.example.pnuunivmiryangcampus.librarySeat.LibrarySeat; -import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import java.util.List; +import java.util.Optional; + public interface LibrarySeatRepository extends JpaRepository { - // Optional Type 고려 - @Query("select ls from LibrarySeat ls where ls.availability = '사용가능'") - List findLibrarySeatsByAvailability(); + @Query("select ls from LibrarySeat ls where ls.isDeleted = false and ls.availability = '사용가능'") + Optional> findLibrarySeatsByAvailability(); - LibrarySeat findBySeatNumber(int seatNumber); + @Query("select ls from LibrarySeat ls where ls.isDeleted = false and ls.id = :id") + Optional findById(Long id); } \ No newline at end of file diff --git a/src/main/java/com/example/pnuunivmiryangcampus/librarySeat/service/LibrarySeatService.java b/src/main/java/com/example/pnuunivmiryangcampus/librarySeat/service/LibrarySeatService.java index a6f4920..0088171 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/librarySeat/service/LibrarySeatService.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/librarySeat/service/LibrarySeatService.java @@ -17,6 +17,7 @@ public class LibrarySeatService { @Transactional(readOnly = true) public List getAvailableLibrarySeat() { return librarySeatRepository.findLibrarySeatsByAvailability() + .orElseThrow() .stream() .map(LibrarySeatDto::from) .toList(); diff --git a/src/main/java/com/example/pnuunivmiryangcampus/reservation/Reservation.java b/src/main/java/com/example/pnuunivmiryangcampus/reservation/Reservation.java index d4947aa..7a5bc80 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/reservation/Reservation.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/reservation/Reservation.java @@ -6,9 +6,12 @@ import java.time.LocalDateTime; import lombok.Getter; import lombok.ToString; +import org.hibernate.annotations.SQLDelete; + @Getter @ToString(callSuper = true) +@SQLDelete(sql = "update reservation set is_deleted = true, modified_at = current_time, modified_by = user_account_id where id = ?") @Entity public class Reservation extends AuditingFields { diff --git a/src/main/java/com/example/pnuunivmiryangcampus/reservation/controller/ReservationController.java b/src/main/java/com/example/pnuunivmiryangcampus/reservation/controller/ReservationController.java index ae92fb7..c7aedf8 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/reservation/controller/ReservationController.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/reservation/controller/ReservationController.java @@ -32,7 +32,7 @@ public ResponseEntity reservation(@PathVariable Long seatId, @Authenticati ReservationDto reservationDto = ReservationDto.of(findUser.getId(), seatId, startAt, endAt); reservationService.saveReservation(reservationDto); - return ResponseEntity.created(URI.create("/library/reservation/" + findUser.getId())).build(); + return ResponseEntity.created(URI.create("/library/reservation/")).build(); } @GetMapping @@ -51,4 +51,11 @@ public ResponseEntity getReservation(@AuthenticationPrincip public ResponseEntity reservationRenewal(@PathVariable Long reservationId) { return ResponseEntity.ok(reservationService.updateReservationRenewalCount(reservationId)); } + + @DeleteMapping("/{reservationId}") + public ResponseEntity reservation(@PathVariable Long reservationId) { + reservationService.deleteReservation(reservationId); + + return ResponseEntity.noContent().build(); + } } diff --git a/src/main/java/com/example/pnuunivmiryangcampus/reservation/dto/response/ReservationResponse.java b/src/main/java/com/example/pnuunivmiryangcampus/reservation/dto/response/ReservationResponse.java index ea91a36..9113d37 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/reservation/dto/response/ReservationResponse.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/reservation/dto/response/ReservationResponse.java @@ -7,20 +7,20 @@ public record ReservationResponse( Long ReservationId, - Long seatNumber, + Long seatId, LocalDateTime startAt, LocalDateTime endAt, int renewalCount ) implements Serializable { - public static ReservationResponse of(Long ReservationId, Long seatNumber, LocalDateTime startAt, LocalDateTime endAt, int renewalCount) { - return new ReservationResponse(ReservationId, seatNumber, startAt, endAt, renewalCount); + public static ReservationResponse of(Long ReservationId, Long seatId, LocalDateTime startAt, LocalDateTime endAt, int renewalCount) { + return new ReservationResponse(ReservationId, seatId, startAt, endAt, renewalCount); } - public static ReservationResponse from(Reservation entity, Long seatNumber) { + public static ReservationResponse from(Reservation entity, Long seatId) { return new ReservationResponse( entity.getId(), - seatNumber, + seatId, entity.getStartAt(), entity.getEndAt(), entity.getRenewalCount() diff --git a/src/main/java/com/example/pnuunivmiryangcampus/reservation/repository/ReservationRepository.java b/src/main/java/com/example/pnuunivmiryangcampus/reservation/repository/ReservationRepository.java index 4f68f4e..59574c8 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/reservation/repository/ReservationRepository.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/reservation/repository/ReservationRepository.java @@ -2,9 +2,15 @@ import com.example.pnuunivmiryangcampus.reservation.Reservation; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import java.util.Optional; public interface ReservationRepository extends JpaRepository { + + @Query("select r from Reservation r where r.isDeleted = false and r.userAccountId = :userId") Optional findByUserAccountId(Long userId); + + @Query("select r from Reservation r where r.isDeleted = false and r.id = :id") + Optional findById(Long id); } \ No newline at end of file diff --git a/src/main/java/com/example/pnuunivmiryangcampus/reservation/service/ReservationService.java b/src/main/java/com/example/pnuunivmiryangcampus/reservation/service/ReservationService.java index 4f82f7a..37d9ef6 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/reservation/service/ReservationService.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/reservation/service/ReservationService.java @@ -31,9 +31,9 @@ public ReservationResponse getReservationByUserId(Long userId) { Optional reservation = reservationRepository.findByUserAccountId(userId); if (reservation.isPresent()) { - Long reservedSeatNumber = librarySeatRepository.findById(reservation.get().getLibrarySeatId()).orElseThrow().getId(); + Long reservedSeatId = librarySeatRepository.findById(reservation.get().getLibrarySeatId()).orElseThrow().getId(); - return ReservationResponse.from(reservation.get(), reservedSeatNumber); + return ReservationResponse.from(reservation.get(), reservedSeatId); } return null; @@ -54,4 +54,10 @@ private void checkRenewalCount(Reservation reservation) { throw new ReservationLimitExceededException(); } } + + @Transactional + public void deleteReservation(Long reservationId) { + reservationRepository.deleteById(reservationId); + } } + diff --git a/src/main/java/com/example/pnuunivmiryangcampus/userAccount/repository/UserAccountRepository.java b/src/main/java/com/example/pnuunivmiryangcampus/userAccount/repository/UserAccountRepository.java index 231b863..d5ae994 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/userAccount/repository/UserAccountRepository.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/userAccount/repository/UserAccountRepository.java @@ -3,8 +3,10 @@ import com.example.pnuunivmiryangcampus.userAccount.UserAccount; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; public interface UserAccountRepository extends JpaRepository { + @Query("select ua from UserAccount ua where ua.isDeleted = false and ua.sub = :sub") Optional findBySub(String sub); } \ No newline at end of file