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] 박소정 미션 제출합니다. #310

Open
wants to merge 8 commits into
base: sojeong0202
Choose a base branch
from
66 changes: 0 additions & 66 deletions src/main/java/roomescape/ReservationController.java

This file was deleted.

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

import java.net.URI;
import java.util.List;
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.dto.ReservationResponseDto;
import roomescape.dto.ReservationSaveRequestDto;
import roomescape.service.ReservationService;

@Controller
public class ReservationController {

private final ReservationService reservationService;

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

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

Choose a reason for hiding this comment

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

viewcontroller를 만들어서 매핑을 해줘서 뭐가 좋을지 찾아봤는데, 각각의 장단점이 있는 것 같더군요.

ViewController를 따로 만드는 경우

장점

  • 분리된 책임: View 관련 로직과 API 관련 로직을 분리하여 관리하기 쉬워집니다.
  • 유지보수 용이: 코드의 책임이 분리되므로 특정 부분의 코드만 변경해도 되며, 변경의 영향 범위가 작아집니다.
  • 가독성: 코드가 분리되므로 각 클래스가 더 짧아지고 가독성이 향상됩니다.

단점

  • 파일 수 증가: 클래스를 더 많이 만들어야 하므로 파일 수가 증가합니다.
  • 간단한 프로젝트에서는 오버헤드

기존 Controller에 코드를 추가하는 경우

장점

  • 간단한 구조: 모든 관련 로직이 하나의 클래스에 있어 단순합니다.
  • 빠른 개발: 작은 프로젝트에서는 빠르게 개발할 수 있습니다.

단점

  • 책임 과부하: 하나의 클래스가 너무 많은 책임을 가지게 되어 복잡도가 증가할 수 있습니다.
  • 유지보수 어려움: 코드가 길어지고 복잡해지면서 유지보수가 어려워질 수 있습니다.
  • 가독성 저하: 다양한 로직이 섞이면서 가독성이 떨어질 수 있습니다.

프로젝트의 크기에 따라 viewcontroller를 만드는 것도 고려해보면 좋을 것 같습니다!


@PostMapping("/reservations")
public ResponseEntity<ReservationResponseDto> createReservation(@RequestBody ReservationSaveRequestDto requestDto) {
ReservationResponseDto reservationResponseDto = reservationService.createReservation(requestDto);
return ResponseEntity.created(URI.create("/reservations/" + reservationResponseDto.getId()))
.body(reservationResponseDto);
}

@GetMapping("/reservations")
public ResponseEntity<List<ReservationResponseDto>> readAllReservations() {

return ResponseEntity.ok().body(reservationService.getAllReservations());
}

@DeleteMapping("/reservations/{id}")
public ResponseEntity<Void> deleteReservation(@PathVariable Long id) {
reservationService.deleteReservation(id);

return ResponseEntity.noContent().build();
}
}
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.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.dto.TimeResponseDto;
import roomescape.dto.TimeSaveRequestDto;
import roomescape.service.TimeService;

@Controller
public class TimeController {

private final TimeService timeService;

public TimeController(TimeService timeService) {
this.timeService = timeService;
}

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

@PostMapping("/times")
public ResponseEntity<TimeResponseDto> createTime(@RequestBody TimeSaveRequestDto requestDto) {
TimeResponseDto timeResponseDto = timeService.createTime(requestDto);

return ResponseEntity.created(URI.create("/times/" + timeResponseDto.getId()))
.body(timeResponseDto);
}

@GetMapping("/times")
public ResponseEntity<List<TimeResponseDto>> readAllTimes() {

return ResponseEntity.ok().body(timeService.readAllTimes());
}

@DeleteMapping("/times/{id}")
public ResponseEntity<Void> deleteTime(@PathVariable Long id) {

Choose a reason for hiding this comment

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

@PathVariable 사용 👍

timeService.deleteTime(id);

return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package roomescape;
package roomescape.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -12,9 +12,9 @@ public class Reservation {
private Long id;
private String name;
private String date;
private String time;
private Time time;

public Reservation(String name, String date, String time) {
public Reservation(String name, String date, Time time) {
this.name = name;
this.date = date;
this.time = time;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/roomescape/domain/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package roomescape.domain;

public class Time {

private Long id;
private String time;

public Time() {
}

public Time(String time) {
this.time = time;
}

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

public Long getId() {
return id;
}

public String getTime() {
return time;
}
}
Comment on lines +3 to +27

Choose a reason for hiding this comment

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

기본 생성자의 접근 지정자가 public이라서 time 필드 없이 생성은 가능한데 time 필드를 채워주는 메서드가 없는 것 같아요! time 필드를 채워주는 메서드(setter)를 만드는 것보단 접근 지정자를 protected나 private으로 정상적으로 사용될 수 없는 객체가 만들어지는 경우는 의도적으로 막을 수 있을 것 같아요!

Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
package roomescape;
package roomescape.dto;

import roomescape.domain.Reservation;

public class ReservationResponseDto {

private final Long id;
private final String name;
private final String date;
private final String time;
private Long id;
private String name;
private String date;
private TimeResponseDto time;

public ReservationResponseDto(Reservation reservation) {
this.id = reservation.getId();
this.name = reservation.getName();
this.date = reservation.getDate();
this.time = reservation.getTime();
this.time = new TimeResponseDto(reservation.getTime());
}

public ReservationResponseDto(Long id, Reservation reservation) {
this.id = id;
this.name = reservation.getName();
this.date = reservation.getDate();
this.time = reservation.getTime();
this.time = new TimeResponseDto(reservation.getTime());
}
Comment on lines 12 to 24
Copy link

@shinheekim shinheekim Jul 26, 2024

Choose a reason for hiding this comment

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

생성자의 역할이 어느정도 비슷하고 중복이 되는데 두 번째 생성자에서

public ReservationResponseDto(Long id, Reservation reservation) {
    this(reservation);
    this.id = id;
}

이렇게 수정해보는 건 어떨까 싶어요! 소정은 어떻게 생각하시나용?


public Long getId() {
return this.id;
return id;
}

public String getName() {
return this.name;
return name;
}

public String getDate() {
return this.date;
return date;
}

public String getTime() {
return this.time;
public TimeResponseDto getTime() {
return time;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package roomescape;
package roomescape.dto;

public class ReservationSaveRequestDto {

private String name;
private String date;
private String time;
private Long time;

public ReservationSaveRequestDto() {
}

public String getName() {
return this.name;
return name;
}

public String getDate() {
return this.date;
return date;
}

public String getTime() {
return this.time;
public Long getTime() {
return time;
}
}
27 changes: 27 additions & 0 deletions src/main/java/roomescape/dto/TimeResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package roomescape.dto;

import roomescape.domain.Time;

public class TimeResponseDto {

private Long id;
private String time;

public TimeResponseDto(Time time) {
this.id = time.getId();
this.time = time.getTime();
}

public TimeResponseDto(Long id, Time time) {
this.id = id;
this.time = time.getTime();
}

public Long getId() {
return id;
}

public String getTime() {
return time;
}
}
13 changes: 13 additions & 0 deletions src/main/java/roomescape/dto/TimeSaveRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package roomescape.dto;

public class TimeSaveRequestDto {

private String time;

public TimeSaveRequestDto() {
}

public String getTime() {
return time;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package roomescape;
package roomescape.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand Down
Loading