Skip to content

[Spring Core] 정민주 미션 제출합니다. #416

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

Open
wants to merge 18 commits into
base: joungminju
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
37 changes: 17 additions & 20 deletions src/main/java/roomescape/controller/ReservationController.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
package roomescape.controller;

import jakarta.transaction.Transactional;
import java.net.URI;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import roomescape.entity.Dto.ReservationInDto;
import roomescape.entity.Reservation;
import roomescape.entity.repository.ReservationRepository;
import roomescape.exception.NotFoundException;
import roomescape.exception.ReservationException;
import roomescape.service.ReservationService;

@RestController
@RequestMapping("/reservations")

Choose a reason for hiding this comment

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

이전 단계에서 적용하신 @Transactional controller에 있는게 적절할까요? 아니면 새로 생긴 service layer에 있는 것이 적절할까요? 한번 고민해주시고 답해주세요!

Copy link
Author

Choose a reason for hiding this comment

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

음 get의 경우 굳이 Transactional을 안붙여도 되니, 필요한 메서드(POST, DELETE)에만 명시해주는 것이 더 좋은 구현방식일 것이라 생각했습니다!

public class ReservationController {

private final ReservationRepository reservationRepository;
private final ReservationService reservationService;

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

@GetMapping("/reservations")
@GetMapping
public List<Reservation> getReservations() {
return reservationRepository.findAll();
return reservationService.findAllReservations();
}

@Transactional
@PostMapping("/reservations")
public ResponseEntity<Reservation> createReservation(@RequestBody Reservation reservation) {
final Reservation save = reservationRepository.save(reservation);
URI location = URI.create("/reservations/" + save.getId());
return ResponseEntity.created(location).body(save);
@PostMapping
public ResponseEntity<Reservation> createReservation(@RequestBody ReservationInDto reservationInDto) {
final Reservation reservation = reservationService.saveReservation(reservationInDto);
URI location = URI.create("/reservations/" + reservation.getId());
return ResponseEntity.created(location).body(reservation);
}

@Transactional
@DeleteMapping("/reservations/{id}")
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteReservation(@PathVariable Long id) {
final int countOfDeleted = reservationRepository.deleteById(id);

if (countOfDeleted <= 0) {
throw new NotFoundException("해당 id를 가진 예약을 찾을 수 없습니다.");
}

reservationService.deleteReservationById(id);
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public String home() {

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

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

import java.net.URI;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import roomescape.entity.Dto.TimeInDto;
import roomescape.entity.value.Time;
import roomescape.service.ReservationService;

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

private final ReservationService reservationService;

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

@GetMapping
public List<Time> getTimes() {
return reservationService.findAllTimes();
}

@Transactional
@PostMapping
public ResponseEntity<Time> createTime(@RequestBody TimeInDto timeInDto) {
final Time time = reservationService.saveTime(timeInDto);
URI location = URI.create("/times/" + time.getId());
return ResponseEntity.created(location).body(time);
}

@Transactional
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteTime(@PathVariable Long id) {
reservationService.deleteTimeById(id);
return ResponseEntity.noContent().build();
}

Choose a reason for hiding this comment

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

여기도 위와 동일한 리뷰 입니다


}
26 changes: 26 additions & 0 deletions src/main/java/roomescape/entity/Dto/ReservationInDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package roomescape.entity.Dto;

public class ReservationInDto {

Choose a reason for hiding this comment

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

해당 dto는 현재 reservation을 생성할 때 이용하는 dto인 것 같습니다. 해당 명칭이 와닿지 않는데 ReservationCreateDto와 같이 의미를 한번에 나타내주는 네이밍으로 관리하는 것은 어떤가요?


private String name;
private String date;
private Long time;

Choose a reason for hiding this comment

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

해당 필드도 time의 값이 아닌 id 정보를 받아오는 것이기에 필드 명칭으로 timeId로 관리하는 것은 어떤가요?

Copy link
Author

Choose a reason for hiding this comment

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

아 화면단에서 요청이 넘어올때 key값이 'time'으로 넘어와서요!!


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

public Long getTimeId() {
return time;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}
}
33 changes: 33 additions & 0 deletions src/main/java/roomescape/entity/Dto/ReservationOutDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package roomescape.entity.Dto;

public class ReservationOutDto {

private Long id;

private String name;
private String date;
private Long timeId;

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

public Long getTimeId() {
return timeId;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}
}
19 changes: 19 additions & 0 deletions src/main/java/roomescape/entity/Dto/TimeInDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package roomescape.entity.Dto;

public class TimeInDto {

private String time;

private TimeInDto() {
this.time = null;
}

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

public String getTime() {
return time;
}

}
16 changes: 7 additions & 9 deletions src/main/java/roomescape/entity/Reservation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package roomescape.entity;

import roomescape.entity.Dto.ReservationOutDto;
import roomescape.entity.value.Date;
import roomescape.entity.value.Name;
import roomescape.entity.value.Time;
Expand All @@ -11,18 +12,15 @@ public class Reservation {
private final Date date;
private final Time time;

public Reservation(Long id, String name, String date, String time) {
public Reservation(Long id, String name, String date, Time time) {
this.id = id;
this.name = Name.of(name);
this.date = Date.of(date);
this.time = Time.of(time);
this.time = time;
}

public Reservation(String name, String date, String time) {
this.id = null;
this.name = Name.of(name);
this.date = Date.of(date);
this.time = Time.of(time);
public static Reservation of(ReservationOutDto reservationOutDto, Time time) {
return new Reservation(reservationOutDto.getId(), reservationOutDto.getName(), reservationOutDto.getDate(), time);
}

private Reservation() {
Expand All @@ -44,8 +42,8 @@ public String getDate() {
return date.getValue();
}

public String getTime() {
return time.getValue();
public Time getTime() {
return time;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import roomescape.entity.Reservation;
import roomescape.entity.Dto.ReservationInDto;
import roomescape.entity.Dto.ReservationOutDto;

@Repository
public class ReservationRepository {
Expand All @@ -22,23 +23,28 @@ public ReservationRepository(JdbcTemplate jdbcTemplate, DataSource source) {
.usingGeneratedKeyColumns("id");
}

public List<Reservation> findAll() {
String sql = "SELECT * FROM reservation";
public List<ReservationOutDto> findAll() {
String sql = "SELECT \n" +
" r.id as reservation_id, \n" +
" r.name, \n" +
" r.date, \n" +
" t.id as time_id, \n" +
" t.time as time_value \n" +
"FROM reservation as r inner join time as t on r.time_id = t.id\n";

Choose a reason for hiding this comment

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

Suggested change
String sql = "SELECT \n" +
" r.id as reservation_id, \n" +
" r.name, \n" +
" r.date, \n" +
" t.id as time_id, \n" +
" t.time as time_value \n" +
"FROM reservation as r inner join time as t on r.time_id = t.id\n";
String sql = """
SELECT
r.id AS reservation_id,
r.name,
r.date,
t.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, (rs, rowNum) ->
new Reservation(
rs.getLong("id"),
new ReservationOutDto(
rs.getLong("reservation_id"),
rs.getString("name"),
rs.getString("date"),
rs.getString("time")));
rs.getLong("time_id")));
}

public Reservation save(Reservation reservation) {
public Long save(ReservationInDto reservationInDto) {
SqlParameterSource params = new MapSqlParameterSource()
.addValue("name", reservation.getName())
.addValue("date", reservation.getDate())
.addValue("time", reservation.getTime());
long id = simpleJdbcInsert.executeAndReturnKey(params).longValue();
return new Reservation(id, reservation.getName(), reservation.getDate(), reservation.getTime());
.addValue("name", reservationInDto.getName())
.addValue("date", reservationInDto.getDate())
.addValue("time_id", reservationInDto.getTimeId());
return simpleJdbcInsert.executeAndReturnKey(params).longValue();
}

public int deleteById(Long id) {
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/roomescape/entity/repository/TimeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package roomescape.entity.repository;

import java.util.List;
import java.util.Optional;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import roomescape.entity.Dto.TimeInDto;
import roomescape.entity.value.Time;

@Repository
public class TimeRepository {

private final JdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert simpleJdbcInsert;

public TimeRepository(JdbcTemplate jdbcTemplate, DataSource source) {
this.jdbcTemplate = jdbcTemplate;
this.simpleJdbcInsert = new SimpleJdbcInsert(source)
.withTableName("time")
.usingGeneratedKeyColumns("id");
}

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

public Time save(TimeInDto timeInDto) {
SqlParameterSource params = new MapSqlParameterSource()
.addValue("time", timeInDto.getTime());
long id = simpleJdbcInsert.executeAndReturnKey(params).longValue();
return new Time(id, timeInDto.getTime());
}

public Optional<Time> findById(Long id) {
String sql = "SELECT * FROM time WHERE id = ?";
final List<Time> times = jdbcTemplate.query(sql, new Object[]{id}, (rs, rowNum) ->
new Time(
rs.getLong("id"),
rs.getString("time")));
return times.stream().findFirst();
}

public int deleteById(Long id) {
String sql = "DELETE FROM time WHERE id = ?";
return jdbcTemplate.update(sql, id);
}

}
Loading