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 1 commit
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 src/main/java/roomescape/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
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;

Expand All @@ -15,7 +17,12 @@ public class Config {
private final DataSource dataSource;

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

@Bean
public TimeRepository timeRepository() {
return new JdbcTimeRepository(dataSource);
}
}
10 changes: 10 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
Expand Up @@ -10,4 +10,14 @@ public class HomeController {
public String home() {
return "home";
}

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

@GetMapping("time")
public String timeController() {
return "time";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public List<ReservationDTO> allReservationsController() {
return reservations;
}

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

@ResponseBody
@PostMapping("/reservations")
public ResponseEntity<ReservationDTO> reservationAddController(@Valid @RequestBody ReservationDTO reservationDTO) {
Expand Down
50 changes: 50 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,50 @@
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.Time;
import roomescape.repository.TimeRepository;

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

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

private final TimeRepository timeRepository;

@ResponseBody
@GetMapping
public List<Time> findTime() {
List<Time> times = timeRepository.findAll();
log.info("times = {}", times);
return times;
}

@ResponseBody
@PostMapping
public ResponseEntity<Time> timeAdd(@RequestBody Time time) {
Time resultTime = timeRepository.timeAdd(time);
HttpHeaders headers = new HttpHeaders();
String uri = "/times/" + resultTime.getId();
headers.setLocation(URI.create(uri));
ResponseEntity<Time> response = new ResponseEntity<>(resultTime, headers, HttpStatus.CREATED);
log.info("response = {}", response);
return response;
}

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

}
2 changes: 0 additions & 2 deletions src/main/java/roomescape/model/ReservationDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import jakarta.validation.constraints.NotBlank;
import lombok.Data;

import java.io.Serializable;

@Data
public class ReservationDTO {
int id;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/roomescape/model/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package roomescape.model;

import lombok.Data;

@Data
public class Time {
int id;
String time;
}
50 changes: 50 additions & 0 deletions src/main/java/roomescape/repository/JdbcTimeRepository.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,50 @@
package roomescape.repository;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import roomescape.model.ReservationDTO;
import roomescape.model.Time;

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

public class JdbcTimeRepository implements TimeRepository {

private final SimpleJdbcInsert jdbcInsert;
private final JdbcTemplate jdbcTemplate;

public JdbcTimeRepository(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.jdbcInsert = new SimpleJdbcInsert(dataSource)
.withTableName("time")
.usingGeneratedKeyColumns("id");
}

@Override
public Time timeAdd(Time time) {
SqlParameterSource param = new BeanPropertySqlParameterSource(time);
Number key = jdbcInsert.executeAndReturnKey(param);
time.setId(key.intValue());
return time;
}

@Override
public List<Time> findAll() {
String sql = "select * from time";
return jdbcTemplate.query(sql, timeRowMapper());
}

private RowMapper<Time> timeRowMapper() {
return BeanPropertyRowMapper.newInstance(Time.class);
}

@Override
public void delete(int id) {
jdbcTemplate.update("delete from time where id = ?", id);
}
}
14 changes: 14 additions & 0 deletions src/main/java/roomescape/repository/TimeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package roomescape.repository;

import roomescape.model.Time;

import java.util.List;

public interface TimeRepository {

Time timeAdd(Time time);

List<Time> findAll();

void delete(int id);
}
15 changes: 11 additions & 4 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
CREATE TABLE reservation
(
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
date VARCHAR(255) NOT NULL,
time VARCHAR(255) NOT NULL,
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
date VARCHAR(255) NOT NULL,
time VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE time
(
id BIGINT NOT NULL AUTO_INCREMENT,
time VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
25 changes: 25 additions & 0 deletions src/test/java/roomescape/MissionStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,29 @@ public class MissionStepTest {
Integer countAfterDelete = jdbcTemplate.queryForObject("SELECT count(1) from reservation", Integer.class);
assertThat(countAfterDelete).isEqualTo(0);
}

@Test
void 팔단계() {
Map<String, String> params = new HashMap<>();
params.put("time", "10:00");

RestAssured.given().log().all()
.contentType(ContentType.JSON)
.body(params)
.when().post("/times")
.then().log().all()
.statusCode(201)
.header("Location", "/times/1");

RestAssured.given().log().all()
.when().get("/times")
.then().log().all()
.statusCode(200)
.body("size()", is(1));

RestAssured.given().log().all()
.when().delete("/times/1")
.then().log().all()
.statusCode(204);
}
}