Skip to content

Commit d0cda21

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent 2da143c commit d0cda21

18 files changed

+392
-213
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package roomescape.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@Controller
7+
public class PageController {
8+
9+
@GetMapping("/")
10+
public String home() {
11+
return "home";
12+
}
13+
14+
@GetMapping("/reservation")
15+
public String reservationPage() {
16+
return "new-reservation";
17+
}
18+
19+
@GetMapping("/time")
20+
public String timePage() {
21+
return "time";
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,67 @@
11
package roomescape.controller;
22

33
import org.springframework.http.ResponseEntity;
4-
import org.springframework.stereotype.Controller;
54
import org.springframework.web.bind.annotation.DeleteMapping;
65
import org.springframework.web.bind.annotation.GetMapping;
76
import org.springframework.web.bind.annotation.PathVariable;
87
import org.springframework.web.bind.annotation.PostMapping;
98
import org.springframework.web.bind.annotation.RequestBody;
10-
import org.springframework.web.bind.annotation.ResponseBody;
9+
import org.springframework.web.bind.annotation.RestController;
1110
import roomescape.dto.ReservationRequest;
1211
import roomescape.dto.ReservationResponse;
12+
import roomescape.dto.TimeRequest;
13+
import roomescape.dto.TimeResponse;
1314
import roomescape.service.ReservationService;
1415

1516
import java.net.URI;
1617
import java.util.List;
1718

18-
@Controller
19-
public class RoomescapeController {
19+
@RestController
20+
public class ReservationController {
2021

2122
private final ReservationService reservationService;
2223

23-
public RoomescapeController(ReservationService reservationService) {
24+
public ReservationController(ReservationService reservationService) {
2425
this.reservationService = reservationService;
2526
}
2627

27-
@GetMapping("/")
28-
public String home() {
29-
return "home";
30-
}
31-
32-
@GetMapping("/reservation")
33-
public String reservationPage() {
34-
return "reservation";
35-
}
36-
3728
@GetMapping("/reservations")
38-
@ResponseBody
3929
public ResponseEntity<List<ReservationResponse>> getReservations() {
40-
List<ReservationResponse> reservations = reservationService.findAll();
30+
List<ReservationResponse> reservations = reservationService.findAllReservations();
4131
return ResponseEntity.ok(reservations);
4232
}
4333

4434
@PostMapping("/reservations")
45-
@ResponseBody
4635
public ResponseEntity<ReservationResponse> createReservation(@RequestBody ReservationRequest request) {
47-
ReservationResponse reservation = reservationService.create(request);
36+
ReservationResponse reservation = reservationService.createReservation(request);
4837
return ResponseEntity
4938
.created(URI.create("/reservations/" + reservation.id()))
5039
.body(reservation);
5140
}
5241

5342
@DeleteMapping("/reservations/{id}")
54-
@ResponseBody
5543
public ResponseEntity<Void> deleteReservation(@PathVariable Long id) {
56-
reservationService.delete(id);
44+
reservationService.deleteReservation(id);
45+
return ResponseEntity.noContent().build();
46+
}
47+
48+
@PostMapping("/times")
49+
public ResponseEntity<TimeResponse> createTime(@RequestBody TimeRequest request) {
50+
TimeResponse time = reservationService.createTime(request);
51+
return ResponseEntity
52+
.created(URI.create("/times/" + time.id()))
53+
.body(time);
54+
}
55+
56+
@GetMapping("/times")
57+
public ResponseEntity<List<TimeResponse>> getTimes() {
58+
List<TimeResponse> times = reservationService.findAllTimes();
59+
return ResponseEntity.ok(times);
60+
}
61+
62+
@DeleteMapping("/times/{id}")
63+
public ResponseEntity<Void> deleteTime(@PathVariable Long id) {
64+
reservationService.deleteTime(id);
5765
return ResponseEntity.noContent().build();
5866
}
5967
}

src/main/java/roomescape/domain/InMemoryReservationRepository.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,60 @@
11
package roomescape.domain;
22

3+
import roomescape.exception.ReservationException;
4+
35
import java.time.LocalDate;
4-
import java.time.LocalTime;
56

67
public class Reservation {
78
private final Long id;
89
private final String name;
910
private final LocalDate date;
10-
private final LocalTime time;
11+
private final Time time;
1112

12-
private Reservation(Long id, String name, LocalDate date, LocalTime time) {
13+
private Reservation(Long id, String name, LocalDate date, Time time) {
1314
validate(name, date, time);
1415
this.id = id;
1516
this.name = name;
1617
this.date = date;
1718
this.time = time;
1819
}
1920

20-
public static Reservation of(Long id, String name, LocalDate date, LocalTime time) {
21-
return new Reservation(id, name, date, time);
22-
}
23-
24-
public static Reservation create(String name, LocalDate date, LocalTime time) {
21+
public static Reservation create(String name, LocalDate date, Time time) {
2522
return new Reservation(null, name, date, time);
2623
}
2724

28-
public Long getId() {
29-
return id;
30-
}
31-
32-
public String getName() {
33-
return name;
34-
}
35-
36-
public LocalDate getDate() {
37-
return date;
38-
}
39-
40-
public LocalTime getTime() {
41-
return time;
25+
public static Reservation of(Long id, String name, LocalDate date, Time time) {
26+
return new Reservation(id, name, date, time);
4227
}
4328

44-
private void validate(String name, LocalDate date, LocalTime time) {
29+
private void validate(String name, LocalDate date, Time time) {
4530
validateName(name);
4631
validateDate(date);
4732
validateTime(time);
4833
}
4934

5035
private void validateName(String name) {
5136
if (name == null || name.isBlank()) {
52-
throw new IllegalArgumentException("[ERROR] 이름은 필수 입력 항목입니다.");
37+
throw new ReservationException("[ERROR] 예약자 이름을 입력해주세요.");
5338
}
5439
}
5540

5641
private void validateDate(LocalDate date) {
5742
if (date == null) {
58-
throw new IllegalArgumentException("[ERROR] 날짜는 필수 입력 항목입니다.");
43+
throw new ReservationException("[ERROR] 예약 날짜를 선택해주세요.");
44+
}
45+
if (date.isBefore(LocalDate.now())) {
46+
throw new ReservationException("[ERROR] 지난 날짜로는 예약할 수 없습니다. 오늘 혹은 이후 날짜를 선택해주세요.");
5947
}
6048
}
6149

62-
private void validateTime(LocalTime time) {
50+
private void validateTime(Time time) {
6351
if (time == null) {
64-
throw new IllegalArgumentException("[ERROR] 시간은 필수 입력 항목입니다.");
52+
throw new ReservationException("[ERROR] 예약 시간을 선택해주세요.");
6553
}
6654
}
67-
}
6855

56+
public Long getId() { return id; }
57+
public String getName() { return name; }
58+
public LocalDate getDate() { return date; }
59+
public Time getTime() { return time; }
60+
}

src/main/java/roomescape/domain/JdbcTemplateReservationRepository.java renamed to src/main/java/roomescape/domain/ReservationRepository.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,51 @@
99
import java.sql.Date;
1010
import java.sql.PreparedStatement;
1111
import java.sql.Statement;
12-
import java.sql.Time;
12+
import java.time.LocalDate;
1313
import java.util.List;
1414

1515
@Repository
16-
public class JdbcTemplateReservationRepository {
16+
public class ReservationRepository {
1717

1818
private final JdbcTemplate jdbcTemplate;
19-
private final RowMapper<Reservation> reservationRowMapper = (rs, rowNum) -> Reservation.of(
20-
rs.getLong("id"),
21-
rs.getString("name"),
22-
rs.getDate("date").toLocalDate(),
23-
rs.getTime("time").toLocalTime()
24-
);
25-
26-
public JdbcTemplateReservationRepository(JdbcTemplate jdbcTemplate) {
19+
20+
private final RowMapper<Reservation> reservationRowMapper = (rs, rowNum) -> {
21+
Time time = Time.of(
22+
rs.getLong("time_id"),
23+
rs.getTime("time_value").toLocalTime()
24+
);
25+
return Reservation.of(
26+
rs.getLong("reservation_id"),
27+
rs.getString("name"),
28+
rs.getDate("date").toLocalDate(),
29+
time
30+
);
31+
};
32+
33+
public ReservationRepository(JdbcTemplate jdbcTemplate) {
2734
this.jdbcTemplate = jdbcTemplate;
2835
}
2936

37+
public boolean existsByDateAndTimeId(LocalDate date, Long timeId) {
38+
String sql = "SELECT EXISTS(SELECT 1 FROM reservation WHERE date = ? AND time_id = ?)";
39+
return jdbcTemplate.queryForObject(sql, Boolean.class, date, timeId);
40+
}
41+
3042
public List<Reservation> findAll() {
31-
String sql = "SELECT * FROM reservation";
43+
String sql = "SELECT r.id as reservation_id, r.name, r.date, t.id as time_id, t.time as time_value " +
44+
"FROM reservation as r inner join time as t on r.time_id = t.id";
3245
return jdbcTemplate.query(sql, reservationRowMapper);
3346
}
3447

3548
public Reservation save(Reservation reservation) {
36-
String sql = "INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)";
49+
String sql = "INSERT INTO reservation (name, date, time_id) VALUES (?, ?, ?)";
3750
KeyHolder keyHolder = new GeneratedKeyHolder();
3851

3952
jdbcTemplate.update(connection -> {
4053
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
4154
ps.setString(1, reservation.getName());
4255
ps.setDate(2, Date.valueOf(reservation.getDate()));
43-
ps.setTime(3, Time.valueOf(reservation.getTime()));
56+
ps.setLong(3, reservation.getTime().getId());
4457
return ps;
4558
}, keyHolder);
4659

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package roomescape.domain;
2+
3+
import java.time.LocalTime;
4+
5+
public class Time {
6+
private final Long id;
7+
private final LocalTime time;
8+
9+
private Time(Long id, LocalTime time) {
10+
validate(time);
11+
this.id = id;
12+
this.time = time;
13+
}
14+
15+
public static Time create(LocalTime time) {
16+
return new Time(null, time);
17+
}
18+
19+
public static Time of(Long id, LocalTime time) {
20+
return new Time(id, time);
21+
}
22+
23+
private void validate(LocalTime time) {
24+
if (time == null) {
25+
throw new IllegalArgumentException("[ERROR] 시간 정보는 비어있을 수 없습니다.");
26+
}
27+
}
28+
29+
public Long getId() { return id; }
30+
31+
public LocalTime getTime() { return time; }
32+
}

0 commit comments

Comments
 (0)