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] 김수민 미션 제출합니다. #265

Open
wants to merge 23 commits into
base: boyekim
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
testImplementation 'com.h2database:h2'
}

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

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import roomescape.repository.JdbcReservationRepository;
import roomescape.repository.JdbcTimeRepository;
import roomescape.repository.ReservationRepository;
import roomescape.repository.TimeRepository;

import javax.sql.DataSource;

@Configuration
@RequiredArgsConstructor
public class Config {

private final DataSource dataSource;

@Bean
public ReservationRepository reservationRepository() {
return new JdbcReservationRepository(dataSource);
}

@Bean
public TimeRepository timeRepository() {
return new JdbcTimeRepository(dataSource);
}
}
23 changes: 23 additions & 0 deletions src/main/java/roomescape/controller/HomeController.java
Copy link

Choose a reason for hiding this comment

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

이렇게 time, reservation, home 빼두신 거 좋은 선택인 것 같아요. 좀 더 세부적으로 나누시려고 빼신 것 같은데 맞을까요?

Copy link
Author

Choose a reason for hiding this comment

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

HomeController 클래스 말씀하시는 것 맞을까요? 이외의 컨트롤러에서 RequestMapping을 써서 좀 더 깔끔하게 만들고자 단순히 페이지 뷰 이름만 반환하는 컨트롤러를 따로 분리하였습니다 ㅎㅎ

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package roomescape.controller;

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

@Controller
public class HomeController {

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

@GetMapping("/reservation")
public String reservationController() {
return "new-reservation";
}

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

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import roomescape.model.ReservationRequestDTO;
import roomescape.model.ReservationResponseDTO;
import roomescape.repository.service.ReservationService;

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

@RequiredArgsConstructor
@Slf4j
@Controller
@RequestMapping("/reservations")
public class ReservationController {

private final ReservationService reservationService;

@ResponseBody
@GetMapping
public List<ReservationResponseDTO> allReservationsController() {
List<ReservationResponseDTO> reservations = reservationService.findAll();
Copy link

Choose a reason for hiding this comment

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

기존에는 controller에서 repository를 호출하셔서 사용했는데 이번에는 service 클래스를 만들어서 service로 호출하신 거 구조에 대한 이해를 잘 하고 계신 것 같아요!

Copy link
Author

Choose a reason for hiding this comment

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

감사합니다

log.info("reservations = {}", reservations);
return reservations;
}

@ResponseBody
@PostMapping
public ResponseEntity<ReservationResponseDTO> reservationAddController(@Valid @RequestBody ReservationRequestDTO reservation) {
ReservationResponseDTO responseDTO = reservationService.reservationAdd(reservation);

HttpHeaders headers = new HttpHeaders();
String uri = "/reservations/" + responseDTO.getId();
headers.setLocation(URI.create(uri));
ResponseEntity<ReservationResponseDTO> response = new ResponseEntity<>(responseDTO, headers, HttpStatus.CREATED);

return response;
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteController(@PathVariable int id) {
reservationService.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
49 changes: 49 additions & 0 deletions src/main/java/roomescape/controller/TimeController.java
Copy link

Choose a reason for hiding this comment

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

TimeController도 요구사항에 맞춰서 잘 짜신 것 같습니다!

Copy link
Author

Choose a reason for hiding this comment

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

감사합니다

Copy link

Choose a reason for hiding this comment

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

여기도 전부 repository에서 service를 호출하셨네요. 굿입니다☺

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package roomescape.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import roomescape.model.TimeRequestDTO;
import roomescape.model.TimeResponseDTO;
import roomescape.repository.service.TimeService;

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

@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("times")
public class TimeController {

private final TimeService timeService;

@ResponseBody
@GetMapping
public List<TimeResponseDTO> findTime() {
List<TimeResponseDTO> times = timeService.findAll();
return times;
}

@ResponseBody
@PostMapping
public ResponseEntity<TimeResponseDTO> timeAdd(@RequestBody TimeRequestDTO timeRequestDTO) {
TimeResponseDTO resultTime = timeService.timeAdd(timeRequestDTO);
HttpHeaders headers = new HttpHeaders();
String uri = "/times/" + resultTime.getId();
headers.setLocation(URI.create(uri));
ResponseEntity<TimeResponseDTO> response = new ResponseEntity<>(resultTime, headers, HttpStatus.CREATED);
return response;
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteController(@PathVariable int id) {
timeService.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

}
26 changes: 26 additions & 0 deletions src/main/java/roomescape/model/Reservation.java
Copy link

Choose a reason for hiding this comment

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

이것도 요구사항 맞춰서 잘 하신 것 같습니당

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package roomescape.model;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;

@Data
public class Reservation {
private int id;

@NotBlank
private String name;

@NotBlank
private String date;

private Time time;

public Reservation(){
}

public Reservation(String name, String date, Time time) {
this.name = name;
this.date = date;
this.time = time;
}
}
11 changes: 11 additions & 0 deletions src/main/java/roomescape/model/ReservationRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package roomescape.model;

import lombok.Data;

@Data
public class ReservationRequestDTO {

private int time;
private String name;
private String date;
}
39 changes: 39 additions & 0 deletions src/main/java/roomescape/model/ReservationResponseDTO.java
Copy link

Choose a reason for hiding this comment

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

responseDTO도 만드셨군요! 저는 requestDTO만 생각했는데, 하나 배워갑니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package roomescape.model;

import lombok.Data;

@Data
public class ReservationResponseDTO {
private int id;

private String name;

private String date;

private Time time;

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

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

public ReservationResponseDTO() {
}

public static ReservationResponseDTO makingResponse(Reservation reservation) {
ReservationResponseDTO result = new ReservationResponseDTO();
result.setId(reservation.getId());
result.setTime(reservation.getTime());
result.setName(reservation.getName());
result.setDate(reservation.getDate());
return result;
}
}
21 changes: 21 additions & 0 deletions src/main/java/roomescape/model/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package roomescape.model;

import lombok.Data;

@Data
public class Time {
private int id;
private String time;

public Time() {
}

public Time(int id, String time) {
this.id = id;
this.time = time;
}

public Time(String time) {
this.time = time;
}
}
8 changes: 8 additions & 0 deletions src/main/java/roomescape/model/TimeRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package roomescape.model;

import lombok.Getter;

@Getter
public class TimeRequestDTO {
private String time;
}
16 changes: 16 additions & 0 deletions src/main/java/roomescape/model/TimeResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package roomescape.model;

import lombok.Data;

@Data
public class TimeResponseDTO {
private int id;
private String time;

public static TimeResponseDTO makingResponse(Time time) {
TimeResponseDTO result = new TimeResponseDTO();
result.setId(time.getId());
result.setTime(time.getTime());
return result;
}
}
74 changes: 74 additions & 0 deletions src/main/java/roomescape/repository/JdbcReservationRepository.java
Copy link

Choose a reason for hiding this comment

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

이 부분도 잘 고치신 것 같습니당

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package roomescape.repository;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import roomescape.model.Reservation;
import roomescape.model.Time;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Slf4j
public class JdbcReservationRepository implements ReservationRepository {
private final JdbcTemplate jdbcTemplate;
private final NamedParameterJdbcTemplate namedTemplate;

private final RowMapper<Reservation> reservationRowMapper = (rs, rowNum) -> {
Reservation reservation = getReservation(rs);
return reservation;
};


public JdbcReservationRepository(DataSource dataSource) {
this.namedTemplate = new NamedParameterJdbcTemplate(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
public Reservation reservationAdd(Reservation reservation) {
String sql = "insert into reservation (name, date, time_id) values (:name, :date, :time_id)";

SqlParameterSource param = new MapSqlParameterSource()
.addValue("name", reservation.getName())
.addValue("date", reservation.getDate())
.addValue("time_id", reservation.getTime().getId());

KeyHolder keyHolder = new GeneratedKeyHolder();
namedTemplate.update(sql, param, keyHolder);

int key = keyHolder.getKey().intValue();
reservation.setId(key);
return reservation;
}

@Override
public List<Reservation> findAll() {
String sql = "select r.id as reservation_id,r.name,r.date," +
"r.id as time_id,t.time as time_value " +
"from reservation as r inner join time as t on r.time_id = t.id ";
return jdbcTemplate.query(sql, reservationRowMapper);
}

@Override
public void delete(int id) {
jdbcTemplate.update("delete from reservation where id = ?", id);
}

private static Reservation getReservation(ResultSet rs) throws SQLException {
Reservation reservation = new Reservation();
reservation.setId(rs.getInt("id"));
reservation.setName(rs.getString("name"));
reservation.setDate(rs.getString("date"));
Time time = new Time(rs.getInt("time_id"), rs.getString("time_value"));
reservation.setTime(time);
return reservation;
}
}
Loading