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 Core] 김연수 미션 제출합니다. #279

Open
wants to merge 2 commits into
base: juanxiu
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
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:3.2.5'

}

test {
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/roomescape/Repository/ReservationRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package roomescape.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
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;

@Repository
public class ReservationRepository {

private JdbcTemplate jdbcTemplate;

public ReservationRepository(final JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

private final RowMapper<Reservation> reservationMapper = (ResultSet rs, int rowNum) -> {
Long id = rs.getLong("reservation_id");
String name = rs.getString("name");
String date = rs.getString("date");
Time time = new Time(rs.getLong("time_id"), rs.getString("time_value"));
return new Reservation(id, name, date, time);
};

public List<Reservation> findAll() {
String query = "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(query, reservationMapper);
}

public Long save(Reservation reservation) {
String query = "INSERT INTO reservation (name, date, time_id) VALUES (?, ?, ?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(query, new String[]{"id"});
ps.setString(1, reservation.getName());
ps.setString(2, reservation.getDate());
ps.setLong(3, reservation.getTime().getId());
return ps;
}, keyHolder);

return keyHolder.getKey().longValue();
}

public void delete(Long reservationId) {
String query = "DELETE FROM reservation WHERE id = ?";
jdbcTemplate.update(query, reservationId);
}

public Reservation findById(Long id) {
String query = "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 " +
"WHERE r.id = ?";
try {
return jdbcTemplate.queryForObject(query, new Object[]{id}, reservationMapper);
} catch (EmptyResultDataAccessException e) {
return null;
}
}
}
54 changes: 54 additions & 0 deletions src/main/java/roomescape/Repository/TimeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package roomescape.Repository;

import java.sql.PreparedStatement;
import java.util.List;
import java.util.Optional;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import roomescape.domain.Time;

@Repository
public class TimeRepository {

private final JdbcTemplate jdbcTemplate;

public TimeRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

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

time.setId(keyHolder.getKey().longValue());
return time;
}

public void deleteById(Long id) {
jdbcTemplate.update("DELETE FROM time WHERE id = ?", id);
}

public List<Time> findAll() {
return jdbcTemplate.query("SELECT * FROM time",
(rs, rowNum) -> new Time(rs.getLong("id"), rs.getString("time")));
}

// findById 추가
public Optional<Time> findById(Long id) {
try {
return Optional.of(jdbcTemplate.queryForObject("SELECT * FROM time WHERE id = ?",
(rs, rowNum) -> new Time(rs.getLong("id"), rs.getString("time")), id));
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}

}

13 changes: 13 additions & 0 deletions src/main/java/roomescape/controller/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package roomescape.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
return ResponseEntity.badRequest().body("잘못된 요청입니다.");
}
}
65 changes: 65 additions & 0 deletions src/main/java/roomescape/controller/ReservationController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package roomescape.controller;


import java.net.URI;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import roomescape.domain.Reservation;
import roomescape.service.ReservationService;

@RestController
@RequestMapping("/reservations")
public class ReservationController {

private final ReservationService reservationService;

public ReservationController(final ReservationService reservationService) {
this.reservationService = reservationService;
}

// 예약 조회
@GetMapping
@ResponseBody
public ResponseEntity<List<Reservation>> getReservations() {
List<Reservation> reservations = reservationService.getAllReservation();
return ResponseEntity.ok(reservations);
}

@GetMapping("/{id}")
@ResponseBody
public ResponseEntity<Reservation> getReservation(@PathVariable Long id) {
return ResponseEntity.ok(reservationService.getReservation(id));
}

// 예약 추가
@PostMapping
@ResponseBody
public ResponseEntity<Reservation> addReservation(@RequestBody ReservationDto reservation) {
Reservation newReservation = reservationService.createReservation(reservation);
URI uri = URI.create("/reservations/" + newReservation.getId());
return ResponseEntity.created(uri).body(newReservation);
}


// 예약 삭제
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteReservation(@PathVariable Long id) {
boolean isDeleted = reservationService.deleteReservation(id);

if (isDeleted) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Reservation not found.");
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/roomescape/controller/ReservationDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package roomescape.controller;

public record ReservationDto(
String date,
String name,
Long time
) {


}
23 changes: 23 additions & 0 deletions src/main/java/roomescape/controller/RoomEscapeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package roomescape.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class RoomEscapeController {

@GetMapping("/reservation")
public String reservation() {
return "new-reservation";
}

@GetMapping("/")
public String index() {
return "home";
}

@GetMapping("/time")
public String time() {
return "time";
}
}
50 changes: 50 additions & 0 deletions src/main/java/roomescape/controller/TimeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package roomescape.controller;

import java.net.URI;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import roomescape.Repository.TimeRepository;
import roomescape.domain.Time;

@RestController
@RequestMapping("/times")
public class TimeController {

private final TimeRepository timeRepository;

public TimeController(TimeRepository timeRepository) {
this.timeRepository = timeRepository;
}

// 시간데이터 조회
@GetMapping
public ResponseEntity<List<Time>> getTime() {
List<Time> times = timeRepository.findAll();
return ResponseEntity.ok(times);
}

// 시간 데이터 추가
@PostMapping
public ResponseEntity<Time> addTime(@RequestBody Time time) {
Time savedTime = timeRepository.save(time);
URI uri = URI.create("/times/" + savedTime.getId());
return ResponseEntity.created(uri).body(savedTime);
}

// 시간 데이터 삭제
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteTime(@PathVariable Long id) {
timeRepository.deleteById(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("시간 데이터가 삭제되었습니다.");
}
}
38 changes: 38 additions & 0 deletions src/main/java/roomescape/domain/Reservation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package roomescape.domain;


public class Reservation {
private Long id;
private String name;
private String date;
private Time time; // 객체(object)로 jackson lib가 인식한다.

public Reservation(Long id, String name, String date, Time time) {
this.id = id;
this.name = name;
this.date = date;
this.time = time;
}
public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getDate() {
return date;
}

public Time getTime() {
return time;
}

public void setId(Long id) {
this.id = id;
}

public void setTime(Time time) {
}
}
26 changes: 26 additions & 0 deletions src/main/java/roomescape/domain/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package roomescape.domain;

public class Time {

private Long id;
private String time;

public Time(Long id, String time){
this.id = id;
this.time = time;
}
public Long getId(){
return id;
}

public String getTime(){
return time;
}

public void setId(Long id) {
this.id = id;
}
public void setTime(String time){
this.time = time;
}
}
Loading