Skip to content

Commit

Permalink
[feat] : 예매대기 등록 API Controller 구현 (#40)
Browse files Browse the repository at this point in the history
* feat: 예매대기 등록 요청 DTO에 검증 어노테이션 추가

* feat: 테스트를 위한 간단한 findAll 쿼리 구현

* feat: 예매대기 등록 API 구현
  • Loading branch information
EunChanNam authored Dec 26, 2023
1 parent 3071f17 commit ae05181
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.hooon.waitingbooking;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import dev.hooon.waitingbooking.application.facade.WaitingBookingFacade;
import dev.hooon.waitingbooking.dto.request.WaitingRegisterRequest;
import dev.hooon.waitingbooking.dto.response.WaitingRegisterResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class WaitingBookingApiController {

private final WaitingBookingFacade waitingBookingFacade;

@PostMapping("/api/waiting_bookings")
public ResponseEntity<WaitingRegisterResponse> registerWaitingBooking(
@Valid @RequestBody WaitingRegisterRequest request,
@RequestParam Long userId // TODO 추후에 인증정보 ArgumentResolver 가 구현되면 수정 예정
) {
WaitingRegisterResponse waitingRegisterResponse = waitingBookingFacade.registerWaitingBooking(userId, request);
return ResponseEntity.ok(waitingRegisterResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
public abstract class ApiTestSupport extends TestContainerSupport {

protected ObjectMapper objectMapper = new ObjectMapper();

protected String toJson(Object object) throws JsonProcessingException {
return objectMapper.writeValueAsString(object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dev.hooon.waitingbooking;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.http.MediaType.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import dev.hooon.common.support.ApiTestSupport;
import dev.hooon.user.application.UserService;
import dev.hooon.user.domain.entity.User;
import dev.hooon.waitingbooking.domain.entity.WaitingBooking;
import dev.hooon.waitingbooking.domain.repository.WaitingBookingRepository;
import dev.hooon.waitingbooking.dto.request.WaitingRegisterRequest;

@DisplayName("[WaitingBookingApiController API 테스트]")
class WaitingBookingApiControllerTest extends ApiTestSupport {

@Autowired
private MockMvc mockMvc;
@Autowired
private WaitingBookingRepository waitingBookingRepository;
// TODO User 저장에 대한 로직을 다른 작업에서 병행중이어서 일단 모킹으로 대체하구 추후에 수정
@MockBean
private UserService userService;

@Test
@DisplayName("[예매대기 등록 API 를 호출하면 예매대기가 등록되고 예매대기 ID 를 응답한다]")
void registerWaitingBookingTest() throws Exception {
//given
// TODO 추후에 User 생성 기능이 구현되면 수정 예정
User user = new User();
ReflectionTestUtils.setField(user, "id", 1L);
given(userService.getUserById(1L)).willReturn(user);

WaitingRegisterRequest waitingRegisterRequest = new WaitingRegisterRequest(2, List.of(1L, 2L, 3L, 4L));

//when
ResultActions actions = mockMvc.perform(
MockMvcRequestBuilders
.post("/api/waiting_bookings")
.queryParam("userId", "1") // TODO 추후에 인증정보 ArgumentResolver 가 구현되면 수정 예정
.contentType(APPLICATION_JSON)
.content(toJson(waitingRegisterRequest))
);

//then
List<WaitingBooking> allWaitingBookings = waitingBookingRepository.findAll();
assertThat(allWaitingBookings).isNotEmpty();

actions.andExpectAll(
status().isOk(),
jsonPath("$.waitingBookingId").value(allWaitingBookings.get(0).getId())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package dev.hooon.waitingbooking.domain.repository;

import java.util.List;

import dev.hooon.waitingbooking.domain.entity.WaitingBooking;

public interface WaitingBookingRepository {

void save(WaitingBooking waitingBooking);

List<WaitingBooking> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import java.util.List;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;

public record WaitingRegisterRequest(
@Positive
int seatCount,
@NotNull
List<Long> seatIds
) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.hooon.waitingbooking.infrastructure.adaptor;

import java.util.List;

import org.springframework.stereotype.Repository;

import dev.hooon.waitingbooking.domain.entity.WaitingBooking;
Expand All @@ -17,4 +19,10 @@ public class WaitingBookingRepositoryAdaptor implements WaitingBookingRepository
public void save(WaitingBooking waitingBooking) {
waitingBookingJpaRepository.save(waitingBooking);
}

@Override
public List<WaitingBooking> findAll() {
return waitingBookingJpaRepository.findAll();
}

}

0 comments on commit ae05181

Please sign in to comment.