Skip to content

Commit 6d1f52c

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent 13c25ff commit 6d1f52c

17 files changed

+509
-9
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ repositories {
1414

1515
dependencies {
1616
implementation 'org.springframework.boot:spring-boot-starter'
17+
implementation 'org.springframework.boot:spring-boot-starter-web'
18+
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
1719
testImplementation 'org.springframework.boot:spring-boot-starter-test'
1820
testImplementation 'io.rest-assured:rest-assured:5.3.1'
21+
implementation 'org.springframework.boot:spring-boot-starter-jdbc' // Jdbc 의존성
22+
runtimeOnly 'com.h2database:h2' // 메모리 기반의 H2 데이터베이스
1923
}
2024

2125
test {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package roomescape.controller;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.jdbc.core.JdbcTemplate;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import roomescape.hello.Customer;
10+
11+
import java.util.List;
12+
13+
@RestController
14+
public class CustomerController {
15+
private JdbcTemplate jdbcTemplate;
16+
17+
public CustomerController(JdbcTemplate jdbcTemplate) {
18+
this.jdbcTemplate = jdbcTemplate;
19+
}
20+
21+
@PostMapping("/customers")
22+
public ResponseEntity<Void> save(@RequestBody Customer customer) {
23+
String sql = "INSERT INTO customers(first_name, last_name) VALUES (?,?)";
24+
jdbcTemplate.update(sql, customer.getFirstName(), customer.getLastName());
25+
return ResponseEntity.ok().build();
26+
}
27+
28+
@GetMapping("/customers") // -> customers 라는 파일(경로)을 만든 적이 없는데 코드가 돌아가긴 함..
29+
public ResponseEntity<List<Customer>> list() {
30+
String sql = "select id, first_name, last_name from customers";
31+
List<Customer> customers = jdbcTemplate.query(
32+
sql, (resultSet, rowNum) -> {
33+
Customer customer = new Customer(
34+
resultSet.getLong("id"),
35+
resultSet.getString("first_name"),
36+
resultSet.getString("last_name")
37+
);
38+
return customer;
39+
});
40+
return ResponseEntity.ok().body(customers);
41+
}
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 HomeController {
8+
9+
@GetMapping("/home")
10+
public String home() {
11+
return "home";
12+
}
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package roomescape.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.*;
9+
import roomescape.model.Reservation;
10+
import roomescape.model.ReservationRequest;
11+
import roomescape.model.ReservationService;
12+
13+
import java.util.List;
14+
15+
@Controller
16+
public class ReservationController {
17+
18+
private ReservationService reservationService;
19+
20+
public ReservationController(ReservationService reservationService) {
21+
this.reservationService = reservationService;
22+
}
23+
24+
@Autowired
25+
private JdbcTemplate jdbcTemplate;
26+
27+
@GetMapping("/reservation")
28+
public String reservationPage() {
29+
return "reservation";
30+
}
31+
32+
@GetMapping("/reservations")
33+
@ResponseBody
34+
public List<Reservation> getReservations() {
35+
return reservationService.getAllReservations();
36+
}
37+
38+
@PostMapping("/reservations")
39+
public ResponseEntity<Reservation> addReservation(@RequestBody ReservationRequest reservationRequest) {
40+
Reservation reservation = reservationService.addReservation(reservationRequest);
41+
return ResponseEntity.status(HttpStatus.CREATED) // status: 201
42+
.header("Location", "/reservations/" + reservation.getId())
43+
.body(reservation);
44+
}
45+
46+
@DeleteMapping("/reservations/{id}")
47+
public ResponseEntity<Void> deleteReservation(@PathVariable Long id) {
48+
reservationService.deleteReservation(id);
49+
return ResponseEntity.noContent().build();
50+
}
51+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package roomescape.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.ControllerAdvice;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
8+
@ControllerAdvice
9+
public class GlobalExceptionHandler {
10+
11+
@ExceptionHandler({NotFoundReservationException.class, InvalidReservationParameterException.class, IllegalArgumentException.class})
12+
public ResponseEntity<String> handleIllegalArgumentException(RuntimeException exception) {
13+
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
14+
.body(exception.getMessage());
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package roomescape.exception;
2+
3+
public class InvalidReservationParameterException extends RuntimeException {
4+
public InvalidReservationParameterException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package roomescape.exception;
2+
3+
public class NotFoundReservationException extends RuntimeException {
4+
public NotFoundReservationException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package roomescape.hello;
2+
3+
public class Customer {
4+
private long id;
5+
private String firstName;
6+
private String lastName;
7+
8+
public Customer(long id, String firstName, String lastName) {
9+
this.id = id;
10+
this.firstName = firstName;
11+
this.lastName = lastName;
12+
}
13+
14+
public long getId() {
15+
return id;
16+
}
17+
18+
public String getFirstName() {
19+
return firstName;
20+
}
21+
22+
public String getLastName() {
23+
return lastName;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package roomescape.hello;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.CommandLineRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.jdbc.core.JdbcTemplate;
8+
9+
@SpringBootApplication
10+
public class DemoApplication implements CommandLineRunner {
11+
12+
public static void main(String args[]) {
13+
SpringApplication.run(DemoApplication.class, args);
14+
}
15+
16+
@Autowired
17+
JdbcTemplate jdbcTemplate;
18+
19+
@Override
20+
public void run(String... strings) throws Exception {
21+
22+
jdbcTemplate.execute("DROP TABLE customers IF EXISTS");
23+
jdbcTemplate.execute("CREATE TABLE customers(id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package roomescape.model;
2+
3+
public class Reservation {
4+
5+
private Long id;
6+
private String name;
7+
private String date;
8+
private String time;
9+
10+
public Reservation(String name, String date, String time) {
11+
this.name = name;
12+
this.date = date;
13+
this.time = time;
14+
}
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public String getDate() {
29+
return date;
30+
}
31+
32+
public String getTime() {
33+
return time;
34+
}
35+
}

0 commit comments

Comments
 (0)