Skip to content
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
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.ReservationCreateDto;
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 ReservationCreateDto reservationCreateDto) {
final Reservation reservation = reservationService.saveReservation(reservationCreateDto);
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.TimeCreateDto;
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 TimeCreateDto timeCreateDto) {
final Time time = reservationService.saveTime(timeCreateDto);
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();
}

}
14 changes: 7 additions & 7 deletions src/main/java/roomescape/entity/Reservation.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ 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;
public Reservation(Long id, String name, String date, Long timdeId, String timeValue) {
this.id = id;
this.name = Name.of(name);
this.date = Date.of(date);
this.time = Time.of(time);
this.time = new Time(timdeId, timeValue);
}

private Reservation() {
Expand All @@ -44,8 +44,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
@@ -1,44 +1,54 @@
package roomescape.entity.repository;
package roomescape.entity.dao;

import java.util.List;
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 org.springframework.stereotype.Component;
import roomescape.entity.Reservation;
import roomescape.entity.dto.ReservationCreateDto;

@Repository
public class ReservationRepository {
@Component
public class ReservationDao {

private final JdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert simpleJdbcInsert;

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

public List<Reservation> findAll() {
String sql = "SELECT * FROM reservation";
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"),
rs.getLong("reservation_id"),
rs.getString("name"),
rs.getString("date"),
rs.getString("time")));
rs.getLong("time_id"),
rs.getString("time_value")));
}

public Reservation save(Reservation reservation) {
public Long save(ReservationCreateDto reservationCreateDto) {
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", reservationCreateDto.getName())
.addValue("date", reservationCreateDto.getDate())
.addValue("time_id", reservationCreateDto.getTimeId());
return simpleJdbcInsert.executeAndReturnKey(params).longValue();
}

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

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.Component;
import roomescape.entity.dto.TimeCreateDto;
import roomescape.entity.value.Time;

@Component
public class TimeDao {

private final JdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert simpleJdbcInsert;

public TimeDao(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(TimeCreateDto timeCreateDto) {
SqlParameterSource params = new MapSqlParameterSource()
.addValue("time", timeCreateDto.getTime());
long id = simpleJdbcInsert.executeAndReturnKey(params).longValue();
return new Time(id, timeCreateDto.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 Optional<Time> findByTime(String time) {
String sql = "SELECT * FROM time WHERE time = ?";
final List<Time> times = jdbcTemplate.query(sql, new Object[]{time}, (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);
}

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

public class ReservationCreateDto {

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

public ReservationCreateDto(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;
}
}
19 changes: 19 additions & 0 deletions src/main/java/roomescape/entity/dto/TimeCreateDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package roomescape.entity.dto;

public class TimeCreateDto {

private String time;

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

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

public String getTime() {
return time;
}

}
Loading