-
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
base: main
Are you sure you want to change the base?
Changes from all commits
c484c44
db7048d
2f48888
0ce729e
cb17d8d
83407c6
7922dde
b240cc9
befa033
f1d6521
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,31 @@ | ||
#-------------------------------------------------------------------------------# | ||
# Qodana analysis is configured by qodana.yaml file # | ||
# https://www.jetbrains.com/help/qodana/qodana-yaml.html # | ||
#-------------------------------------------------------------------------------# | ||
version: "1.0" | ||
|
||
#Specify inspection profile for code analysis | ||
profile: | ||
name: qodana.starter | ||
|
||
#Enable inspections | ||
#include: | ||
# - name: <SomeEnabledInspectionId> | ||
|
||
#Disable inspections | ||
#exclude: | ||
# - name: <SomeDisabledInspectionId> | ||
# paths: | ||
# - <path/where/not/run/inspection> | ||
|
||
projectJDK: 17 #(Applied in CI/CD pipeline) | ||
|
||
#Execute shell command before Qodana execution (Applied in CI/CD pipeline) | ||
#bootstrap: sh ./prepare-qodana.sh | ||
|
||
#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline) | ||
#plugins: | ||
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com) | ||
|
||
#Specify Qodana linter for analysis (Applied in CI/CD pipeline) | ||
linter: jetbrains/qodana-jvm:latest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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.domain.Time; | ||
import roomescape.dto.request.TimeRequestDto; | ||
import roomescape.dto.response.TimeResponseDto; | ||
import roomescape.service.TimeService; | ||
|
||
@RequiredArgsConstructor | ||
@Controller | ||
public class TimeController { | ||
|
||
private final TimeService timeService; | ||
|
||
@GetMapping("/time") | ||
public String time() { | ||
return "time"; | ||
} | ||
|
||
@GetMapping("/times") | ||
public ResponseEntity<List<TimeResponseDto>> getTimes() { | ||
List<Time> times = timeService.findAll(); | ||
return ResponseEntity.ok().body(TimeResponseDto.from(times)); | ||
} | ||
|
||
@PostMapping("/times") | ||
public ResponseEntity<TimeResponseDto> creatTime(@RequestBody TimeRequestDto request) { | ||
|
||
Time time = request.toTime(); | ||
timeService.create(time); | ||
|
||
String uri = "/times/" + time.getId(); | ||
return ResponseEntity.created(URI.create(uri)).body(TimeResponseDto.from(time)); | ||
} | ||
|
||
@DeleteMapping("/times/{id}") | ||
public ResponseEntity<TimeResponseDto> deleteTime(@PathVariable Long id) { | ||
timeService.delete(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 roomescape.domain.Reservation; | ||
import roomescape.domain.Time; | ||
import roomescape.dto.request.ReservationRequestDto; | ||
import roomescape.dto.response.ReservationResponseDto; | ||
import roomescape.service.ReservationService; | ||
import roomescape.service.TimeService; | ||
|
||
@RequiredArgsConstructor | ||
@Controller | ||
public class ReservationController { | ||
|
||
private final ReservationService reservationService; | ||
private final TimeService timeService; | ||
|
||
@GetMapping("/reservation") | ||
public String reservation() { | ||
return "new-reservation"; | ||
} | ||
|
||
@GetMapping("/reservations") | ||
public ResponseEntity<?> reservations() { | ||
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. 와일드카드를 사용하신 이유가 있으신가요??? |
||
List<Reservation> reservations = reservationService.findAll(); | ||
return ResponseEntity.ok().body(ReservationResponseDto.from(reservations)); | ||
} | ||
|
||
@PostMapping("/reservations") | ||
public ResponseEntity<?> createReservation (@RequestBody @Valid ReservationRequestDto request) { | ||
|
||
Time time = timeService.findById(request.time()); | ||
Reservation reservation = reservationService.create(request.toReservation(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. 요런 방식도 괜찮은 것 같네요 |
||
|
||
String url = "/reservations/" + reservation.getId(); | ||
|
||
return ResponseEntity.created(URI.create(url)).body(ReservationResponseDto.from(reservation)); | ||
} | ||
|
||
@DeleteMapping("/reservations/{id}") | ||
public ResponseEntity<Void> createReservation (@PathVariable Long id) { | ||
|
||
reservationService.delete(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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("reservation_id")) | ||
.name(resultSet.getString("name")) | ||
.date(resultSet.getString("date")) | ||
.time( | ||
Time.builder() | ||
.id(resultSet.getLong("time_id")) | ||
.time(resultSet.getString("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 AS reservation_id, r.name, r.date, t.id AS time_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 AS reservation_id, r.name, r.date, t.id AS time_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() | ||
); | ||
} | ||
} |
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() | ||
); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package roomescape.domain; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
|
||
@Builder | ||
@Getter | ||
public class Reservation { | ||
|
||
private Long id; | ||
private String name; | ||
private String date; | ||
private Time time; | ||
|
||
public void generateId(Long id) { | ||
this.id = id; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package roomescape.domain; | ||
|
||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
|
||
@Getter | ||
@Builder | ||
public class Time { | ||
private Long id; | ||
private String time; | ||
private List<Reservation> reservations; | ||
|
||
public void generateId(Long id) { | ||
this.id = id; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package roomescape.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.annotation.JsonFormat.Shape; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import roomescape.domain.Reservation; | ||
import roomescape.domain.Time; | ||
|
||
public record ReservationRequestDto( | ||
|
||
@NotBlank | ||
String name, | ||
|
||
@NotBlank | ||
String date, | ||
|
||
@NotNull | ||
@JsonFormat(shape = Shape.NUMBER_INT) | ||
Long time | ||
|
||
) { | ||
|
||
public Reservation toReservation(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. 오 요런식으로 dto에서 도메인 객체 만드는 거 좋은 것 같습니다. |
||
return Reservation.builder() | ||
.name(this.name()) | ||
.date(this.date()) | ||
.time(time) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package roomescape.dto.request; | ||
|
||
import roomescape.domain.Time; | ||
|
||
public record TimeRequestDto( String time) { | ||
public Time toTime() { | ||
|
||
return Time.builder() | ||
.time(time()) | ||
.build(); | ||
} | ||
} |
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.
오 이건 어떤 역할을 하는 yaml인가요??