Skip to content

Commit e288315

Browse files
committed
dao 사용
1 parent 944254c commit e288315

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

src/main/java/roomescape/controller/ReservationController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.web.bind.annotation.GetMapping;
99
import org.springframework.web.bind.annotation.PathVariable;
1010
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.PutMapping;
1112
import org.springframework.web.bind.annotation.RequestBody;
1213
import org.springframework.web.bind.annotation.RequestMapping;
1314
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -46,4 +47,10 @@ public void delete(@PathVariable int id) {
4647
reservationService.delete(id);
4748
}
4849

50+
@PutMapping("/{id}")
51+
@ResponseStatus(HttpStatus.OK)
52+
public void update(@PathVariable int id, @RequestBody Reservation reservation) {
53+
Reservation updated = new Reservation(id, reservation.getName(), reservation.getDate(), reservation.getTime());
54+
reservationService.update(updated);
55+
}
4956
}

src/main/java/roomescape/dao/ReservationDAO.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Reservation addReservation(final Reservation reservation) {
2626
reservation.getDate().toString(),
2727
reservation.getTime().toString());
2828

29-
final var fetchSql = "SELECT * FROM reservation ORDER BY id DESC LIMIT 1";
29+
final var fetchSql = "SELECT * FROM reservation ORDER BY id DESC LIMIT 1"; //방금추가한 최근꺼 다시 반환해줌 -> 이걸로 테스트 확인
3030
return jdbcTemplate.queryForObject(fetchSql, reservationRowMapper);
3131
}
3232

@@ -36,14 +36,23 @@ public Optional<Reservation> findByID(final int id) {
3636
return results.stream().findFirst();
3737
}
3838

39+
public List<Reservation> findAll() {
40+
final var query = "SELECT * FROM reservation";
41+
return jdbcTemplate.query(query, reservationRowMapper);
42+
}
43+
3944
public void deleteReservation(final int id) {
4045
final var query = "DELETE FROM reservation WHERE id = ?";
4146
jdbcTemplate.update(query, id);
4247
}
4348

44-
public List<Reservation> findAll() {
45-
final var query = "SELECT * FROM reservation";
46-
return jdbcTemplate.query(query, reservationRowMapper);
49+
public void updateReservation(final Reservation reservation) {
50+
final var query = "UPDATE reservation SET name = ?, date = ?, time = ? WHERE id = ?";
51+
jdbcTemplate.update(query,
52+
reservation.getName(),
53+
reservation.getDate().toString(),
54+
reservation.getTime().toString(),
55+
reservation.getId());
4756
}
4857

4958
private final RowMapper<Reservation> reservationRowMapper = (resultSet, rowNum) -> new Reservation(

src/main/java/roomescape/service/ReservationService.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
import java.util.List;
55
import java.util.concurrent.atomic.AtomicLong;
66
import org.springframework.stereotype.Service;
7+
import roomescape.dao.ReservationDAO;
78
import roomescape.domain.Reservation;
89
import roomescape.exception.InvalidReservationException;
910
import roomescape.exception.NotFoundReservationException;
1011

1112
@Service
1213
public class ReservationService {
1314

14-
private final List<Reservation> reservations = new ArrayList<>();
15+
private final ReservationDAO reservationDAO;
1516
private final AtomicLong index = new AtomicLong(1);
1617

17-
public Reservation add(Reservation reservation) {
18-
Reservation newReservation = new Reservation(
19-
(int) index.getAndIncrement(),
20-
reservation.getName(),
21-
reservation.getDate(),
22-
reservation.getTime()
23-
);
18+
public ReservationService(ReservationDAO reservationDAO) {
19+
this.reservationDAO = reservationDAO;
20+
}
2421

25-
boolean isDuplicate = reservations.stream().anyMatch(r ->
22+
public Reservation add(Reservation reservation) {
23+
boolean isDuplicate = reservationDAO.findAll().stream().anyMatch(r ->
2624
r.getName().equals(reservation.getName()) &&
2725
r.getDate().equals(reservation.getDate()) &&
2826
r.getTime().equals(reservation.getTime())
@@ -31,18 +29,26 @@ public Reservation add(Reservation reservation) {
3129
throw new InvalidReservationException("동일한 예약이 이미 존재합니다.");
3230
}
3331

34-
reservations.add(newReservation);
35-
return newReservation;
32+
return reservationDAO.addReservation(reservation);
3633
}
3734

3835
public List<Reservation> findAll() {
39-
return reservations;
36+
return reservationDAO.findAll();
4037
}
4138

4239
public void delete(int id) {
43-
boolean removed = reservations.removeIf(r -> r.getId() == id);
44-
if (!removed) {
40+
boolean exists = reservationDAO.findByID(id).isPresent();
41+
if (!exists) {
42+
throw new NotFoundReservationException("해당 ID가 없습니다.");
43+
}
44+
reservationDAO.deleteReservation(id);
45+
}
46+
47+
public void update(Reservation reservation) {
48+
boolean exists = reservationDAO.findByID(reservation.getId()).isPresent();
49+
if (!exists) {
4550
throw new NotFoundReservationException("해당 ID가 없습니다.");
4651
}
52+
reservationDAO.updateReservation(reservation);
4753
}
4854
}

src/test/java/roomescape/DAOTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import roomescape.dao.ReservationDAO;
1818
import roomescape.domain.Reservation;
1919

20-
@SpringBootTest
20+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
2121
public class DAOTest {
2222

2323
@Autowired
@@ -43,9 +43,10 @@ void findReservation() {
4343
Reservation reservation = new Reservation(1, "전서희", LocalDate.parse("2026-05-12"), LocalTime.parse("19:00"));
4444
reservationDAO.addReservation(reservation);
4545

46-
Optional<Reservation> found = reservationDAO.findByID(1);
46+
Reservation saved = reservationDAO.addReservation(reservation);
47+
Optional<Reservation> found = reservationDAO.findByID(saved.getId());
4748
assertThat(found).isPresent();
48-
assertThat(found.get()).isEqualTo(reservation);
49+
assertThat(found.get()).isEqualTo(saved);
4950
}
5051

5152
@Test
@@ -58,6 +59,22 @@ void deleteReservation() {
5859
assertThat(result).isEmpty();
5960
}
6061

62+
@Test
63+
void updateReservation() {
64+
LocalDate date = LocalDate.now().plusDays(1);
65+
Reservation original = new Reservation(null, "전서희", date, LocalTime.of(19, 0));
66+
Reservation saved = reservationDAO.addReservation(original);
67+
68+
Reservation updated = new Reservation(saved.getId(), "서희전", date.plusDays(1), LocalTime.of(14, 0));
69+
reservationDAO.updateReservation(updated);
70+
71+
Optional<Reservation> result = reservationDAO.findByID(saved.getId());
72+
assertThat(result).isPresent();
73+
assertThat(result.get().getName()).isEqualTo("서희전");
74+
assertThat(result.get().getDate()).isEqualTo(date.plusDays(1));
75+
assertThat(result.get().getTime()).isEqualTo(LocalTime.of(14, 0));
76+
}
77+
6178
@Test
6279
void 오단계() {
6380
try (Connection connection = jdbcTemplate.getDataSource().getConnection()) {
@@ -71,7 +88,9 @@ void deleteReservation() {
7188

7289
@Test
7390
void 육단계() {
74-
jdbcTemplate.update("INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)", "브라운", "2023-08-05", "15:40");
91+
LocalDate tomorrow = LocalDate.now().plusDays(1);
92+
93+
jdbcTemplate.update("INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)", "브라운", tomorrow.toString(), "15:40");
7594

7695
List<Reservation> reservations = RestAssured.given().log().all()
7796
.when().get("/reservations")

0 commit comments

Comments
 (0)