diff --git a/src/main/java/roomescape/ReservationController.java b/src/main/java/roomescape/ReservationController.java deleted file mode 100644 index 8b445901..00000000 --- a/src/main/java/roomescape/ReservationController.java +++ /dev/null @@ -1,66 +0,0 @@ -package roomescape; - -import java.net.URI; -import java.util.List; -import java.util.stream.Collectors; -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; - -@Controller -public class ReservationController { - - private final ReservationDAO reservationDAO; - - public ReservationController(ReservationDAO reservationDAO) { - this.reservationDAO = reservationDAO; - } - - @GetMapping("/reservation") - public String goReservationPage() { - return "reservation"; - } - - @PostMapping("/reservations") - public ResponseEntity createReservation(@RequestBody ReservationSaveRequestDto requestDto) { - if (isReservationArgumentEmpty(requestDto)) { - throw new IllegalArgumentException("잘못된 요청입니다."); - } - - Reservation reservation = new Reservation(requestDto.getName(), requestDto.getDate(), requestDto.getTime()); - - Long id = reservationDAO.insert(reservation); - - return ResponseEntity.created(URI.create("/reservations/" + id)) - .body(new ReservationResponseDto(id, reservation)); - } - - @GetMapping("/reservations") - public ResponseEntity> readAllReservations() { - List reservationResponseDtos = reservationDAO.findAllReservations() - .stream() - .map(ReservationResponseDto::new) - .collect(Collectors.toList()); - return ResponseEntity.ok().body(reservationResponseDtos); - } - - @DeleteMapping("/reservations/{id}") - public ResponseEntity deleteReservation(@PathVariable Long id) { - reservationDAO.delete(id); - return ResponseEntity.noContent().build(); - } - - private boolean isReservationArgumentEmpty(ReservationSaveRequestDto requestDto) { - return isStringEmpty(requestDto.getName()) - || isStringEmpty(requestDto.getDate()) - || isStringEmpty(requestDto.getTime()); - } - - private boolean isStringEmpty(String argument) { - return argument == null || argument.trim().isEmpty(); - } -} diff --git a/src/main/java/roomescape/controller/ReservationController.java b/src/main/java/roomescape/controller/ReservationController.java new file mode 100644 index 00000000..2712c12d --- /dev/null +++ b/src/main/java/roomescape/controller/ReservationController.java @@ -0,0 +1,49 @@ +package roomescape.controller; + +import java.net.URI; +import java.util.List; +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.ReservationResponseDto; +import roomescape.dto.ReservationSaveRequestDto; +import roomescape.service.ReservationService; + +@Controller +public class ReservationController { + + private final ReservationService reservationService; + + public ReservationController(ReservationService reservationService) { + this.reservationService = reservationService; + } + + @GetMapping("/reservation") + public String goReservationPage() { + return "new-reservation"; + } + + @PostMapping("/reservations") + public ResponseEntity createReservation(@RequestBody ReservationSaveRequestDto requestDto) { + ReservationResponseDto reservationResponseDto = reservationService.createReservation(requestDto); + return ResponseEntity.created(URI.create("/reservations/" + reservationResponseDto.getId())) + .body(reservationResponseDto); + } + + @GetMapping("/reservations") + public ResponseEntity> readAllReservations() { + + return ResponseEntity.ok().body(reservationService.getAllReservations()); + } + + @DeleteMapping("/reservations/{id}") + public ResponseEntity deleteReservation(@PathVariable Long id) { + reservationService.deleteReservation(id); + + return ResponseEntity.noContent().build(); + } +} diff --git a/src/main/java/roomescape/controller/TimeController.java b/src/main/java/roomescape/controller/TimeController.java new file mode 100644 index 00000000..a6cb94b8 --- /dev/null +++ b/src/main/java/roomescape/controller/TimeController.java @@ -0,0 +1,50 @@ +package roomescape.controller; + +import java.net.URI; +import java.util.List; +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.TimeResponseDto; +import roomescape.dto.TimeSaveRequestDto; +import roomescape.service.TimeService; + +@Controller +public class TimeController { + + private final TimeService timeService; + + public TimeController(TimeService timeService) { + this.timeService = timeService; + } + + @GetMapping("/time") + public String goReservationPage() { + return "time"; + } + + @PostMapping("/times") + public ResponseEntity createTime(@RequestBody TimeSaveRequestDto requestDto) { + TimeResponseDto timeResponseDto = timeService.createTime(requestDto); + + return ResponseEntity.created(URI.create("/times/" + timeResponseDto.getId())) + .body(timeResponseDto); + } + + @GetMapping("/times") + public ResponseEntity> readAllTimes() { + + return ResponseEntity.ok().body(timeService.readAllTimes()); + } + + @DeleteMapping("/times/{id}") + public ResponseEntity deleteTime(@PathVariable Long id) { + timeService.deleteTime(id); + + return ResponseEntity.noContent().build(); + } +} diff --git a/src/main/java/roomescape/Reservation.java b/src/main/java/roomescape/domain/Reservation.java similarity index 74% rename from src/main/java/roomescape/Reservation.java rename to src/main/java/roomescape/domain/Reservation.java index bdc29954..d8a6a318 100644 --- a/src/main/java/roomescape/Reservation.java +++ b/src/main/java/roomescape/domain/Reservation.java @@ -1,4 +1,4 @@ -package roomescape; +package roomescape.domain; import lombok.AllArgsConstructor; import lombok.Getter; @@ -12,9 +12,9 @@ public class Reservation { private Long id; private String name; private String date; - private String time; + private Time time; - public Reservation(String name, String date, String time) { + public Reservation(String name, String date, Time time) { this.name = name; this.date = date; this.time = time; diff --git a/src/main/java/roomescape/domain/Time.java b/src/main/java/roomescape/domain/Time.java new file mode 100644 index 00000000..2d4dfd32 --- /dev/null +++ b/src/main/java/roomescape/domain/Time.java @@ -0,0 +1,27 @@ +package roomescape.domain; + +public class Time { + + private Long id; + private String time; + + public Time() { + } + + public Time(String time) { + this.time = time; + } + + public Time(Long id, String time) { + this.id = id; + this.time = time; + } + + public Long getId() { + return id; + } + + public String getTime() { + return time; + } +} diff --git a/src/main/java/roomescape/ReservationResponseDto.java b/src/main/java/roomescape/dto/ReservationResponseDto.java similarity index 56% rename from src/main/java/roomescape/ReservationResponseDto.java rename to src/main/java/roomescape/dto/ReservationResponseDto.java index d2c2d972..a25be642 100644 --- a/src/main/java/roomescape/ReservationResponseDto.java +++ b/src/main/java/roomescape/dto/ReservationResponseDto.java @@ -1,39 +1,41 @@ -package roomescape; +package roomescape.dto; + +import roomescape.domain.Reservation; public class ReservationResponseDto { - private final Long id; - private final String name; - private final String date; - private final String time; + private Long id; + private String name; + private String date; + private TimeResponseDto time; public ReservationResponseDto(Reservation reservation) { this.id = reservation.getId(); this.name = reservation.getName(); this.date = reservation.getDate(); - this.time = reservation.getTime(); + this.time = new TimeResponseDto(reservation.getTime()); } public ReservationResponseDto(Long id, Reservation reservation) { this.id = id; this.name = reservation.getName(); this.date = reservation.getDate(); - this.time = reservation.getTime(); + this.time = new TimeResponseDto(reservation.getTime()); } public Long getId() { - return this.id; + return id; } public String getName() { - return this.name; + return name; } public String getDate() { - return this.date; + return date; } - public String getTime() { - return this.time; + public TimeResponseDto getTime() { + return time; } } diff --git a/src/main/java/roomescape/ReservationSaveRequestDto.java b/src/main/java/roomescape/dto/ReservationSaveRequestDto.java similarity index 59% rename from src/main/java/roomescape/ReservationSaveRequestDto.java rename to src/main/java/roomescape/dto/ReservationSaveRequestDto.java index cbe13500..5b00b8a5 100644 --- a/src/main/java/roomescape/ReservationSaveRequestDto.java +++ b/src/main/java/roomescape/dto/ReservationSaveRequestDto.java @@ -1,23 +1,23 @@ -package roomescape; +package roomescape.dto; public class ReservationSaveRequestDto { private String name; private String date; - private String time; + private Long time; public ReservationSaveRequestDto() { } public String getName() { - return this.name; + return name; } public String getDate() { - return this.date; + return date; } - public String getTime() { - return this.time; + public Long getTime() { + return time; } } diff --git a/src/main/java/roomescape/dto/TimeResponseDto.java b/src/main/java/roomescape/dto/TimeResponseDto.java new file mode 100644 index 00000000..2c39f770 --- /dev/null +++ b/src/main/java/roomescape/dto/TimeResponseDto.java @@ -0,0 +1,27 @@ +package roomescape.dto; + +import roomescape.domain.Time; + +public class TimeResponseDto { + + private Long id; + private String time; + + public TimeResponseDto(Time time) { + this.id = time.getId(); + this.time = time.getTime(); + } + + public TimeResponseDto(Long id, Time time) { + this.id = id; + this.time = time.getTime(); + } + + public Long getId() { + return id; + } + + public String getTime() { + return time; + } +} diff --git a/src/main/java/roomescape/dto/TimeSaveRequestDto.java b/src/main/java/roomescape/dto/TimeSaveRequestDto.java new file mode 100644 index 00000000..ca020a0c --- /dev/null +++ b/src/main/java/roomescape/dto/TimeSaveRequestDto.java @@ -0,0 +1,13 @@ +package roomescape.dto; + +public class TimeSaveRequestDto { + + private String time; + + public TimeSaveRequestDto() { + } + + public String getTime() { + return time; + } +} diff --git a/src/main/java/roomescape/GlobalExceptionHandler.java b/src/main/java/roomescape/exception/GlobalExceptionHandler.java similarity index 93% rename from src/main/java/roomescape/GlobalExceptionHandler.java rename to src/main/java/roomescape/exception/GlobalExceptionHandler.java index 65c32809..3455358c 100644 --- a/src/main/java/roomescape/GlobalExceptionHandler.java +++ b/src/main/java/roomescape/exception/GlobalExceptionHandler.java @@ -1,4 +1,4 @@ -package roomescape; +package roomescape.exception; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; diff --git a/src/main/java/roomescape/ReservationDAO.java b/src/main/java/roomescape/repository/ReservationDAO.java similarity index 64% rename from src/main/java/roomescape/ReservationDAO.java rename to src/main/java/roomescape/repository/ReservationDAO.java index df1c9678..2500f197 100644 --- a/src/main/java/roomescape/ReservationDAO.java +++ b/src/main/java/roomescape/repository/ReservationDAO.java @@ -1,4 +1,4 @@ -package roomescape; +package roomescape.repository; import java.sql.PreparedStatement; import java.util.List; @@ -7,6 +7,8 @@ import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.stereotype.Repository; +import roomescape.domain.Reservation; +import roomescape.domain.Time; @Repository public class ReservationDAO { @@ -17,8 +19,22 @@ public ReservationDAO(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } - public Long insert(Reservation reservation) { - String sql = "INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)"; + private final RowMapper reservationRowMapper = (resultSet, rowNum) -> { + Time time = new Time( + resultSet.getLong("time_id"), + resultSet.getString("time_value") + ); + + return new Reservation( + resultSet.getLong("reservation_id"), + resultSet.getString("name"), + resultSet.getString("date"), + time + ); + }; + + public Long insert(Reservation reservation, Long timeId) { + String sql = "INSERT INTO reservation (name, date, time_id) VALUES (?, ?, ?)"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(connect -> { @@ -27,19 +43,13 @@ public Long insert(Reservation reservation) { new String[]{"id"}); ps.setString(1, reservation.getName()); ps.setString(2, reservation.getDate()); - ps.setString(3, reservation.getTime()); + ps.setLong(3, timeId); return ps; }, keyHolder); return keyHolder.getKey().longValue(); } - public List findAllReservations() { - String sql = "SELECT id, name, date, time FROM reservation"; - - return jdbcTemplate.query(sql, actorRowMapper); - } - public void delete(Long id) { if (countReservationById(id) == 0) { throw new IllegalArgumentException("해당 예약이 존재하지 않습니다."); @@ -54,13 +64,11 @@ private int countReservationById(Long id) { return rowCount; } - private final RowMapper actorRowMapper = (resultSet, rowNum) -> { - Reservation reservation = new Reservation( - resultSet.getLong("id"), - resultSet.getString("name"), - resultSet.getString("date"), - resultSet.getString("time") - ); - return reservation; - }; + public List findAllReservations() { + 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, reservationRowMapper); + } } diff --git a/src/main/java/roomescape/repository/TimeDAO.java b/src/main/java/roomescape/repository/TimeDAO.java new file mode 100644 index 00000000..0da58619 --- /dev/null +++ b/src/main/java/roomescape/repository/TimeDAO.java @@ -0,0 +1,62 @@ +package roomescape.repository; + +import java.sql.PreparedStatement; +import java.util.List; +import java.util.Optional; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; +import roomescape.domain.Time; + +@Repository +public class TimeDAO { + + private final JdbcTemplate jdbcTemplate; + + public TimeDAO(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public Time insert(Time time) { + String sql = "INSERT INTO time (time) VALUES ?"; + KeyHolder keyHolder = new GeneratedKeyHolder(); + + jdbcTemplate.update(connect -> { + PreparedStatement ps = connect.prepareStatement( + sql, + new String[]{"id"}); + ps.setString(1, time.getTime()); + return ps; + }, keyHolder); + + Long id = keyHolder.getKey().longValue(); + return new Time(id, time.getTime()); + } + + public List