Skip to content

Commit

Permalink
<FIX> 예외처리 위치 변경에 따른 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
goldm0ng committed Dec 6, 2024
1 parent fb00e38 commit f29f0c6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import roomescape.reservation.persistence.ReservationRepository;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -24,8 +23,8 @@ public List<Reservation> checkReservations() {
return repository.findAll();
}

public Optional<Reservation> deleteReservation(Long reservationId) {
return repository.delete(reservationId);
public void deleteReservation(Long reservationId) {
repository.delete(reservationId);
}

private Reservation convertToEntity(ReservationDto reservationDto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import roomescape.reservation.domain.Reservation;
import roomescape.reservation.presentation.exception.NotFoundReservationException;

import java.sql.PreparedStatement;
import java.util.List;
import java.util.Optional;

@Repository
@RequiredArgsConstructor
Expand All @@ -38,14 +38,13 @@ public Reservation save(Reservation reservation) {
}

@Override
public Optional<Reservation> findById(Long reservationId) {
public Reservation findById(Long reservationId) {
String sql = "select id, name, date, time from reservation where id = ?";

try {
Reservation reservation = jdbcTemplate.queryForObject(sql, reservationMapper(), reservationId);
return Optional.of(reservation);
return jdbcTemplate.queryForObject(sql, reservationMapper(), reservationId);
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
throw new NotFoundReservationException();
}
}

Expand All @@ -57,16 +56,12 @@ public List<Reservation> findAll() {
}

@Override
public Optional<Reservation> delete(Long reservationId) {
Optional<Reservation> reservation = this.findById(reservationId);
if (reservation.isEmpty()) {
return Optional.empty();
}
public void delete(Long reservationId) {
Reservation deletedReservation = this.findById(reservationId);

String sql = "delete from reservation where id = ?";
jdbcTemplate.update(sql, reservationId);
jdbcTemplate.update(sql, deletedReservation.getId());

return reservation;
}

private RowMapper<Reservation> reservationMapper() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package roomescape.reservation.persistence;

import roomescape.reservation.domain.Reservation;
import roomescape.reservation.presentation.exception.NotFoundReservationException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -25,8 +25,12 @@ public Reservation save(Reservation reservation) {
}

@Override
public Optional<Reservation> findById(Long reservationId) {
return Optional.ofNullable(reservationStore.get(reservationId));
public Reservation findById(Long reservationId) {
Reservation reservation = reservationStore.get(reservationId);
if (reservation == null) {
throw new NotFoundReservationException();
}
return reservation;
}

@Override
Expand All @@ -35,9 +39,8 @@ public List<Reservation> findAll() {
}

@Override
public Optional<Reservation> delete(Long reservationId) {
Optional<Reservation> reservation = findById(reservationId);

return Optional.ofNullable(reservationStore.remove(reservationId));
public void delete(Long reservationId) {
Reservation deletedReservation = this.findById(reservationId);
reservationStore.remove(deletedReservation.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import roomescape.reservation.domain.Reservation;

import java.util.List;
import java.util.Optional;

public interface ReservationRepository {

Reservation save(Reservation reservation);

Optional<Reservation> findById(Long reservationId);
Reservation findById(Long reservationId);

List<Reservation> findAll();

public Optional<Reservation> delete(Long reservationId);
void delete(Long reservationId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import org.springframework.web.bind.annotation.*;
import roomescape.reservation.domain.Reservation;
import roomescape.reservation.presentation.dto.ReservationDto;
import roomescape.reservation.presentation.exception.NotFoundReservationException;
import roomescape.reservation.business.ReservationService;

import java.net.URI;
import java.util.List;
import java.util.Optional;

@RestController
@RequiredArgsConstructor
Expand All @@ -32,9 +30,7 @@ public ResponseEntity<Reservation> createReservation(@Valid @RequestBody Reserva

@DeleteMapping("/reservations/{reservationId}")
public ResponseEntity<Void> deleteReservation(@PathVariable Long reservationId) {
Reservation deletedReservation = reservationService.deleteReservation(reservationId)
.orElseThrow(NotFoundReservationException::new);

reservationService.deleteReservation(reservationId);
return ResponseEntity.noContent().build();
}
}

0 comments on commit f29f0c6

Please sign in to comment.