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

[1-2단계 - 데이터베이스 적용] #98

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/roomescape/controller/PageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package roomescape.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class PageController {
@GetMapping
public String home() {
return "admin/index";
}

@GetMapping("/admin/reservation")
public String reservation() {
return "admin/reservation-legacy";
}
}
35 changes: 35 additions & 0 deletions src/main/java/roomescape/controller/ReservationController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package roomescape.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import roomescape.model.Reservation;
import roomescape.service.ReservationService;

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

@RestController
@RequestMapping("/reservations")
public class ReservationController {
private final ReservationService reservationService;

@Autowired
public ReservationController(ReservationService reservationService) {
this.reservationService = reservationService;
}

@PostMapping
public Long insert(@RequestBody Reservation reservation) {
return reservationService.addReservation(reservation);
}

@GetMapping
public List<Reservation> read() {
return reservationService.lookUpReservation();
}

@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
reservationService.deleteReservation(id);
}
}
35 changes: 35 additions & 0 deletions src/main/java/roomescape/controller/ReservationTimeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package roomescape.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import roomescape.model.ReservationTime;
import roomescape.service.ReservationTimeService;

import java.util.List;

@RestController
@RequestMapping("/times")
public class ReservationTimeController {

private ReservationTimeService reservationTimeService;

@Autowired
public ReservationTimeController(ReservationTimeService reservationTimeService) {
this.reservationTimeService = reservationTimeService;
}

@PostMapping
public ReservationTime insert(@RequestBody String startAt) {
return reservationTimeService.addReservation(startAt);
}

@GetMapping
public List<ReservationTime> read() {
return reservationTimeService.lookUpReservationTime();
}

@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
reservationTimeService.deleteReservation(id);
}
}
33 changes: 33 additions & 0 deletions src/main/java/roomescape/model/Reservation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package roomescape.model;

import java.util.concurrent.atomic.AtomicLong;

public class Reservation {
private Long id;
private String name;
private String date;
private Long timeId;

public Reservation(Long id, String name, String date, Long timeId) {
this.id = id;
this.name = name;
this.date = date;
this.timeId = timeId;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}

public Long getTimeId() {
return timeId;
}
}
24 changes: 24 additions & 0 deletions src/main/java/roomescape/model/ReservationTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package roomescape.model;

public class ReservationTime {
private Long id;
private String startAt;

public ReservationTime(String startAt) {
this(0L, startAt);
}

public ReservationTime(Long id, String startAt) {
this.id = id;
this.startAt = startAt;
}

public Long getId() {
return id;
}

public String getStartAt() {
return startAt;
}

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import roomescape.model.Reservation;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ReservationRepository {
private final JdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert jdbcInsert;
private final RowMapper<Reservation> rowMapper = (resultSet, rowNum) -> new Reservation(
resultSet.getLong("id"),
resultSet.getString("name"),
resultSet.getString("date"),
resultSet.getLong("time_id")
);

@Autowired
public ReservationRepository(SimpleJdbcInsert jdbcInsert, JdbcTemplate jdbcTemplate, DataSource dataSource) {
this.jdbcTemplate = jdbcTemplate;
this.jdbcInsert = new SimpleJdbcInsert(dataSource)
.withTableName("reservation")
.usingGeneratedKeyColumns("id");
}

public Long save(Reservation reservation) {
Map<String, Object> params = new HashMap<>();
params.put("id", reservation.getId());
params.put("name", reservation.getName());
params.put("date", reservation.getDate());
params.put("time_id", reservation.getTimeId());

return jdbcInsert.executeAndReturnKey(params).longValue();
}

public List<Reservation> readAll() {
String sql = "SELECT \n" +
"r.id as reservation_id, \n" +
"r.name as reservation_name, \n" +
"r.date as reservation_date, \n" +
"t.id as time_id, \n" +
"t.start_at as time_start_at \n" +
"FROM reservation as r inner join reservation_time as t on r.time_id = t.id";
return jdbcTemplate.query(sql, rowMapper);
}

public void deleteById(Long id) {
String sql = "DELETE FROM reservation WHERE id = ?";
jdbcTemplate.update(sql, id);
}
}
50 changes: 50 additions & 0 deletions src/main/java/roomescape/repository/ReservationTimeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package roomescape.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import roomescape.model.ReservationTime;

import javax.sql.DataSource;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReservationTimeRepository {

private final JdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert jdbcInsert;

private final RowMapper rowMapper = (resultSet, rowNum) -> new ReservationTime(
resultSet.getLong("id"),
resultSet.getString("start_at")
);

@Autowired
public ReservationTimeRepository(JdbcTemplate jdbcTemplate, SimpleJdbcInsert jdbcInsert, DataSource dataSource) {
this.jdbcTemplate = jdbcTemplate;
this.jdbcInsert = new SimpleJdbcInsert(dataSource)
.withTableName("reservation_time")
.usingGeneratedKeyColumns("id");
}

public ReservationTime save(String startAt) {
Map<String, Object> params = new HashMap<>();
params.put("startAt", startAt);
return new ReservationTime(jdbcInsert.executeAndReturnKey(params).longValue(), startAt);
}

public List<ReservationTime> readAll() {
String sql = "SELECT * FROM reservation_time";
return jdbcTemplate.query(sql, rowMapper);
}

public void deleteById(Long id) {
String sql = "DELETE FROM reservation_time WHERE id = ?";
jdbcTemplate.update(sql, id);
}
}
30 changes: 30 additions & 0 deletions src/main/java/roomescape/service/ReservationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package roomescape.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import roomescape.model.Reservation;
import roomescape.repository.ReservationRepository;

import java.util.List;

@Service
public class ReservationService {
private final ReservationRepository reservationRepository;

@Autowired
public ReservationService(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}

public Long addReservation(Reservation reservation) {
return reservationRepository.save(reservation);
}

public List<Reservation> lookUpReservation() {
return reservationRepository.readAll();
}

public void deleteReservation(Long id) {
reservationRepository.deleteById(id);
}
}
32 changes: 32 additions & 0 deletions src/main/java/roomescape/service/ReservationTimeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package roomescape.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import roomescape.model.ReservationTime;
import roomescape.repository.ReservationRepository;
import roomescape.repository.ReservationTimeRepository;

import java.util.List;

@Service
public class ReservationTimeService {

private final ReservationTimeRepository reservationTimeRepository;

@Autowired
public ReservationTimeService(ReservationTimeRepository reservationTimeRepository) {
this.reservationTimeRepository = reservationTimeRepository;
}

public ReservationTime addReservation(String startAt) {
return reservationTimeRepository.save(startAt);
}

public List<ReservationTime> lookUpReservationTime() {
return reservationTimeRepository.readAll();
}

public void deleteReservation(Long id) {
reservationTimeRepository.deleteById(id);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:database
spring.datasource.driver-class-name=org.h2.Driver
16 changes: 16 additions & 0 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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)
);