-
Notifications
You must be signed in to change notification settings - Fork 134
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] 김의진 미션 제출합니다. #392
base: sansan20535
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -6,45 +6,46 @@ | |
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import roomescape.api.reservations.dto.request.ReservationRegisterRequest; | ||
import roomescape.api.reservations.dto.response.ReservationResponse; | ||
import roomescape.api.reservations.service.ReservationsService; | ||
import roomescape.db.entity.ReservationsEntity; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/reservations") | ||
@RequiredArgsConstructor | ||
public class ReservationsController { | ||
public class ReservationController { | ||
|
||
private final ReservationsService reservationsService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<ReservationsEntity>> getReservations() { | ||
return ResponseEntity.status(HttpStatus.OK).body(reservationsService.getReservations()); | ||
public ResponseEntity<List<ReservationResponse>> getReservations() { | ||
return ResponseEntity.status(HttpStatus.OK) | ||
.body(reservationsService.getReservations()); | ||
} | ||
|
||
Comment on lines
+22
to
26
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. 저희 회사에서 가장 많이 보는 코드 형태네요 👍 |
||
@PostMapping | ||
public ResponseEntity<Void> createReservation( | ||
public ResponseEntity<ReservationResponse> createReservation( | ||
@RequestBody @Valid final ReservationRegisterRequest reservationRegisterRequest | ||
) { | ||
|
||
final long reservationsId = reservationsService.createReservations( | ||
final ReservationResponse reservationResponse = reservationsService.createReservations( | ||
reservationRegisterRequest.name(), | ||
reservationRegisterRequest.date(), | ||
reservationRegisterRequest.time() | ||
reservationRegisterRequest.timeId() | ||
); | ||
|
||
return ResponseEntity.created(URI.create("/reservations/" + reservationsId)).build(); | ||
return ResponseEntity.status(HttpStatus.CREATED) | ||
.header("Location", "/reservations/" + reservationResponse.id()) | ||
.body(reservationResponse); | ||
} | ||
|
||
@DeleteMapping("/{reservationId}") | ||
public ResponseEntity<Void> deleteReservation( | ||
@PathVariable(name = "reservationId") final long reservationId | ||
) { | ||
|
||
reservationsService.deleteReservations(reservationId); | ||
|
||
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); | ||
return ResponseEntity.status(HttpStatus.NO_CONTENT) | ||
.body(null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
package roomescape.api.reservations.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record ReservationRegisterRequest( | ||
@NotBlank(message = "날짜가 비어있습니다.") | ||
String date, | ||
@NotBlank(message = "이름이 비어있습니다.") | ||
String name, | ||
@NotBlank(message = "시간이 비어있습니다.") | ||
String time | ||
@NotNull(message = "시간이 비어있습니다.") | ||
Long timeId | ||
Comment on lines
+11
to
+12
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. 약간 궁금한 부분인데요 |
||
) { | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package roomescape.api.reservations.dto.response; | ||
|
||
public record ReservationResponse( | ||
long id, | ||
String name, | ||
String date, | ||
String time | ||
) { | ||
|
||
public static ReservationResponse of(final long id, final String name, final String date, final String time) { | ||
return new ReservationResponse(id, name, date, time); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,44 @@ | ||
package roomescape.api.reservations.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.support.GeneratedKeyHolder; | ||
import org.springframework.jdbc.support.KeyHolder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import roomescape.db.entity.ReservationsEntity; | ||
import roomescape.api.reservations.dto.response.ReservationResponse; | ||
import roomescape.db.reservation.dao.ReservationDao; | ||
import roomescape.db.reservation.entity.ReservationEntity; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.Statement; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReservationsService { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
private final ReservationDao reservationDao; | ||
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. 아마 요구사항에 dao 와 repository 를 구분하라는 내용이 있었던 것 같은데요 이 부분이 맞을까요? (제가 정확하게 몰라서 잘못 알고 있을 수도 있을 것 같아요) |
||
|
||
@Transactional(readOnly = true) | ||
public List<ReservationsEntity> getReservations() { | ||
final String sql = "SELECT id, name, date, time FROM reservation"; | ||
|
||
return jdbcTemplate.query( | ||
sql, (resultSet, rowNum) -> ReservationsEntity.builder() | ||
.id(resultSet.getLong("id")) | ||
.name(resultSet.getString("name")) | ||
.date(resultSet.getString("date")) | ||
.time(resultSet.getString("time")) | ||
.build()); | ||
public List<ReservationResponse> getReservations() { | ||
return reservationDao.getReservations().stream() | ||
.map(reservationsEntity -> ReservationResponse.of( | ||
reservationsEntity.getId(), | ||
reservationsEntity.getName(), | ||
reservationsEntity.getDate(), | ||
reservationsEntity.getTime().getTime())) | ||
.toList(); | ||
} | ||
|
||
@Transactional | ||
public long createReservations(final String name, final String date, final String time) { | ||
final String sql = "INSERT INTO reservation(name, date, time) VALUES (?, ?, ?)"; | ||
final KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
|
||
jdbcTemplate.update(connection -> { | ||
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); | ||
ps.setString(1, name); | ||
ps.setString(2, date); | ||
ps.setString(3, time); | ||
return ps; | ||
}, keyHolder); | ||
|
||
return keyHolder.getKey().longValue(); | ||
public ReservationResponse createReservations(final String name, final String date, final Long timeId) { | ||
final ReservationEntity reservationEntity = reservationDao.createReservations(name, date, timeId); | ||
return ReservationResponse.of( | ||
reservationEntity.getId(), | ||
reservationEntity.getName(), | ||
reservationEntity.getDate(), | ||
reservationEntity.getTime().getTime() | ||
); | ||
} | ||
|
||
@Transactional | ||
public void deleteReservations(final long reservationId) { | ||
final String sql = "DELETE FROM reservation WHERE id = ?"; | ||
|
||
jdbcTemplate.update(sql, reservationId); | ||
reservationDao.deleteReservations(reservationId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package roomescape.api.times.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import roomescape.api.times.dto.request.TimeRegisterRequest; | ||
import roomescape.api.times.dto.response.TimeResponse; | ||
import roomescape.api.times.service.TimeService; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/times") | ||
@RequiredArgsConstructor | ||
public class TimeController { | ||
|
||
private final TimeService timeService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<TimeResponse>> getTimes() { | ||
return ResponseEntity.status(HttpStatus.OK) | ||
.body(timeService.getTimes()); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<TimeResponse> createTime( | ||
@RequestBody final TimeRegisterRequest timeRegisterRequest | ||
) { | ||
final TimeResponse timeResponse = timeService.createTime( | ||
timeRegisterRequest.time() | ||
); | ||
return ResponseEntity.status(HttpStatus.CREATED) | ||
.header("Location", "/times/" + timeResponse.id()) | ||
.body(timeResponse); | ||
} | ||
|
||
@DeleteMapping("/{timeId}") | ||
public ResponseEntity<Void> deleteTime( | ||
@PathVariable(name = "timeId") final long timeId | ||
) { | ||
timeService.deleteTime(timeId); | ||
return ResponseEntity.status(HttpStatus.NO_CONTENT) | ||
.body(null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package roomescape.api.times.dto.request; | ||
|
||
public record TimeRegisterRequest( | ||
String time | ||
) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package roomescape.api.times.dto.response; | ||
|
||
public record TimeResponse( | ||
long id, | ||
String time | ||
) { | ||
|
||
public static TimeResponse of(final long id, final String time) { | ||
return new TimeResponse(id, time); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package roomescape.api.times.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import roomescape.api.times.dto.response.TimeResponse; | ||
import roomescape.db.time.dao.TimeDao; | ||
import roomescape.db.time.entity.TimeEntity; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TimeService { | ||
|
||
private final TimeDao timeDao; | ||
|
||
@Transactional(readOnly = true) | ||
public List<TimeResponse> getTimes() { | ||
return timeDao.getTimes().stream() | ||
.map(timeEntity -> TimeResponse.of(timeEntity.getId(), timeEntity.getTime())) | ||
.toList(); | ||
} | ||
|
||
@Transactional | ||
public TimeResponse createTime(final String time) { | ||
final TimeEntity timeEntity = timeDao.createTime(time); | ||
return TimeResponse.of(timeEntity.getId(), timeEntity.getTime()); | ||
} | ||
|
||
@Transactional | ||
public void deleteTime(final long timeId) { | ||
timeDao.deleteTime(timeId); | ||
} | ||
} |
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.
요런 부분은 [x] 로 체크 표시를 할 수 있어요