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 9 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
15 changes: 15 additions & 0 deletions src/main/java/roomescape/Controller/HomeController.java
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";
}

}

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

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(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 src/main/java/roomescape/converter/ReservationConverter.java
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();
}

}
33 changes: 33 additions & 0 deletions src/main/java/roomescape/converter/TimeConverter.java
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();
}

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

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, 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()
);
}

}
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()
);
}

}
Loading