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] 안정현 미션 제출합니다. #272

Open
wants to merge 21 commits into
base: anhye0n
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion src/main/java/roomescape/controller/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public String viewHome() {
@GetMapping("/reservation")
public String viewReservation(){

return "reservation";
return "new-reservation";
}
}
16 changes: 6 additions & 10 deletions src/main/java/roomescape/controller/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,33 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import roomescape.dao.ReservationRepository;
import roomescape.exception.NotFoundReservationException;
import roomescape.domain.Reservation;
import roomescape.exception.NotFoundReservationException;
import roomescape.service.ReservationService;

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

@Controller
public class ReservationController {
private ReservationRepository reservationRepository;

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

@GetMapping("/reservations")
public ResponseEntity<List<Reservation>> getReservations() {
final List<Reservation> reservations = reservationRepository.findAll();
final List<Reservation> reservations = reservationService.getAllReservation();
return ResponseEntity.ok().body(reservations);
}

@PostMapping("/reservations")
public ResponseEntity<Reservation> createReservation(@RequestBody Reservation reservation) {
Reservation newReservation = reservationRepository.save(reservation);
Reservation newReservation = reservationService.insertReservation(reservation);

return ResponseEntity.created(URI.create("/reservations/" + newReservation.getId())).body(newReservation);
}

@DeleteMapping("/reservations/{id}")
public ResponseEntity<Void> deleteReservation(@PathVariable int id) {
reservationRepository.delete(id);
reservationService.deleteReservation(id);

return ResponseEntity.noContent().build();
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/roomescape/controller/TimeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package roomescape.controller;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import roomescape.domain.Time;
import roomescape.exception.NotFoundReservationException;
import roomescape.service.TimeService;

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

@Controller
public class TimeController {

private TimeService timeService;

@GetMapping("/times")
public ResponseEntity<List<Time>> getTimeInformation() {
final List<Time> time = timeService.getAllReservation();
return ResponseEntity.ok().body(time);
}

@PostMapping("/times")
public ResponseEntity<Time> createTime(
@DateTimeFormat(pattern = "HH:mm")
@RequestBody Time time
Comment on lines +27 to +28

Choose a reason for hiding this comment

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

@DateTimeFormat@RequestBody에서 동작하지 않는다고 하네요 :)

@DateTimeFormat을 이용한 날짜 포매팅

) {
Time newTime = timeService.inserTime(time);

return ResponseEntity.created(URI.create("/times/" + newTime.getId())).body(newTime);
}

@DeleteMapping("/times/{id}")
public ResponseEntity<Void> deleteTime(@PathVariable int id){
timeService.deleteTime(id);

return ResponseEntity.noContent().build();
}

@ExceptionHandler(NotFoundReservationException.class)
public ResponseEntity<Void> handleException(NotFoundReservationException e) {
return ResponseEntity.badRequest().build();
}
}
41 changes: 25 additions & 16 deletions src/main/java/roomescape/dao/ReservationRepository.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
package roomescape.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
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 org.springframework.stereotype.Repository;
import roomescape.domain.Reservation;
import roomescape.domain.Time;

import java.sql.PreparedStatement;
import java.util.List;

@Repository
public class ReservationRepository {
private final JdbcTemplate jdbcTemplate;

@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public ReservationRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public List<Reservation> 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";

return jdbcTemplate.query(
"select * from reservation",
sql,
(resultSet, rowNum) ->
new Reservation(
resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getString("date"),
resultSet.getString("time")
new Time(resultSet.getInt("time_id"), resultSet.getTime("time_value").toLocalTime())
)
);
}

public Reservation save(Reservation reservation) {
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(
"insert into reservation (name, date, time) values (?, ?, ?)",
new String[]{"id"});
ps.setString(1, reservation.getName());
ps.setString(2, reservation.getDate());
ps.setString(3, reservation.getTime());
return ps;
}, keyHolder);

return new Reservation((int) keyHolder.getKey().longValue(), reservation.getName(), reservation.getDate(), reservation.getTime());
String sql = "INSERT INTO reservation (name, date, time) VALUES (:name, :date, :time)";

SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(reservation);
GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
namedParameterJdbcTemplate.update(sql, namedParameters, keyHolder);
int generatedKey = keyHolder.getKey().intValue();
return new Reservation(generatedKey, reservation.getName(), reservation.getDate(), reservation.getTime());
}

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;
import roomescape.domain.Time;

import java.util.List;

@Repository
public class TimeRepository {
private final JdbcTemplate jdbcTemplate;

@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public TimeRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public List<Time> findAll() {
return jdbcTemplate.query(
"select * from time",
(resultSet, rowNum) ->
new Time(
resultSet.getInt("id"),
resultSet.getTime("time").toLocalTime()
)
);
}

public Time save(Time time) {
String sql = "INSERT INTO time (time) VALUES (:time)";
SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(time);
GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
namedParameterJdbcTemplate.update(sql, namedParameters, keyHolder);
int generatedKey = keyHolder.getKey().intValue();
return new Time(generatedKey, time.getTime());
}

public int delete(int id) {
String sql = "delete from time where id = ?";
return jdbcTemplate.update(sql, id);
}
}
27 changes: 6 additions & 21 deletions src/main/java/roomescape/domain/Reservation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,29 @@
import roomescape.exception.NotFoundReservationException;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Reservation {
private int id;
private String name;
private String date;
private String time;
private Time time;

public Reservation() {
}
public Reservation() { }

public Reservation(String name, String date, String time) {
public Reservation(String name, String date, Time time) {
validateEmpty(name);
validateDate(date);
validateTime(time);

this.name = name;
this.date = date;
this.time = time;
}

public Reservation(int id, String name, String date, String time) {
public Reservation(int id, String name, String date, Time time) {
validateEmpty(name);
validateDate(date);
validateTime(time);

this.id = id;
this.name = name;
Expand Down Expand Up @@ -61,11 +57,11 @@ public void setDate(String date) {
this.date = date;
}

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

public void setTime(String time) {
public void setTime(Time time) {
this.time = time;
}

Expand All @@ -88,15 +84,4 @@ private void validateDate(String dateString) {
throw new IllegalArgumentException("날짜 형식이 맞지 않습니다.");
}
}

private void validateTime(String timeString) {
DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");

try {
LocalTime.parse(timeString, TIME_FORMATTER);
} catch (DateTimeParseException e) {
throw new IllegalArgumentException("시간 형식이 맞지 않습니다.");
}
}

}
35 changes: 35 additions & 0 deletions src/main/java/roomescape/domain/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package roomescape.domain;

import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalTime;

public class Time {

private int id;
@DateTimeFormat(pattern = "HH:mm")
private LocalTime time;

public Time() {}

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

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public LocalTime getTime() {
return time;
}

public void setTIme(LocalTime time) {
this.time = time;
}
}
28 changes: 28 additions & 0 deletions src/main/java/roomescape/service/ReservationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package roomescape.service;

import roomescape.dao.ReservationRepository;
import roomescape.domain.Reservation;

import java.util.List;

public class ReservationService {

Comment on lines +7 to +9
Copy link

@mangsuyo mangsuyo May 28, 2024

Choose a reason for hiding this comment

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

Service 어노테이션이 빠졌네요!

이러한 어노테이션은 의존성 주입에 관련해서 어떤 기능을 하는지 저에게 알려주세요 :)

private final ReservationRepository reservationRepository;

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

public List<Reservation> getAllReservation() {
return reservationRepository.findAll();
}

public Reservation insertReservation(Reservation reservation) {
return reservationRepository.save(reservation);
}

public int deleteReservation(int id){
return reservationRepository.delete(id);
}

}
23 changes: 23 additions & 0 deletions src/main/java/roomescape/service/TimeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package roomescape.service;

import roomescape.dao.TimeRepository;
import roomescape.domain.Time;

import java.util.List;

public class TimeService {

private TimeRepository timeRepository;

public List<Time> getAllReservation() {

Choose a reason for hiding this comment

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

메서드 이름이 수정되어야 할 것 같아요 :)

return timeRepository.findAll();
}

public Time inserTime(Time time) {

Choose a reason for hiding this comment

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

오타가 있네요!

return timeRepository.save(time);
}

public int deleteTime(int id) {
return timeRepository.delete(id);
}
}
Loading