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-3 : 시간 관리 기능 추가] #101

Open
wants to merge 19 commits into
base: heoseungjun
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
fe7a701
fix: theme 추가로 인한 링크 추가 및 기존 링크 수정
boorownie Jun 6, 2024
97663d9
feat: theme 추가로 인한 html 및 js 파일 생성
boorownie Jun 6, 2024
0423809
FEAT : PageController 생성 - home과 reservation 페이지로 이동 가능
dev-domo Jun 21, 2024
ee2eb72
FEAT : ReservationController 생성 - 예약 추가, 조회, 삭제 API 작성
dev-domo Jun 21, 2024
48d66bb
FEAT : Reservation 생성 - id, 예약자명, 날짜, 시간을 갖는 예약 객체
dev-domo Jun 21, 2024
8d9a22b
CHORE : 1-2단계 수행을 위한 sql 파일 생성
dev-domo Jun 21, 2024
1446d29
FEAT : ReservationRepository 생성
dev-domo Jun 22, 2024
bc7677c
FEAT : ReservationService 생성
dev-domo Jun 22, 2024
c078c96
CHORE : Repository 및 Service 생성에 따른 수정
dev-domo Jun 22, 2024
788b9d2
REFACOR : 데이터베이스 추가 설정
dev-domo Jun 22, 2024
2acf662
FEAT : ReservationTimeController 생성
dev-domo Jun 25, 2024
9ce9565
FEAT : ReservationTime 생성 - id와 startAt을 멤버로 가짐
dev-domo Jun 25, 2024
a74051b
FEAT : ReservationTimeRepository 생성 - 예약시간 조회, 생성, 삭제 구현
dev-domo Jun 25, 2024
0d477e9
FEAT : ReservationTimeService 생성
dev-domo Jun 25, 2024
f05616c
CHORE : 오탈자 수정
dev-domo Jun 25, 2024
37cd26b
REFACTOR : time_id로 수정
dev-domo Jun 25, 2024
d3dac35
REFACTOR : SimpleJdbcInsert를 활용한 예약 생성 수정
dev-domo Jun 25, 2024
100cd1d
REFACTOR : addReservation 리턴 타입을 Long으로 수정
dev-domo Jun 25, 2024
bca58b1
REFACTOR : reservation_time 스키마 추가
dev-domo Jun 25, 2024
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)
);
Loading