-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat : 예약 선점 로직 구현
- Loading branch information
Showing
17 changed files
with
391 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/prgrms/catchtable/facade/ReservationFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.prgrms.catchtable.facade; | ||
|
||
import com.prgrms.catchtable.reservation.domain.ReservationTime; | ||
import com.prgrms.catchtable.reservation.dto.request.CreateReservationRequest; | ||
import com.prgrms.catchtable.reservation.dto.response.CreateReservationResponse; | ||
import com.prgrms.catchtable.reservation.service.ReservationAsync; | ||
import com.prgrms.catchtable.reservation.service.ReservationService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReservationFacade { | ||
|
||
private final ReservationService reservationService; | ||
private final ReservationAsync reservationAsync; | ||
|
||
public CreateReservationResponse preOccupyReservation( | ||
CreateReservationRequest request) { | ||
ReservationTime reservationTime = reservationService.validateReservationAndSave( | ||
request); | ||
|
||
String shopName = reservationTime.getShop().getName(); | ||
|
||
reservationAsync.setPreOcuppied(reservationTime); | ||
|
||
return CreateReservationResponse.builder() | ||
.shopName(shopName) | ||
.memberName("memberA") | ||
.date(reservationTime.getTime()) | ||
.peopleCount(request.peopleCount()) | ||
.build(); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/prgrms/catchtable/reservation/controller/ReservationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.prgrms.catchtable.reservation.controller; | ||
|
||
import com.prgrms.catchtable.facade.ReservationFacade; | ||
import com.prgrms.catchtable.reservation.dto.request.CreateReservationRequest; | ||
import com.prgrms.catchtable.reservation.dto.response.CreateReservationResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/reservations") | ||
@RequiredArgsConstructor | ||
public class ReservationController { | ||
|
||
private final ReservationFacade reservationFacade; | ||
|
||
@PostMapping | ||
public ResponseEntity<CreateReservationResponse> createReservationResponse( | ||
CreateReservationRequest request) { | ||
return ResponseEntity.ok(reservationFacade.preOccupyReservation(request)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/prgrms/catchtable/reservation/dto/request/CreateReservationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.prgrms.catchtable.reservation.dto.request; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record CreateReservationRequest(Long reservationTimeId, | ||
int peopleCount) { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/prgrms/catchtable/reservation/dto/response/CreateReservationResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.prgrms.catchtable.reservation.dto.response; | ||
|
||
import java.time.LocalDateTime; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record CreateReservationResponse(String shopName, | ||
String memberName, | ||
LocalDateTime date, | ||
int peopleCount) { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/prgrms/catchtable/reservation/repository/ReservationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.prgrms.catchtable.reservation.repository; | ||
|
||
import com.prgrms.catchtable.reservation.domain.Reservation; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ReservationRepository extends JpaRepository<Reservation, Long> { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/prgrms/catchtable/reservation/repository/ReservationTimeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.prgrms.catchtable.reservation.repository; | ||
|
||
import com.prgrms.catchtable.reservation.domain.ReservationTime; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ReservationTimeRepository extends JpaRepository<ReservationTime, Long> { | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/prgrms/catchtable/reservation/service/ReservationAsync.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.prgrms.catchtable.reservation.service; | ||
|
||
import com.prgrms.catchtable.reservation.domain.ReservationTime; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ReservationAsync { | ||
|
||
@Transactional | ||
public void setPreOcuppied(ReservationTime reservationTime) { | ||
reservationTime.reversePreOccupied(); | ||
|
||
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ||
scheduler.schedule(reservationTime::reversePreOccupied, 10, TimeUnit.SECONDS); | ||
|
||
scheduler.shutdown(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/prgrms/catchtable/reservation/service/ReservationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.prgrms.catchtable.reservation.service; | ||
|
||
import static com.prgrms.catchtable.common.exception.ErrorCode.ALREADY_PREOCCUPIED_RESERVATION_TIME; | ||
import static com.prgrms.catchtable.common.exception.ErrorCode.NOT_EXIST_TIME; | ||
|
||
import com.prgrms.catchtable.common.exception.custom.BadRequestCustomException; | ||
import com.prgrms.catchtable.common.exception.custom.NotFoundCustomException; | ||
import com.prgrms.catchtable.reservation.domain.ReservationTime; | ||
import com.prgrms.catchtable.reservation.dto.request.CreateReservationRequest; | ||
import com.prgrms.catchtable.reservation.repository.ReservationTimeRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReservationService { | ||
|
||
private final ReservationTimeRepository reservationTimeRepository; | ||
|
||
@Transactional | ||
public ReservationTime validateReservationAndSave(CreateReservationRequest request) { | ||
ReservationTime reservationTime = reservationTimeRepository.findById( | ||
request.reservationTimeId()) | ||
.orElseThrow(() -> new NotFoundCustomException(NOT_EXIST_TIME)); | ||
|
||
if (reservationTime.isPreOccupied()) { | ||
throw new BadRequestCustomException(ALREADY_PREOCCUPIED_RESERVATION_TIME); | ||
} | ||
|
||
return reservationTime; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/com/prgrms/catchtable/common/data/reservation/ReservationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.prgrms.catchtable.common.data.reservation; | ||
|
||
import static com.prgrms.catchtable.reservation.domain.ReservationStatus.COMPLETED; | ||
|
||
import com.prgrms.catchtable.common.data.shop.ShopData; | ||
import com.prgrms.catchtable.reservation.domain.Reservation; | ||
import com.prgrms.catchtable.reservation.domain.ReservationTime; | ||
import com.prgrms.catchtable.reservation.dto.request.CreateReservationRequest; | ||
import java.time.LocalDateTime; | ||
|
||
public class ReservationData { | ||
|
||
public static Reservation getReservation() { | ||
return Reservation.builder() | ||
.status(COMPLETED) | ||
.peopleCount(4) | ||
.reservationTime(getReservationTimeNotPreOccupied()) | ||
.build(); | ||
} | ||
|
||
public static ReservationTime getReservationTimeNotPreOccupied() { | ||
ReservationTime reservationTime = ReservationTime.builder() | ||
.time(LocalDateTime.of(2024, 12, 31, 19, 30)) | ||
.build(); | ||
reservationTime.insertShop(ShopData.getShop()); | ||
return reservationTime; | ||
} | ||
|
||
public static ReservationTime getReservationTimePreOccupied() { | ||
ReservationTime reservationTime = ReservationTime.builder() | ||
.time(LocalDateTime.of(2024, 12, 31, 19, 30)) | ||
.build(); | ||
reservationTime.insertShop(ShopData.getShop()); | ||
reservationTime.reversePreOccupied(); | ||
return reservationTime; | ||
} | ||
|
||
public static CreateReservationRequest getCreateReservationRequest() { | ||
return CreateReservationRequest.builder() | ||
.reservationTimeId(1L) | ||
.peopleCount(4) | ||
.build(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/prgrms/catchtable/common/data/shop/ShopData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.prgrms.catchtable.common.data.shop; | ||
|
||
import static com.prgrms.catchtable.shop.domain.Category.JAPANESE_FOOD; | ||
|
||
import com.prgrms.catchtable.shop.domain.Address; | ||
import com.prgrms.catchtable.shop.domain.Shop; | ||
import java.math.BigDecimal; | ||
|
||
public class ShopData { | ||
|
||
public static Shop getShop() { | ||
return Shop.builder() | ||
.name("shopA") | ||
.rating(BigDecimal.valueOf(5L)) | ||
.category(JAPANESE_FOOD) | ||
.address(Address.builder().build()) | ||
.capacity(30) | ||
.build(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/prgrms/catchtable/facade/ReservationFacadeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.prgrms.catchtable.facade; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.prgrms.catchtable.common.data.reservation.ReservationData; | ||
import com.prgrms.catchtable.reservation.domain.ReservationTime; | ||
import com.prgrms.catchtable.reservation.dto.request.CreateReservationRequest; | ||
import com.prgrms.catchtable.reservation.dto.response.CreateReservationResponse; | ||
import com.prgrms.catchtable.reservation.service.ReservationAsync; | ||
import com.prgrms.catchtable.reservation.service.ReservationService; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ReservationFacadeTest { | ||
|
||
@Mock | ||
private ReservationAsync reservationAsync; | ||
@Mock | ||
private ReservationService reservationService; | ||
@InjectMocks | ||
private ReservationFacade reservationFacade; | ||
|
||
@Test | ||
@DisplayName("예약을 검증하고 선점권을 true로 바꾸는 것에 성공한다.") | ||
void preOccupyReservation() { | ||
ReservationTime reservationTime = ReservationData.getReservationTimeNotPreOccupied(); | ||
CreateReservationRequest request = ReservationData.getCreateReservationRequest(); | ||
|
||
when(reservationService.validateReservationAndSave( | ||
any(CreateReservationRequest.class))).thenReturn(reservationTime); | ||
|
||
CreateReservationResponse response = reservationFacade.preOccupyReservation( | ||
request); | ||
|
||
assertAll( | ||
() -> assertThat(response.date()).isEqualTo(reservationTime.getTime()), | ||
() -> assertThat(response.peopleCount()).isEqualTo(request.peopleCount()) | ||
); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/com/prgrms/catchtable/reservation/domain/ReservationTimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.prgrms.catchtable.reservation.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.prgrms.catchtable.common.data.reservation.ReservationData; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ReservationTimeTest { | ||
|
||
@Test | ||
@DisplayName("예약 선점 여부 변경에 성공한다") | ||
void reversePreOccupied() { | ||
ReservationTime reservationTime = ReservationData.getReservationTimeNotPreOccupied(); | ||
reservationTime.reversePreOccupied(); | ||
|
||
assertThat(reservationTime.isPreOccupied()).isTrue(); | ||
} | ||
|
||
@Test | ||
@DisplayName("예약 여부 변경에 성공한다.") | ||
void reverseOccupied() { | ||
ReservationTime reservationTime = ReservationData.getReservationTimeNotPreOccupied(); | ||
reservationTime.reverseOccupied(); | ||
|
||
assertThat(reservationTime.isOccupied()).isTrue(); | ||
} | ||
} |
Oops, something went wrong.