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

Feature/2단계 예외처리및리팩터링 #109

Open
wants to merge 12 commits into
base: koreacat
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-validation'

Choose a reason for hiding this comment

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

spring validation을 적용해주셨군요 👍

runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/roomescape/controller/AdminPageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package roomescape.controller;

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

@Controller
@RequestMapping("/admin")
public class AdminPageController {
@GetMapping("/reservation")
public String reservation() {
return "/admin/reservation-new";
}

@GetMapping("/time")
public String time() {
return "/admin/time";
}

@GetMapping("/theme")
public String theme() {
return "/admin/theme";
}
}
11 changes: 0 additions & 11 deletions src/main/java/roomescape/controller/PageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,4 @@ public class PageController {
public String home() {
return "/admin/index";
}

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

@GetMapping("/admin/time")
public String time() {
return "/admin/time";
}

}
12 changes: 5 additions & 7 deletions src/main/java/roomescape/controller/ReservationController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package roomescape.controller;

import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import roomescape.dto.ReservationRq;
import roomescape.dto.ReservationRs;
import roomescape.service.ReservationService;

import java.net.URI;
import java.util.List;

@RestController
Expand All @@ -17,25 +19,21 @@ public ReservationController(ReservationService reservationService) {
this.reservationService = reservationService;
}

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

// 예약 추가
@PostMapping
public ResponseEntity<ReservationRs> addReservation(@RequestBody ReservationRq reservationRq) {
public ResponseEntity<ReservationRs> addReservation(@RequestBody @Valid ReservationRq reservationRq) {
ReservationRs reservationRs = reservationService.addReservation(reservationRq);
return ResponseEntity.ok().body(reservationRs);
return ResponseEntity.created(URI.create("/reservations/" + reservationRs.getId())).body(reservationRs);

Choose a reason for hiding this comment

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

Http Status Code 를 적절히 잘 활용해주셨네요 👍

}

// 예약 취소
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteReservation(@PathVariable Long id) {
reservationService.deleteReservation(id);
return ResponseEntity.ok().build();
return ResponseEntity.noContent().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import roomescape.dto.ReservationTimeRs;
import roomescape.service.ReservationTimeService;

import java.net.URI;
import java.util.List;

@RestController
Expand All @@ -28,14 +29,14 @@ public ResponseEntity<List<ReservationTimeRs>> getReservationTimes() {
@PostMapping
public ResponseEntity<ReservationTimeRs> addReservationTime(@RequestBody ReservationTimeRq reservationTimeRq) {
ReservationTimeRs reservationTimeRs = reservationTimeService.addReservationTime(reservationTimeRq);
return ResponseEntity.ok().body(reservationTimeRs);
return ResponseEntity.created(URI.create("/times/" + reservationTimeRs.getId())).body(reservationTimeRs);
}

// 시간 삭제
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteReservationTime(@PathVariable Long id) {
reservationTimeService.deleteReservationTime(id);
return ResponseEntity.ok().build();
return ResponseEntity.noContent().build();
}

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

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import roomescape.dto.ThemeRq;
import roomescape.dto.ThemeRs;
import roomescape.service.ThemeService;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/themes")
public class ThemeController {
private final ThemeService themeService;

public ThemeController(ThemeService themeService) {
this.themeService = themeService;
}

// 테마 조회
@GetMapping
public ResponseEntity<List<ThemeRs>> getThemes() {
List<ThemeRs> themes = themeService.getAllThemes();
return ResponseEntity.ok().body(themes);
}

// 테마 추가
@PostMapping
public ResponseEntity<ThemeRs> addTheme(@RequestBody ThemeRq themeRq) {
ThemeRs themeRs = themeService.addTheme(themeRq);
return ResponseEntity.created(URI.create("/themes/" + themeRs.getId())).body(themeRs);
}

// 테마 삭제
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteTheme(@PathVariable Long id) {
themeService.deleteTheme(id);
return ResponseEntity.noContent().build();
}

}
47 changes: 0 additions & 47 deletions src/main/java/roomescape/dto/ReservationRequest.java

This file was deleted.

32 changes: 28 additions & 4 deletions src/main/java/roomescape/dto/ReservationRq.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package roomescape.dto;

import java.time.LocalDate;

import jakarta.validation.constraints.Future;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

public class ReservationRq {
@NotEmpty(message = "Reservation name must not be empty")

Choose a reason for hiding this comment

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

문자열 검증에는 @NotNull, @NotEmpty, @NotBlank` 를 사용할 수 있는데,
각 어노테이션의 차이는 무엇일까요~?

private String name;
private String date;

@NotNull(message = "Reservation date must not be null")
@Future(message = "Reservation date must be in the future")
private LocalDate date;

@NotNull(message = "Reservation time ID must not be null")
private Long timeId;

@NotNull(message = "Theme ID must not be null")
private Long themeId;

public ReservationRq() {}

public ReservationRq(String name, String date, Long timeId) {
public ReservationRq(String name, LocalDate date, Long timeId, Long themeId) {
this.name = name;
this.date = date;
this.timeId = timeId;
this.themeId = themeId;
}

public String getName() {
Expand All @@ -21,11 +37,11 @@ public void setName(String name) {
this.name = name;
}

public String getDate() {
public LocalDate getDate() {
return date;
}

public void setDate(String date) {
public void setDate(LocalDate date) {
this.date = date;
}

Expand All @@ -36,4 +52,12 @@ public Long getTimeId() {
public void setTimeId(Long timeId) {
this.timeId = timeId;
}

public Long getThemeId() {
return themeId;
}

public void setThemeId(Long themeId) {
this.themeId = themeId;
}
}
21 changes: 17 additions & 4 deletions src/main/java/roomescape/dto/ReservationRs.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package roomescape.dto;

import roomescape.model.ReservationTime;
import roomescape.model.Theme;

import java.time.LocalDate;

public class ReservationRs {
private Long id;
private String name;
private String date;
private LocalDate date;
private ReservationTime time;
private Theme theme;

public ReservationRs() {}

public ReservationRs(Long id, String name, String date, ReservationTime time) {
public ReservationRs(Long id, String name, LocalDate date, ReservationTime time, Theme theme) {
this.id = id;
this.name = name;
this.date = date;
this.time = time;
this.theme = theme;
}

public Long getId() {
Expand All @@ -33,11 +38,11 @@ public void setName(String name) {
this.name = name;
}

public String getDate() {
public LocalDate getDate() {
return date;
}

public void setDate(String date) {
public void setDate(LocalDate date) {
this.date = date;
}

Expand All @@ -48,4 +53,12 @@ public ReservationTime getTime() {
public void setTime(ReservationTime time) {
this.time = time;
}

public Theme getTheme() {
return theme;
}

public void setTheme(Theme theme) {
this.theme = theme;
}
}
10 changes: 6 additions & 4 deletions src/main/java/roomescape/dto/ReservationTimeRq.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package roomescape.dto;

import java.time.LocalTime;

public class ReservationTimeRq {
private String startAt;
private LocalTime startAt;

public ReservationTimeRq() {}

public ReservationTimeRq(String startAt) {
public ReservationTimeRq(LocalTime startAt) {
this.startAt = startAt;
}

public String getStartAt() {
public LocalTime getStartAt() {
return startAt;
}

public void setStartAt(String startAt) {
public void setStartAt(LocalTime startAt) {
this.startAt = startAt;
}
}
10 changes: 6 additions & 4 deletions src/main/java/roomescape/dto/ReservationTimeRs.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package roomescape.dto;

import java.time.LocalTime;

public class ReservationTimeRs {
private Long id;
private String startAt;
private LocalTime startAt;

public ReservationTimeRs() {}

public ReservationTimeRs(Long id, String startAt) {
public ReservationTimeRs(Long id, LocalTime startAt) {
this.id = id;
this.startAt = startAt;
}
Expand All @@ -19,11 +21,11 @@ public void setId(Long id) {
this.id = id;
}

public String getStartAt() {
public LocalTime getStartAt() {
return startAt;
}

public void setStartAt(String startAt) {
public void setStartAt(LocalTime startAt) {
this.startAt = startAt;
}
}
Loading