-
Notifications
You must be signed in to change notification settings - Fork 119
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
base: boyekim
Are you sure you want to change the base?
Changes from all commits
a9004ad
b1a40a3
56c15c3
fd13535
95a28af
930df59
479443e
d0ff629
887148c
1ffa569
b4e3901
6073bad
a828dd1
90e4fcb
27e0f5d
e1e3667
cd2f609
76cabe5
ea519fa
403eca5
75f9754
d772917
a23806c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package roomescape.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
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; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class Config { | ||
|
||
private final DataSource dataSource; | ||
|
||
@Bean | ||
public ReservationRepository reservationRepository() { | ||
return new JdbcReservationRepository(dataSource); | ||
} | ||
|
||
@Bean | ||
public TimeRepository timeRepository() { | ||
return new JdbcTimeRepository(dataSource); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package roomescape.controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Controller | ||
public class HomeController { | ||
|
||
@GetMapping("/") | ||
public String home() { | ||
return "home"; | ||
} | ||
|
||
@GetMapping("/reservation") | ||
public String reservationController() { | ||
return "new-reservation"; | ||
} | ||
|
||
@GetMapping("time") | ||
public String timeController() { | ||
return "time"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package roomescape.controller; | ||
|
||
import jakarta.validation.Valid; | ||
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.ReservationRequestDTO; | ||
import roomescape.model.ReservationResponseDTO; | ||
import roomescape.repository.service.ReservationService; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Controller | ||
@RequestMapping("/reservations") | ||
public class ReservationController { | ||
|
||
private final ReservationService reservationService; | ||
|
||
@ResponseBody | ||
@GetMapping | ||
public List<ReservationResponseDTO> allReservationsController() { | ||
List<ReservationResponseDTO> reservations = reservationService.findAll(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기존에는 controller에서 repository를 호출하셔서 사용했는데 이번에는 service 클래스를 만들어서 service로 호출하신 거 구조에 대한 이해를 잘 하고 계신 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다 |
||
log.info("reservations = {}", reservations); | ||
return reservations; | ||
} | ||
|
||
@ResponseBody | ||
@PostMapping | ||
public ResponseEntity<ReservationResponseDTO> reservationAddController(@Valid @RequestBody ReservationRequestDTO reservation) { | ||
ReservationResponseDTO responseDTO = reservationService.reservationAdd(reservation); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
String uri = "/reservations/" + responseDTO.getId(); | ||
headers.setLocation(URI.create(uri)); | ||
ResponseEntity<ReservationResponseDTO> response = new ResponseEntity<>(responseDTO, headers, HttpStatus.CREATED); | ||
|
||
return response; | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteController(@PathVariable int id) { | ||
reservationService.delete(id); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TimeController도 요구사항에 맞춰서 잘 짜신 것 같습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,49 @@ | ||
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.TimeRequestDTO; | ||
import roomescape.model.TimeResponseDTO; | ||
import roomescape.repository.service.TimeService; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Controller | ||
@RequestMapping("times") | ||
public class TimeController { | ||
|
||
private final TimeService timeService; | ||
|
||
@ResponseBody | ||
@GetMapping | ||
public List<TimeResponseDTO> findTime() { | ||
List<TimeResponseDTO> times = timeService.findAll(); | ||
return times; | ||
} | ||
|
||
@ResponseBody | ||
@PostMapping | ||
public ResponseEntity<TimeResponseDTO> timeAdd(@RequestBody TimeRequestDTO timeRequestDTO) { | ||
TimeResponseDTO resultTime = timeService.timeAdd(timeRequestDTO); | ||
HttpHeaders headers = new HttpHeaders(); | ||
String uri = "/times/" + resultTime.getId(); | ||
headers.setLocation(URI.create(uri)); | ||
ResponseEntity<TimeResponseDTO> response = new ResponseEntity<>(resultTime, headers, HttpStatus.CREATED); | ||
return response; | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteController(@PathVariable int id) { | ||
timeService.delete(id); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,26 @@ | ||
package roomescape.model; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class Reservation { | ||
private int id; | ||
|
||
@NotBlank | ||
private String name; | ||
|
||
@NotBlank | ||
private String date; | ||
|
||
private Time time; | ||
|
||
public Reservation(){ | ||
} | ||
|
||
public Reservation(String name, String date, Time time) { | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package roomescape.model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class ReservationRequestDTO { | ||
|
||
private int time; | ||
private String name; | ||
private String date; | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. responseDTO도 만드셨군요! 저는 requestDTO만 생각했는데, 하나 배워갑니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package roomescape.model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class ReservationResponseDTO { | ||
private int id; | ||
|
||
private String name; | ||
|
||
private String date; | ||
|
||
private Time time; | ||
|
||
public ReservationResponseDTO(String name, String date, Time time) { | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
public ReservationResponseDTO(int id, String name, String date, Time time) { | ||
this.id = id; | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
public ReservationResponseDTO() { | ||
} | ||
|
||
public static ReservationResponseDTO makingResponse(Reservation reservation) { | ||
ReservationResponseDTO result = new ReservationResponseDTO(); | ||
result.setId(reservation.getId()); | ||
result.setTime(reservation.getTime()); | ||
result.setName(reservation.getName()); | ||
result.setDate(reservation.getDate()); | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package roomescape.model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Time { | ||
private int id; | ||
private String time; | ||
|
||
public Time() { | ||
} | ||
|
||
public Time(int id, String time) { | ||
this.id = id; | ||
this.time = time; | ||
} | ||
|
||
public Time(String time) { | ||
this.time = time; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package roomescape.model; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class TimeRequestDTO { | ||
private String time; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package roomescape.model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class TimeResponseDTO { | ||
private int id; | ||
private String time; | ||
|
||
public static TimeResponseDTO makingResponse(Time time) { | ||
TimeResponseDTO result = new TimeResponseDTO(); | ||
result.setId(time.getId()); | ||
result.setTime(time.getTime()); | ||
return result; | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,74 @@ | ||
package roomescape.repository; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
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 roomescape.model.Reservation; | ||
import roomescape.model.Time; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
public class JdbcReservationRepository implements ReservationRepository { | ||
private final JdbcTemplate jdbcTemplate; | ||
private final NamedParameterJdbcTemplate namedTemplate; | ||
|
||
private final RowMapper<Reservation> reservationRowMapper = (rs, rowNum) -> { | ||
Reservation reservation = getReservation(rs); | ||
return reservation; | ||
}; | ||
|
||
|
||
public JdbcReservationRepository(DataSource dataSource) { | ||
this.namedTemplate = new NamedParameterJdbcTemplate(dataSource); | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
} | ||
|
||
@Override | ||
public Reservation reservationAdd(Reservation reservation) { | ||
String sql = "insert into reservation (name, date, time_id) values (:name, :date, :time_id)"; | ||
|
||
SqlParameterSource param = new MapSqlParameterSource() | ||
.addValue("name", reservation.getName()) | ||
.addValue("date", reservation.getDate()) | ||
.addValue("time_id", reservation.getTime().getId()); | ||
|
||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
namedTemplate.update(sql, param, keyHolder); | ||
|
||
int key = keyHolder.getKey().intValue(); | ||
reservation.setId(key); | ||
return reservation; | ||
} | ||
|
||
@Override | ||
public List<Reservation> findAll() { | ||
String sql = "select r.id as reservation_id,r.name,r.date," + | ||
"r.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); | ||
} | ||
|
||
@Override | ||
public void delete(int id) { | ||
jdbcTemplate.update("delete from reservation where id = ?", id); | ||
} | ||
|
||
private static Reservation getReservation(ResultSet rs) throws SQLException { | ||
Reservation reservation = new Reservation(); | ||
reservation.setId(rs.getInt("id")); | ||
reservation.setName(rs.getString("name")); | ||
reservation.setDate(rs.getString("date")); | ||
Time time = new Time(rs.getInt("time_id"), rs.getString("time_value")); | ||
reservation.setTime(time); | ||
return reservation; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 time, reservation, home 빼두신 거 좋은 선택인 것 같아요. 좀 더 세부적으로 나누시려고 빼신 것 같은데 맞을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HomeController 클래스 말씀하시는 것 맞을까요? 이외의 컨트롤러에서 RequestMapping을 써서 좀 더 깔끔하게 만들고자 단순히 페이지 뷰 이름만 반환하는 컨트롤러를 따로 분리하였습니다 ㅎㅎ