-
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 MVC + JDBC + CORE] 이해찬 미션 제출합니다. #369
Open
LHC0312
wants to merge
10
commits into
next-step:main
Choose a base branch
from
LHC0312:main
base: main
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 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c484c44
feat: 일단계 기능 추가
LHC0312 db7048d
feat: 삼단계 기능 추가
LHC0312 2f48888
Merge remote-tracking branch 'origin/main'
LHC0312 0ce729e
feat: 사단계 기능 추가
LHC0312 cb17d8d
feat: 오, 육단계 기능 추가
LHC0312 83407c6
feat: 칠단계 기능 추가
LHC0312 7922dde
Merge remote-tracking branch 'origin/main'
LHC0312 b240cc9
feat: 팔단계 기능 추가
LHC0312 befa033
feat: 십단계 기능 추가(에러있음)
LHC0312 f1d6521
FIX: 10단계 기능 완성
LHC0312 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
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,15 @@ | ||
package roomescape.Controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@Controller | ||
public class HomeController { | ||
|
||
@GetMapping("/") | ||
public String home() { | ||
return "home"; | ||
} | ||
|
||
} | ||
|
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,48 @@ | ||
package roomescape.controller; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
import org.springframework.web.bind.annotation.RequestBody; | ||
import roomescape.converter.TimeConverter; | ||
import roomescape.domain.Time; | ||
import roomescape.dto.TimeRequestDto; | ||
import roomescape.dto.TimeResponseDto; | ||
import roomescape.dto.TimeResponseDto.ResponseDto; | ||
import roomescape.service.TimeService; | ||
|
||
@RequiredArgsConstructor | ||
@Controller | ||
public class TimeController { | ||
|
||
private final TimeService timeService; | ||
|
||
@GetMapping("/times") | ||
public ResponseEntity<List<ResponseDto>> getTimes() { | ||
List<Time> times = timeService.findAll(); | ||
return ResponseEntity.ok().body(TimeConverter.toResponseDto(times)); | ||
} | ||
|
||
@PostMapping("/times") | ||
public ResponseEntity<TimeResponseDto.ResponseDto> creatTime(@RequestBody TimeRequestDto.createDto request) { | ||
|
||
Time time = TimeConverter.toTime(request); | ||
timeService.create(time); | ||
|
||
String uri = "/times/" + time.getId(); | ||
return ResponseEntity.created(URI.create(uri)).body(TimeConverter.toResponseDto(time)); | ||
} | ||
|
||
@DeleteMapping("/times/{id}") | ||
public ResponseEntity<TimeResponseDto.ResponseDto> deleteTime(@PathVariable Long id) { | ||
timeService.delete(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,15 @@ | ||
package roomescape.controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@Controller | ||
public class HomeController { | ||
|
||
@GetMapping("/") | ||
public String home() { | ||
return "home"; | ||
} | ||
|
||
} | ||
|
58 changes: 58 additions & 0 deletions
58
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package roomescape.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import java.net.URI; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import roomescape.converter.ReservationConverter; | ||
import roomescape.domain.Reservation; | ||
import roomescape.dto.ReservationReqeustDto; | ||
import roomescape.dto.ReservationResponseDto; | ||
import roomescape.dto.ReservationResponseDto.ResponseDto; | ||
import roomescape.service.ReservationService; | ||
|
||
@RequiredArgsConstructor | ||
@Controller | ||
public class ReservationController { | ||
|
||
private final ReservationService reservationService; | ||
|
||
@GetMapping("/reservation") | ||
public String reservation() { | ||
return "new-reservation"; | ||
} | ||
|
||
@GetMapping("/reservations") | ||
public ResponseEntity<?> reservations() { | ||
List<Reservation> reservations = reservationService.findAll(); | ||
return ResponseEntity.ok().body(ReservationConverter.toResponseDto(reservations)); | ||
} | ||
|
||
@PostMapping("/reservations") | ||
public ResponseEntity<?> createReservation (@RequestBody @Valid ReservationReqeustDto.CreateReservationDto request) { | ||
LHC0312 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Reservation reservation = reservationService.create(request); | ||
|
||
String url = "/reservations/" + reservation.getId(); | ||
|
||
return ResponseEntity.created(URI.create(url)).body(ReservationConverter.toResponseDto(reservation)); | ||
} | ||
|
||
@DeleteMapping("/reservations/{id}") | ||
public ResponseEntity<Void> createReservation (@PathVariable Long id) { | ||
|
||
Reservation reservation = reservationService.delete(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/roomescape/converter/ReservationConverter.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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package roomescape.converter; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import roomescape.domain.Reservation; | ||
import roomescape.domain.Time; | ||
import roomescape.dto.ReservationReqeustDto; | ||
import roomescape.dto.ReservationResponseDto; | ||
|
||
public class ReservationConverter { | ||
|
||
|
||
public static ReservationResponseDto.ResponseDto toResponseDto(Reservation reservation) { | ||
|
||
|
||
return ReservationResponseDto.ResponseDto.builder() | ||
.id(reservation.getId()) | ||
.name(reservation.getName()) | ||
.date(reservation.getDate()) | ||
.time(reservation.getTime().getTime()) | ||
.build(); | ||
} | ||
|
||
public static List<ReservationResponseDto.ResponseDto> toResponseDto(List<Reservation> reservations) { | ||
return reservations.stream() | ||
.map(ReservationConverter::toResponseDto).collect( | ||
Collectors.toList()); | ||
} | ||
|
||
public static Reservation toReservation(ReservationReqeustDto.CreateReservationDto request, Time time) { | ||
|
||
|
||
return Reservation.builder() | ||
.name(request.getName()) | ||
.date(request.getDate()) | ||
.time(time) | ||
.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,33 @@ | ||
package roomescape.converter; | ||
|
||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import roomescape.domain.Time; | ||
import roomescape.dto.TimeRequestDto; | ||
import roomescape.dto.TimeResponseDto; | ||
|
||
public class TimeConverter { | ||
|
||
public static TimeResponseDto.ResponseDto toResponseDto(Time time) { | ||
|
||
return TimeResponseDto.ResponseDto.builder() | ||
.id(time.getId()) | ||
.time(time.getTime()) | ||
.build(); | ||
} | ||
|
||
public static List<TimeResponseDto.ResponseDto> toResponseDto(List<Time> times) { | ||
return times.stream() | ||
.map(TimeConverter::toResponseDto).collect( | ||
Collectors.toList()); | ||
} | ||
|
||
public static Time toTime(TimeRequestDto.createDto request) { | ||
|
||
return Time.builder() | ||
.time(request.getTime()) | ||
.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,78 @@ | ||
package roomescape.dao; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
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.Reservation; | ||
import roomescape.domain.Time; | ||
import roomescape.service.TimeService; | ||
import roomescape.service.TimeServiceImpl; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ReservationDao { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
private final RowMapper<Reservation> rowMapper = (resultSet, rowNum) -> { | ||
|
||
return Reservation.builder() | ||
.id(resultSet.getLong("r.id")) | ||
.name(resultSet.getString("r.name")) | ||
.date(resultSet.getString("r.date")) | ||
.time( | ||
Time.builder() | ||
.id(resultSet.getLong("t.id")) | ||
.time(resultSet.getString("t.time")) | ||
.build() | ||
) | ||
.build(); | ||
}; | ||
|
||
|
||
public Optional<Reservation> findById(Long id) { | ||
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. Optional도 잘 써주셨군요!! 좋습니다. |
||
return jdbcTemplate.query( | ||
"SELECT r.id, r.name, r.date, t.id, t.time FROM reservation AS r JOIN time AS t ON (r.time_id = t.id AND r.id = ?)", rowMapper, id | ||
).stream().findFirst(); | ||
} | ||
|
||
public List<Reservation> findAll() { | ||
return jdbcTemplate.query( | ||
"SELECT r.id, r.name, r.date, t.id, t.time FROM reservation AS r JOIN time AS t ON r.time_id = t.id", rowMapper | ||
); | ||
} | ||
|
||
public Reservation save(Reservation reservation) { | ||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
|
||
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, reservation.getTime().getId()); | ||
return ps; | ||
}, keyHolder); | ||
Long id = keyHolder.getKey().longValue(); | ||
reservation.generateId(id); | ||
|
||
return reservation; | ||
} | ||
|
||
public void remove(Reservation reservation) { | ||
jdbcTemplate.update( | ||
"DELETE FROM reservation WHERE id = ?", reservation.getId() | ||
); | ||
} | ||
|
||
} |
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,64 @@ | ||
package roomescape.dao; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
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; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class TimeDao { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
private final RowMapper<Time> rowMapper = (resultSet, rowNum) -> { | ||
|
||
return Time.builder() | ||
.id(resultSet.getLong("id")) | ||
.time(resultSet.getString("time")) | ||
.build(); | ||
}; | ||
|
||
|
||
public Optional<Time> findById(Long id) { | ||
return jdbcTemplate.query( | ||
"select id, time from time where id = ?", rowMapper, id | ||
).stream().findFirst(); | ||
} | ||
|
||
public List<Time> findAll() { | ||
return jdbcTemplate.query( | ||
"select id, time from time", rowMapper | ||
); | ||
} | ||
|
||
public Time save(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().toString()); | ||
return ps; | ||
}, keyHolder); | ||
Long id = keyHolder.getKey().longValue(); | ||
time.generateId(id); | ||
|
||
return time; | ||
} | ||
|
||
public void remove(Time time) { | ||
jdbcTemplate.update( | ||
"delete from time where id = ?", time.getId() | ||
); | ||
} | ||
|
||
} |
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.
와일드카드를 사용하신 이유가 있으신가요???