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

[Spring Core] 남해윤 미션 제출합니다. #391

Open
wants to merge 14 commits into
base: haeyoon1
Choose a base branch
from
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
}

test {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/roomescape/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package roomescape.controller;

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

@Controller
public class HomeController {

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

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

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import roomescape.dto.ReservationRequestDto;
import roomescape.dto.ReservationResponseDto;
import roomescape.service.ReservationService;

import java.net.URI;
import java.util.List;

@Controller
public class ReservationController {

private final ReservationService reservationService;

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

// 홈화면
@GetMapping("/reservation")
public String reservationPage() {
return "new-reservation";
}

//예약 조회
@ResponseBody
@GetMapping("/reservations")
public ResponseEntity<List<ReservationResponseDto>> list() {
List<ReservationResponseDto> reservations = reservationService.getAllReservations();
return ResponseEntity.ok(reservations);
}

//예약 추가
@ResponseBody
@PostMapping("/reservations")
public ResponseEntity<ReservationResponseDto> create(@RequestBody ReservationRequestDto newReservationDto) {

ReservationResponseDto reservation = reservationService.createReservation(newReservationDto);

return ResponseEntity.created(URI.create("/reservations/" + reservation.getId()))
.body(reservation);
}

//예약 삭제
@DeleteMapping("/reservations/{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) {
reservationService.deleteReservation(id);
return ResponseEntity.noContent().build();
}
}
52 changes: 52 additions & 0 deletions src/main/java/roomescape/controller/TimeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package roomescape.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import roomescape.dto.TimeRequestDto;
import roomescape.dto.TimeResponseDto;
import roomescape.service.TimeService;

import java.net.URI;
import java.time.LocalDateTime;
import java.util.List;

@Controller
public class TimeController {

private final TimeService timeService;

public TimeController(TimeService timeService) {
this.timeService = timeService;
}

// 홈화면
@GetMapping("/time")
public String reservationPage() {
return "time";
}

@PostMapping("/times") //시간 추가
public ResponseEntity<TimeResponseDto> createTime(@RequestBody TimeRequestDto requestDto){
TimeResponseDto newTime = timeService.createTime(requestDto);
URI location = URI.create("/times/" + newTime.getId());

return ResponseEntity.created(location)
.body(newTime);
}
@GetMapping("/times") //시간 조회
public ResponseEntity<List<TimeResponseDto>> findTimes(){
List<TimeResponseDto> timeList = timeService.findAllTimes();
return ResponseEntity.ok(timeList);
}

@DeleteMapping("/times/{id}") // 시간 삭제
public ResponseEntity<TimeResponseDto> deleteTime(@PathVariable Long id){
timeService.deleteTime(id);
return ResponseEntity.noContent().build();
}
}
53 changes: 53 additions & 0 deletions src/main/java/roomescape/dao/ReservationDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package roomescape.dao;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import roomescape.entity.Reservation;
import roomescape.entity.Time;

import java.util.List;

@Repository
public class ReservationDao {

private final JdbcTemplate jdbcTemplate;

public ReservationDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public List<Reservation> findAll() {
String sql = """
SELECT r.id AS reservation_id, r.name, r.date, t.id AS time_id, t.time AS time_value
FROM reservation r
INNER JOIN time t ON r.time_id = t.id
""";

return jdbcTemplate.query(sql, (rs, rowNum) -> new Reservation(
rs.getLong("reservation_id"),
rs.getString("name"),
rs.getString("date"),
new Time(
rs.getLong("time_id"),
rs.getString("time_value")
)
));
}


public Reservation insert(Reservation reservation) {
String sql = "INSERT INTO reservation(name, date, time) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, reservation.getName(), reservation.getDate(), reservation.getTime().getId());

String query = "SELECT id FROM reservation ORDER BY id DESC LIMIT 1";
Long id = jdbcTemplate.queryForObject(query, Long.class);

return new Reservation(id, reservation.getName(), reservation.getDate(), reservation.getTime());
}

public void delete(Long id) {
String sql = "DELETE FROM reservation WHERE id = ?";
jdbcTemplate.update(sql, id);
}

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

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import roomescape.entity.Time;

import java.util.List;

@Repository
public class TimeDao {
public final JdbcTemplate jdbcTemplate;

public TimeDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public List<Time> findAll() {
String sql = "SELECT id, time FROM time";
return jdbcTemplate.query(sql, (rs, rowNum) -> new Time(
rs.getLong("id"),
rs.getString("time")
));
}

public Time insert(Time time) {
String sql = "INSERT INTO time(time) VALUES (?)";
jdbcTemplate.update(sql, time.getTime());

String query = "SELECT id FROM time ORDER BY id DESC LIMIT 1";
Long id = jdbcTemplate.queryForObject(query, Long.class);

return new Time(id, time.getTime());
}

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

import roomescape.entity.Time;

public class ReservationRequestDto {
private String name;
private String date;
private String time;

public ReservationRequestDto(String name, String date, String time) {
this.name = name;
this.date = date;
this.time = time;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}

public String getTime() {
return time;
}

public Time getStringTimeAsTime(){ //string -> time
return new Time(time);
}
}
35 changes: 35 additions & 0 deletions src/main/java/roomescape/dto/ReservationResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package roomescape.dto;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class ReservationResponseDto {
private Long id;
private String name;
private String date;
private LocalTime time;

public ReservationResponseDto(Long id, String name, String date, LocalTime time) {
this.id = id;
this.name = name;
this.date = date;
this.time = time;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}

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

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class TimeRequestDto {
private String time;

public TimeRequestDto() {}

public TimeRequestDto(String time) {
this.time = time;
}

public String getTime() {
return time;
}

public LocalTime toLocalTime() { //string -> localTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return LocalTime.parse(time, formatter);
}
}
22 changes: 22 additions & 0 deletions src/main/java/roomescape/dto/TimeResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package roomescape.dto;

import java.time.LocalTime;

public class TimeResponseDto {

public Long id;
public String time;

public TimeResponseDto(Long id, LocalTime time) {
this.id = id;
this.time = time.toString();
}

public Long getId() {
return id;
}

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

import java.time.LocalTime;

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 Reservation(String name, String date, Time time) {
this(null, name, date, time);
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}

public Time getTime() {
return time;
}

public LocalTime getTimeAsLocalTime() {
return time.getTimeASALocalTime();
}
}
Loading