Skip to content

Commit

Permalink
Merge pull request #43 from ki-met-hoon/feature/#42-deleteResevation
Browse files Browse the repository at this point in the history
도서관 퇴실 서비스 구현
  • Loading branch information
ki-met-hoon authored Mar 24, 2024
2 parents 63be379 + 588a29a commit 4fe6895
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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<LibrarySeat, Long> {

// Optional Type 고려
@Query("select ls from LibrarySeat ls where ls.availability = '사용가능'")
List<LibrarySeat> findLibrarySeatsByAvailability();
@Query("select ls from LibrarySeat ls where ls.isDeleted = false and ls.availability = '사용가능'")
Optional<List<LibrarySeat>> findLibrarySeatsByAvailability();

LibrarySeat findBySeatNumber(int seatNumber);
@Query("select ls from LibrarySeat ls where ls.isDeleted = false and ls.id = :id")
Optional<LibrarySeat> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class LibrarySeatService {
@Transactional(readOnly = true)
public List<LibrarySeatDto> getAvailableLibrarySeat() {
return librarySeatRepository.findLibrarySeatsByAvailability()
.orElseThrow()
.stream()
.map(LibrarySeatDto::from)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ResponseEntity<Void> 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
Expand All @@ -51,4 +51,11 @@ public ResponseEntity<ReservationResponse> getReservation(@AuthenticationPrincip
public ResponseEntity<ReservationRenewalResponse> reservationRenewal(@PathVariable Long reservationId) {
return ResponseEntity.ok(reservationService.updateReservationRenewalCount(reservationId));
}

@DeleteMapping("/{reservationId}")
public ResponseEntity<Void> reservation(@PathVariable Long reservationId) {
reservationService.deleteReservation(reservationId);

return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Reservation, Long> {

@Query("select r from Reservation r where r.isDeleted = false and r.userAccountId = :userId")
Optional<Reservation> findByUserAccountId(Long userId);

@Query("select r from Reservation r where r.isDeleted = false and r.id = :id")
Optional<Reservation> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public ReservationResponse getReservationByUserId(Long userId) {
Optional<Reservation> 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;
Expand All @@ -54,4 +54,10 @@ private void checkRenewalCount(Reservation reservation) {
throw new ReservationLimitExceededException();
}
}

@Transactional
public void deleteReservation(Long reservationId) {
reservationRepository.deleteById(reservationId);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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<UserAccount, Long> {

@Query("select ua from UserAccount ua where ua.isDeleted = false and ua.sub = :sub")
Optional<UserAccount> findBySub(String sub);
}

0 comments on commit 4fe6895

Please sign in to comment.