-
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] 김이화 미션 제출합니다. #309
Open
ihwag719
wants to merge
14
commits into
next-step:ihwag719
Choose a base branch
from
ihwag719:ihwag719-step10
base: ihwag719
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
17cd0d9
1단계
ihwag719 13cd4b3
2단계
ihwag719 63d8d62
3단계
ihwag719 a9d5050
4단계
ihwag719 7a8a744
리팩토링
ihwag719 fdfdee3
리팩터링 및 spring jdbc
ihwag719 04f3a9b
리팩토링
ihwag719 60f812f
8단계
ihwag719 767e6c9
9단계
ihwag719 d2d37fc
10단계
ihwag719 8b9792a
Merge branch 'ihwag719' into ihwag719-step10
ihwag719 2e650e6
리팩토링
ihwag719 6913375
리팩토링
ihwag719 663b475
리팩토링
ihwag719 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 19 additions & 56 deletions
75
src/main/java/roomescape/controller/ReservationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,40 @@ | ||
package roomescape.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.context.request.WebRequest; | ||
import roomescape.exception.InvalidReservationException; | ||
import roomescape.exception.NotFoundReservationException; | ||
import roomescape.model.Reservation; | ||
import roomescape.dto.ReservationRequestDto; | ||
import roomescape.dto.ReservationResponseDto; | ||
import roomescape.service.ReservationService; | ||
|
||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
@Controller | ||
@RestController | ||
@RequestMapping("/reservations") | ||
public class ReservationController { | ||
|
||
private List<Reservation> reservations = new ArrayList<>(); | ||
private final ReservationService reservationService; | ||
|
||
private AtomicLong index = new AtomicLong(1); | ||
|
||
@GetMapping("/reservation") | ||
public String reservation() { | ||
return "reservation"; | ||
@Autowired | ||
public ReservationController(ReservationService reservationService) { | ||
this.reservationService = reservationService; | ||
} | ||
|
||
@GetMapping("/reservations") | ||
@ResponseBody | ||
public List<Reservation> getReservations() { | ||
return reservations; | ||
@GetMapping | ||
public List<ReservationResponseDto> getReservations() { | ||
return reservationService.getAllReservations(); | ||
} | ||
|
||
@PostMapping("/reservations") | ||
@ResponseBody | ||
public ResponseEntity<Reservation> createReservation(@RequestBody Reservation reservation) { | ||
validateReservation(reservation); | ||
|
||
Long id = index.getAndIncrement(); | ||
String name = reservation.getName(); | ||
String date = reservation.getDate(); | ||
String time = reservation.getTime(); | ||
|
||
Reservation newReservation = new Reservation(id, name, date, time); | ||
reservations.add(newReservation); | ||
|
||
return ResponseEntity.created(URI.create("/reservations/" + newReservation.getId())).body(newReservation); | ||
@PostMapping | ||
public ResponseEntity<ReservationResponseDto> createReservation(@RequestBody ReservationRequestDto requestDto) { | ||
ReservationResponseDto responseDto = reservationService.createReservation(requestDto); | ||
return ResponseEntity.created(URI.create("/reservations/" + responseDto.getId())).body(responseDto); | ||
} | ||
|
||
@DeleteMapping("/reservations/{id}") | ||
@ResponseBody | ||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteReservation(@PathVariable Long id) { | ||
boolean removed = reservations.removeIf(r -> r.getId() == id); | ||
if (!removed) { | ||
throw new NotFoundReservationException("예약을 찾을 수 없습니다: " + id); | ||
} | ||
reservationService.deleteReservation(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@ExceptionHandler({InvalidReservationException.class, NotFoundReservationException.class}) | ||
public ResponseEntity<String> handleException(RuntimeException e, WebRequest request) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); | ||
} | ||
|
||
private void validateReservation(Reservation reservation) { | ||
if (reservation.getName() == null || reservation.getName().isEmpty()) { | ||
throw new InvalidReservationException("이름이 필요합니다."); | ||
} | ||
if (reservation.getDate() == null || reservation.getDate().isEmpty()) { | ||
throw new InvalidReservationException("날짜가 필요합니다."); | ||
} | ||
if (reservation.getTime() == null || reservation.getTime().isEmpty()) { | ||
throw new InvalidReservationException("시간이 필요합니다"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package roomescape.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import roomescape.dto.TimeRequestDto; | ||
import roomescape.dto.TimeResponseDto; | ||
import roomescape.service.TimeService; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/times") | ||
public class TimeController { | ||
|
||
private final TimeService timeService; | ||
|
||
@Autowired | ||
public TimeController(TimeService timeService) { | ||
this.timeService = timeService; | ||
} | ||
|
||
@GetMapping | ||
public List<TimeResponseDto> getTimes() { | ||
return timeService.getAllTimes(); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<TimeResponseDto> createTime(@RequestBody TimeRequestDto timeRequestDto) { | ||
TimeResponseDto newTimeDto = timeService.createTime(timeRequestDto); | ||
return ResponseEntity.created(URI.create("/times/" + newTimeDto.getId())).body(newTimeDto); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteTime(@PathVariable Long id) { | ||
timeService.deleteTime(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package roomescape.controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@Controller | ||
@RequestMapping("/reservations") | ||
public class ViewController { | ||
@GetMapping("/reservationpage") | ||
public String reservationPage() { | ||
return "new-reservation"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package roomescape.dao; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
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.exception.NotFoundReservationException; | ||
import roomescape.domain.Reservation; | ||
import roomescape.domain.Time; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.util.List; | ||
|
||
@Repository | ||
public class ReservationDAO { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Autowired | ||
public ReservationDAO(JdbcTemplate jdbcTemplate, TimeDAO timeDAO) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
private final RowMapper<Reservation> rowMapper = (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 List<Reservation> 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, rowMapper); | ||
} | ||
|
||
public Reservation insert(Reservation reservation) { | ||
Time time = reservation.getTime(); | ||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
|
||
jdbcTemplate.update(connection -> { | ||
PreparedStatement ps = connection.prepareStatement( | ||
"INSERT INTO time(time) VALUES (?)", new String[]{"id"}); | ||
ps.setString(1, time.getTime()); | ||
return ps; | ||
}, keyHolder); | ||
|
||
Long timeId = keyHolder.getKey().longValue(); | ||
|
||
jdbcTemplate.update(connection -> { | ||
PreparedStatement ps = connection.prepareStatement( | ||
"INSERT INTO reservation(name, date, time_id) VALUES (?, ?, ?)", new String[]{"id"}); | ||
ps.setString(1, reservation.getName()); | ||
ps.setString(2, reservation.getDate()); | ||
ps.setLong(3, timeId); | ||
return ps; | ||
}, keyHolder); | ||
|
||
Long reservationId = keyHolder.getKey().longValue(); | ||
return new Reservation(reservationId, reservation.getName(), reservation.getDate(), new Time(timeId, time.getTime())); | ||
} | ||
|
||
public int delete(Long id) { | ||
String sql = "DELETE FROM reservation WHERE id = ?"; | ||
int row = jdbcTemplate.update(sql, id); | ||
if (row == 0) { | ||
throw new NotFoundReservationException(id); | ||
} | ||
return row; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package roomescape.dao; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.dao.EmptyResultDataAccessException; | ||
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; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.util.List; | ||
|
||
@Repository | ||
public class TimeDAO { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Autowired | ||
public TimeDAO(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
private final RowMapper<Time> rowMapper = (resultSet, rowNum) -> { | ||
Time time = new Time( | ||
resultSet.getLong("id"), | ||
resultSet.getString("time")); | ||
return time; | ||
}; | ||
|
||
public List<Time> findAllTime() { | ||
String sql = "SELECT id, time FROM time"; | ||
return jdbcTemplate.query(sql, rowMapper); | ||
} | ||
|
||
public Time findByTimeValue(String timeValue) { | ||
String sql = "SELECT id, time FROM time where time = ?"; | ||
try { | ||
return jdbcTemplate.queryForObject(sql, rowMapper, timeValue); | ||
} catch (EmptyResultDataAccessException e) { | ||
return null; | ||
} | ||
} | ||
|
||
public Time insert(Time time) { | ||
|
||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
|
||
jdbcTemplate.update(connection -> { | ||
PreparedStatement ps = connection.prepareStatement( | ||
"INSERT INTO time(time) VALUES (?)", new String[]{"id"}); | ||
ps.setString(1, time.getTime()); | ||
return ps; | ||
}, keyHolder); | ||
|
||
Long id = keyHolder.getKey().longValue(); | ||
return new Time(id, time.getTime()); | ||
} | ||
|
||
public void delete(Long id) { | ||
String sql = "DELETE FROM time WHERE id = ?"; | ||
jdbcTemplate.update(sql, id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package roomescape.domain; | ||
|
||
public class Reservation { | ||
private Long id; | ||
private String name; | ||
private String date; | ||
private Time time; | ||
|
||
public Reservation(Long id, String name, String date, Time time) { | ||
this.id = id; | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
|
||
public Time getTime() { | ||
return time; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package roomescape.domain; | ||
|
||
public class Time { | ||
private Long id; | ||
private String time; | ||
|
||
public Time(Long id, String time) { | ||
this.id = id; | ||
this.time = time; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTime() { | ||
return time; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package roomescape.dto; | ||
|
||
public class ReservationRequestDto { | ||
private String name; | ||
private String date; | ||
private String time; | ||
|
||
public ReservationRequestDto(String name, String date, String time) { | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
|
||
public String getTime() { | ||
return time; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
시간 관리 페이지 Mapping도 있어야 하지 않나용? 없어도 시간 관리 페이지가 나오는지 궁금합니당
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.
Viewcontroller
에 시간 관리 페이지를 작성하지 않아서 추가했습니다!