-
Notifications
You must be signed in to change notification settings - Fork 117
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
base: anhye0n
Are you sure you want to change the base?
Changes from 4 commits
ce7cb7f
aa41cbe
50a9c9c
664fe38
7c34c2d
1dc190a
c8e97c4
d357ab3
e2f17e7
e8d18c5
ff9df48
a1631a9
80a433a
5490e90
0c312a6
3c62160
39e0e70
aef90c1
6af2aa5
83844b5
08e0494
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,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 | ||
) { | ||
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(); | ||
} | ||
} |
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); | ||
} | ||
} |
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; | ||
} | ||
} |
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
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.
이러한 어노테이션은 |
||
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); | ||
} | ||
|
||
} |
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() { | ||
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. 메서드 이름이 수정되어야 할 것 같아요 :) |
||
return timeRepository.findAll(); | ||
} | ||
|
||
public Time inserTime(Time time) { | ||
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. 오타가 있네요! |
||
return timeRepository.save(time); | ||
} | ||
|
||
public int deleteTime(int id) { | ||
return timeRepository.delete(id); | ||
} | ||
} |
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.
@DateTimeFormat
은@RequestBody
에서 동작하지 않는다고 하네요 :)@DateTimeFormat을 이용한 날짜 포매팅