Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

실습 - 페이지 설정 #94

Open
wants to merge 11 commits into
base: movingone
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RoomescapeApplication {
public class RoomEscapeApplication {
public static void main(String[] args) {
SpringApplication.run(RoomescapeApplication.class, args);
SpringApplication.run(RoomEscapeApplication.class, args);
}

}
116 changes: 116 additions & 0 deletions src/main/java/roomescape/controller/ReserveController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package roomescape.controller;

import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import roomescape.domain.Reservation;
import roomescape.domain.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@Controller
public class ReserveController {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reservation 이라는 이름으로 도메인을 바꿔주셨으니,
관련된 곳들도 이름을 바꿔볼까요?
(해당 class 이름 말고도 다른 reserve 들을 확인해주세요!)


private final JdbcTemplate jdbcTemplate;

@Autowired
public ReserveController(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@GetMapping("/")
public String home() {
return "index";
}

@GetMapping("/admin/reservation")
public String adminReservation() {
return "admin/reservation";
}

@GetMapping("/reservation")
public List<Reservation> readReserve() {
String sql = "select r.id as reservation_id," +
"r.name as reservation_name," +
"r.date as reservation_date," +
"t.id as time_id," +
"t.start_at time_start_at" +
" from reservation as r " +
"inner join reservation_time as t" +
" on r.time_id = t.id";
return jdbcTemplate.query(sql,
(rs, rowNum) -> {
long reservationId = rs.getLong("id");
String name = rs.getString("name");
String date = rs.getString("date");
Time time = new Time(rs.getString("time"));
return new Reservation(reservationId, name, date, time);
});
}

@GetMapping("/reservation/{id}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#94 (comment)

여기는 복수형으로 두시면 좋을 것 같아요!
해당 리뷰에서 말한 프론트엔드 / 백엔드는 각각

프론트엔드 : 웹 페이지 (파일)
백엔드 : api (Spring Boot 를 활용하여 만드는 것)

이었습니다 :)

public List<Reservation> findReserve(@PathVariable Long id) {
String sql = "select id, name, date, time from reservation where id = ?";
return jdbcTemplate.query(sql,
(rs, rowNum) -> {
long reservationId = rs.getLong("id");
String name = rs.getString("name");
String date = rs.getString("date");
Time time = new Time(rs.getString("time"));
return new Reservation(reservationId, name, date, time);
}, id);
}

@PostMapping("/reservation")
public void addReserve(@RequestBody Reservation reserve, Time time) {

String sql = "insert into reservation(name, date, time_id) values (?, ?, ?)";
jdbcTemplate.update(sql, reserve.getName(), reserve.getDate(), time.getId());
}

@DeleteMapping("/reservation/{id}")
public int deleteReserve(@PathVariable Long id) {
String sql = "delete from reservation where id = ?";
return jdbcTemplate.update(sql, id);
}

@PostMapping("/times")
public ResponseEntity<Time> addTime(@RequestBody Time time) {
String sql = "insert into reservation_time (start_at) values ?";
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, time.getStartAt());
return ps;
}, keyHolder);

Long id = keyHolder.getKey().longValue();
time.setId(id);

return ResponseEntity.ok().body(time);
}

@GetMapping("/admin/time")
public List<Time> checkTime() {
String sql = "select id, start_at from reservation_time";

return jdbcTemplate.query(sql,
(rs, rowNum) -> {
Time time = new Time(
rs.getString("start_at"));
return time;});
}

@DeleteMapping("/times/{id}")
public ResponseEntity<Void> timeDelete(@PathVariable Long id) {
String sql = "delete from reservation_time where id = ?";
jdbcTemplate.update(sql, id);
return ResponseEntity.ok().build();
}
}
35 changes: 35 additions & 0 deletions src/main/java/roomescape/domain/Reservation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package roomescape.domain;

public class Reservation {
private long id;
private String name;
private String date;
private Time time;

public Reservation(long id, String name, String date, Time time) {
this.id = id;
this.name = name;
this.date = date;
this.time = time;
}

public void setId(long id) {
this.id = id;
}

public long getId() {
return id;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}

public Time getTime() {
return time;
}
}
30 changes: 30 additions & 0 deletions src/main/java/roomescape/domain/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package roomescape.domain;

public class Time {

Long id;
String startAt;

public Time(String startAt) {
this.startAt = startAt;
}

public Time() {
}

public Long getId() {
return id;
}

public String getStartAt() {
return startAt;
}

public void setId(Long id) {
this.id = id;
}

public void setStartAt(String startAt) {
this.startAt = startAt;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:database
20 changes: 20 additions & 0 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
create table reservation_time
(
id BIGINT NOT NULL AUTO_INCREMENT,
start_at VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

create table reservation
(
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
date VARCHAR(255) NOT NULL,
time_id BIGINT,
PRIMARY KEY (id),
FOREIGN KEY (time_id) REFERENCES reservation_time (id)
);


select *
from reservation_time;
Loading