-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
224 additions
and
105 deletions.
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
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
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,74 @@ | ||
package roomescape.dao; | ||
|
||
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.model.Reservation; | ||
import roomescape.model.ReservationRequest; | ||
import roomescape.model.Time; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.util.List; | ||
|
||
@Repository | ||
public class ReservationDao { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
private final RowMapper<Reservation> reservationRowMapper = (rs, rowNum) -> new Reservation( | ||
rs.getLong("reservation_id"), | ||
rs.getString("name"), | ||
rs.getString("date"), | ||
new Time(rs.getLong("time_id"), rs.getString("time_value")) | ||
); | ||
|
||
public ReservationDao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public JdbcTemplate getJdbcTemplate() { | ||
return jdbcTemplate; | ||
} | ||
|
||
public List<Reservation> findAll() { | ||
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, reservationRowMapper); | ||
} | ||
|
||
public Reservation save(ReservationRequest request) { | ||
String sql = "INSERT INTO reservation (name, date, time_id) VALUES (?, ?, ?)"; | ||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
|
||
jdbcTemplate.update(connection -> { | ||
PreparedStatement ps = connection.prepareStatement(sql, new String[]{"id"}); | ||
ps.setString(1, request.getName()); | ||
ps.setString(2, request.getDate()); | ||
ps.setLong(3, request.getTimeId()); | ||
return ps; | ||
}, keyHolder); | ||
|
||
Long id = keyHolder.getKey().longValue(); | ||
|
||
String timeQuery = "SELECT time FROM time WHERE id = ?"; | ||
String timeValue = jdbcTemplate.queryForObject(timeQuery, String.class, request.getTimeId()); | ||
|
||
return new Reservation(id, request.getName(), request.getDate(), new Time(request.getTimeId(), timeValue)); | ||
} | ||
|
||
public int deleteById(Long id) { | ||
String sql = "DELETE FROM reservation WHERE id = ?"; | ||
return 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,50 @@ | ||
package roomescape.dao; | ||
|
||
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.model.Time; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.util.List; | ||
|
||
@Repository | ||
public class TimeDao { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
private final RowMapper<Time> timeRowMapper = (rs, rowNum) -> new Time( | ||
rs.getLong("id"), | ||
rs.getString("time") | ||
); | ||
|
||
public TimeDao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public List<Time> findAll() { | ||
String sql = "SELECT id, time FROM time"; | ||
return jdbcTemplate.query(sql, timeRowMapper); | ||
} | ||
|
||
public Time save(Time time) { | ||
String sql = "INSERT INTO time (time) VALUES (?)"; | ||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
|
||
jdbcTemplate.update(connection -> { | ||
PreparedStatement ps = connection.prepareStatement(sql, new String[]{"id"}); | ||
ps.setString(1, time.getTime()); | ||
return ps; | ||
}, keyHolder); | ||
|
||
Long id = keyHolder.getKey().longValue(); | ||
return new Time(id, time.getTime()); | ||
} | ||
|
||
public int deleteById(Long id) { | ||
String sql = "DELETE FROM time WHERE id = ?"; | ||
return 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
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
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,35 @@ | ||
package roomescape.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import roomescape.dao.ReservationDao; | ||
import roomescape.exception.NotFoundReservationException; | ||
import roomescape.model.Reservation; | ||
import roomescape.model.ReservationRequest; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class ReservationService { | ||
|
||
private final ReservationDao reservationDao; | ||
|
||
public ReservationService(ReservationDao reservationDao) { | ||
this.reservationDao = reservationDao; | ||
} | ||
|
||
public List<Reservation> getReservations() { | ||
return reservationDao.findAll(); | ||
} | ||
|
||
public Reservation addReservation(ReservationRequest request) { | ||
request.validateAndSetTimeId(reservationDao.getJdbcTemplate()); | ||
return reservationDao.save(request); | ||
} | ||
|
||
public void deleteReservation(Long id) { | ||
int rowsAffected = reservationDao.deleteById(id); | ||
if (rowsAffected == 0) { | ||
throw new NotFoundReservationException("삭제할 예약이 없습니다."); | ||
} | ||
} | ||
} |
Oops, something went wrong.