Skip to content
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
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
}
Expand Down
31 changes: 31 additions & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#-------------------------------------------------------------------------------#

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이건 어떤 역할을 하는 yaml인가요??

# 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
51 changes: 51 additions & 0 deletions src/main/java/roomescape/Controller/TimeController.java
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();
}
}
57 changes: 57 additions & 0 deletions src/main/java/roomescape/controller/ReservationController.java
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() {

Choose a reason for hiding this comment

The 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));

Choose a reason for hiding this comment

The 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();
}
}
77 changes: 77 additions & 0 deletions src/main/java/roomescape/dao/ReservationDao.java
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) {

Choose a reason for hiding this comment

The 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()
);
}
}
64 changes: 64 additions & 0 deletions src/main/java/roomescape/dao/TimeDao.java
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()
);
}

}
22 changes: 22 additions & 0 deletions src/main/java/roomescape/domain/Reservation.java
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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/roomescape/domain/Time.java
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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/roomescape/dto/request/ReservationRequestDto.java
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) {

Choose a reason for hiding this comment

The 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();
}
}
12 changes: 12 additions & 0 deletions src/main/java/roomescape/dto/request/TimeRequestDto.java
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();
}
}
Loading