Skip to content
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 @@ -19,10 +19,16 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'

runtimeOnly 'com.h2database:h2'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public record HolidayResponse(Long id, LocalDate date) {

public static HolidayResponse from(Holiday holiday) {
return new HolidayResponse(
holiday.id(),
holiday.date()
holiday.getId(),
holiday.getDate()
);
}
}
28 changes: 22 additions & 6 deletions src/main/java/roomescape/holiday/domain/Holiday.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package roomescape.holiday.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

public record Holiday(Long id, LocalDate date) {
public Holiday(LocalDate date) {
this(null, date);
}
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Holiday {

public Holiday withId(Long id) {
return new Holiday(id, this.date);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private LocalDate date;

public Holiday(LocalDate date) {
this.date = date;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package roomescape.holiday.repository;

import java.time.LocalDate;
import org.springframework.data.jpa.repository.JpaRepository;
import roomescape.holiday.domain.Holiday;

import java.util.List;
import java.time.LocalDate;

public interface HolidayRepository {
Holiday save(Holiday holiday);
List<Holiday> findAll();
public interface HolidayRepository extends JpaRepository<Holiday, Long> {

boolean existsByDate(LocalDate date);

boolean deleteById(Long id);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public Holiday create(HolidaySaveServiceRequest holiday) {
@Override
@Transactional
public void delete(Long id) {
boolean deleted = holidayRepository.deleteById(id);
if (!deleted) {
if (!holidayRepository.existsById(id)) {
throw new HolidayNotFoundException(id);
}
holidayRepository.deleteById(id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public ResponseEntity<List<ReservationResponse>> getAll() {
return ResponseEntity.ok(body);
}

@GetMapping("/waiting")
public ResponseEntity<List<ReservationResponse>> getWaitings() {
return ResponseEntity.ok(reservationService.getWaitings());
}

@PostMapping
public ResponseEntity<ReservationResponse> create(
@RequestBody @Valid ReservationSaveRequest reservationRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import roomescape.reservation.controller.dto.*;
import roomescape.reservation.repository.dto.ReservationWithRank;
import roomescape.reservation.service.ReservationService;

import java.util.List;

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

private final ReservationService reservationService;
Expand All @@ -19,19 +20,19 @@ public ReservationController(ReservationService reservationService) {
this.reservationService = reservationService;
}

@GetMapping
@GetMapping("/reservations")
public ResponseEntity<List<ReservationWithWaitingOrderResponse>> getAllByName(@RequestParam String name) {
List<ReservationWithWaitingOrderResponse> body = reservationService.getAllByName(name);
return ResponseEntity.ok(body);
}

@DeleteMapping("/{id}")
@DeleteMapping("/reservations/{id}")
public ResponseEntity<Void> cancel(@PathVariable Long id, @RequestParam String name) {
reservationService.cancelForUser(id, name);
return ResponseEntity.noContent().build();
}

@PutMapping("/{id}")
@PutMapping("/reservations/{id}")
public ResponseEntity<ReservationResponse> update(
@PathVariable Long id,
@RequestBody @Valid UserReservationUpdateRequest request,
Expand All @@ -41,11 +42,36 @@ public ResponseEntity<ReservationResponse> update(
return ResponseEntity.ok(body);
}

@PostMapping
@PostMapping("/reservations")
public ResponseEntity<ReservationSaveResponse> create(
@RequestBody @Valid ReservationSaveRequest reservationRequest) {
ReservationSaveResponse body = ReservationSaveResponse.from(
reservationService.create(reservationRequest.toServiceDto()));
return ResponseEntity.status(HttpStatus.CREATED).body(body);
}

@GetMapping("/reservation-mine")
public ResponseEntity<List<ReservationResponse>> findMine(@RequestParam String name) {
List<ReservationResponse> response = reservationService.findMine(name);
return ResponseEntity.ok(response);
}

@GetMapping("/reservation-mine-rank")
public ResponseEntity<List<ReservationWithRank>> findMineWithRank(@RequestParam String name) {
List<ReservationWithRank> response = reservationService.findMineWithRank(name);
return ResponseEntity.ok(response);
}

@PostMapping("/waitings")
public ResponseEntity<ReservationSaveResponse> createWaiting(@RequestBody @Valid ReservationSaveRequest request) {
ReservationSaveResponse response = ReservationSaveResponse.from(
reservationService.requestWaiting(request.toServiceDto()));
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

@DeleteMapping("/waitings/{id}")
public ResponseEntity<Void> cancelWaiting(@PathVariable Long id, @RequestParam String name) {
reservationService.cancelWaiting(id, name);
return ResponseEntity.noContent().build();
}
}
107 changes: 46 additions & 61 deletions src/main/java/roomescape/reservation/domain/Reservation.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,41 @@
package roomescape.reservation.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import roomescape.reservation.exception.ForbiddenRequestException;
import roomescape.theme.domain.Theme;
import roomescape.time.domain.ReservationTime;

import java.time.LocalDateTime;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Reservation {
private final Long id;
private final String name;
private final ReservationTime time;
private final Theme theme;
private final Status status;
private final LocalDateTime createdAt;

public Reservation(String name, ReservationTime time, Theme theme, Status status, LocalDateTime createdAt) {
this(null, name, time, theme, status, createdAt);
}

private Reservation(Long id, String name, ReservationTime time, Theme theme, Status status,
LocalDateTime createdAt) {
this.id = id;
this.name = name;
this.time = time;
this.theme = theme;
this.status = status;
this.createdAt = createdAt;
}

public Reservation withId(Long id) {
return new Reservation(id, this.name, this.time, this.theme, this.status, this.createdAt);
}

public Reservation withTime(ReservationTime time) {
return new Reservation(this.id, this.name, time, this.theme, this.status, this.createdAt);
}

public Reservation withStatus(Status status) {
return new Reservation(this.id, this.name, this.time, this.theme, status, this.createdAt);
}
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

public Reservation promote() {
if (this.status != Status.WAITING) {
throw new IllegalStateException("WAITING ์ƒํƒœ๋งŒ ์˜ˆ์•ฝ์œผ๋กœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.");
}
return new Reservation(id, name, time, theme, Status.RESERVED, createdAt);
}
@Column
private String name;

public Reservation withCreatedAt(LocalDateTime createdAt) {
return new Reservation(this.id, this.name, this.time, this.theme, this.status, createdAt);
}
@ManyToOne
@JoinColumn(name = "time_id")
private ReservationTime time;

public Long getId() {
return id;
}
@ManyToOne
@JoinColumn(name = "theme_id")
private Theme theme;

public String getName() {
return name;
}
@Enumerated(EnumType.STRING)
private Status status;

public ReservationTime getTime() {
return time;
}

public Theme getTheme() {
return theme;
}

public Status getStatus() {
return status;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}
@Column
private LocalDateTime createdAt;

public boolean isReserved() {
return this.status.equals(Status.RESERVED);
Expand All @@ -90,6 +52,14 @@ public void validateOwnedBy(String name) {
}
}

public Reservation(String name, ReservationTime time, Theme theme, Status status, LocalDateTime createdAt) {
this.name = name;
this.time = time;
this.theme = theme;
this.status = status;
this.createdAt = createdAt;
}

public void validateExpired(LocalDateTime dateTime) {
time.validateExpired(dateTime);
}
Expand All @@ -101,4 +71,19 @@ public Long getThemeId() {
public Long getTimeId() {
return time.getId();
}

public void promote() {
if (this.status != Status.WAITING) {
throw new IllegalStateException("WAITING ์ƒํƒœ๋งŒ ์˜ˆ์•ฝ์œผ๋กœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.");
}
status = Status.RESERVED;
}

public void update(ReservationTime newTime) {
time = newTime;
}

public void updateName(String name) {
this.name = name;
}
}
Loading