Skip to content

Commit

Permalink
[feat] 전체 회원 조회 (#100)
Browse files Browse the repository at this point in the history
* [test] RetrieveCustomerServiceTest 구현
-  RetrieveCustomerServiceTest.testRetrieveAllCustomers: 전체 구매자 조회 테스트 성공

* [feat] RetrieveCustomerService 구현
-  RetrieveCustomerService.retrieveAllCustomers: 전체 구매자 조회 로직

* [test] CustomerApiControllerTest 수정
- CustomerApiControllerTest.testRetrieveCustomers: 전체 구매자 조회 테스트

* [feat] CustomerApiController 수정
- CustomerApiController.retrieveAllCustomers: 전체 구매자 조회 요청 처리

* [feat] RetrieveCustomerResponse 구현

* [refactor] VendorApiControllerTest 패키지 수정

* [test] RetrieveVendorServiceTest 구현
- RetrieveVendorServiceTest.testRetrieveVendors: 전체 판매자 조회 테스트

* [test] VendorApiControllerTest 수정
- VendorApiControllerTest.FindVendor: 전체 점주 조회 요청 테스트

* [feat] RetrieveVendorService 구현
- RetrieveVendorService.retrieveVendors: 전체 점주 조회 로직

* [feat] VendorApiController 수정
- VendorApiController.retrieveVendors: 전체 점주 조회 요청 처리

* [feat] RetrieveVendorResponse 구현

* [fix] RetrieveVendorServiceTest 수정
- assertEquals actual 과 expected 정확한 위치로 가도록 수정

* [feat] DTO 구현
- VendorDTO, CustomerDTO, PayAccountDTO

* [refactor] DTO 구현에 따른 변경 적용

* [refactor] DTO 외부 종속 제거
- Vendor 와 Customer 가 보여주고 싶은 PayAccount의 정보가 다를 수 있으므로 각 DTO 안에 종속된 PayAccount 정보를 가지도록 수정
  • Loading branch information
kimhyun5u authored Aug 17, 2024
1 parent a3079a1 commit e19b259
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package camp.woowak.lab.customer.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import camp.woowak.lab.customer.repository.CustomerRepository;
import camp.woowak.lab.customer.service.dto.CustomerDTO;

@Service
@Transactional(readOnly = true)
public class RetrieveCustomerService {
private final CustomerRepository customerRepository;

public RetrieveCustomerService(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

public List<CustomerDTO> retrieveAllCustomers() {
return customerRepository.findAll().stream().map(CustomerDTO::new).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package camp.woowak.lab.customer.service.dto;

import java.util.UUID;

import camp.woowak.lab.customer.domain.Customer;
import camp.woowak.lab.payaccount.domain.PayAccount;
import lombok.Getter;

@Getter
public class CustomerDTO {
private final UUID id;
private final String name;
private final String email;
private final String phone;
private final PayAccountDTO payAccount;

public CustomerDTO(Customer customer) {
this.id = customer.getId();
this.name = customer.getName();
this.email = customer.getEmail();
this.phone = customer.getPhone();
this.payAccount = new PayAccountDTO(customer.getPayAccount());
}

@Getter
public static class PayAccountDTO {
private final Long id;
private final Long balance;

public PayAccountDTO(PayAccount payAccount) {
this.id = payAccount.getId();
this.balance = payAccount.getBalance();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package camp.woowak.lab.vendor.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import camp.woowak.lab.vendor.repository.VendorRepository;
import camp.woowak.lab.vendor.service.dto.VendorDTO;

@Service
@Transactional(readOnly = true)
public class RetrieveVendorService {
private final VendorRepository vendorRepository;

public RetrieveVendorService(VendorRepository vendorRepository) {
this.vendorRepository = vendorRepository;
}

public List<VendorDTO> retrieveVendors() {
return vendorRepository.findAll().stream().map(VendorDTO::new).toList();
}
}
35 changes: 35 additions & 0 deletions src/main/java/camp/woowak/lab/vendor/service/dto/VendorDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package camp.woowak.lab.vendor.service.dto;

import java.util.UUID;

import camp.woowak.lab.payaccount.domain.PayAccount;
import camp.woowak.lab.vendor.domain.Vendor;
import lombok.Getter;

@Getter
public class VendorDTO {
private final UUID id;
private final String name;
private final String email;
private final String phone;
private final PayAccountDTO payAccount;

public VendorDTO(Vendor vendor) {
this.id = vendor.getId();
this.name = vendor.getName();
this.email = vendor.getEmail();
this.phone = vendor.getPhone();
this.payAccount = new PayAccountDTO(vendor.getPayAccount());
}

@Getter
public static class PayAccountDTO {
private final Long id;
private final Long balance;

public PayAccountDTO(PayAccount payAccount) {
this.id = payAccount.getId();
this.balance = payAccount.getBalance();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
import java.util.UUID;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import camp.woowak.lab.customer.service.RetrieveCustomerService;
import camp.woowak.lab.customer.service.SignInCustomerService;
import camp.woowak.lab.customer.service.SignUpCustomerService;
import camp.woowak.lab.customer.service.command.SignInCustomerCommand;
import camp.woowak.lab.customer.service.command.SignUpCustomerCommand;
import camp.woowak.lab.web.authentication.LoginCustomer;
import camp.woowak.lab.web.dto.request.customer.SignInCustomerRequest;
import camp.woowak.lab.web.dto.request.customer.SignUpCustomerRequest;
import camp.woowak.lab.web.dto.response.customer.RetrieveCustomerResponse;
import camp.woowak.lab.web.dto.response.customer.SignInCustomerResponse;
import camp.woowak.lab.web.dto.response.customer.SignUpCustomerResponse;
import camp.woowak.lab.web.resolver.session.SessionConst;
Expand All @@ -26,11 +29,20 @@
public class CustomerApiController {
private final SignUpCustomerService signUpCustomerService;
private final SignInCustomerService signInCustomerService;
private final RetrieveCustomerService retrieveCustomerService;

public CustomerApiController(SignUpCustomerService signUpCustomerService,
SignInCustomerService signInCustomerService) {
SignInCustomerService signInCustomerService,
RetrieveCustomerService retrieveCustomerService) {
this.signUpCustomerService = signUpCustomerService;
this.signInCustomerService = signInCustomerService;
this.retrieveCustomerService = retrieveCustomerService;
}

@GetMapping("/customers")
@ResponseStatus(HttpStatus.OK)
public RetrieveCustomerResponse retrieveAllCustomers() {
return new RetrieveCustomerResponse(retrieveCustomerService.retrieveAllCustomers());
}

@PostMapping("/customers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
import java.util.UUID;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import camp.woowak.lab.vendor.service.RetrieveVendorService;
import camp.woowak.lab.vendor.service.SignInVendorService;
import camp.woowak.lab.vendor.service.SignUpVendorService;
import camp.woowak.lab.vendor.service.command.SignInVendorCommand;
import camp.woowak.lab.vendor.service.command.SignUpVendorCommand;
import camp.woowak.lab.web.authentication.LoginVendor;
import camp.woowak.lab.web.dto.request.vendor.SignInVendorRequest;
import camp.woowak.lab.web.dto.request.vendor.SignUpVendorRequest;
import camp.woowak.lab.web.dto.response.vendor.RetrieveVendorResponse;
import camp.woowak.lab.web.dto.response.vendor.SignInVendorResponse;
import camp.woowak.lab.web.dto.response.vendor.SignUpVendorResponse;
import camp.woowak.lab.web.resolver.session.SessionConst;
Expand All @@ -25,10 +28,19 @@
public class VendorApiController {
private final SignUpVendorService signUpVendorService;
private final SignInVendorService signInVendorService;
private final RetrieveVendorService retrieveVendorService;

public VendorApiController(SignUpVendorService signUpVendorService, SignInVendorService signInVendorService) {
public VendorApiController(SignUpVendorService signUpVendorService, SignInVendorService signInVendorService,
RetrieveVendorService retrieveVendorService) {
this.signUpVendorService = signUpVendorService;
this.signInVendorService = signInVendorService;
this.retrieveVendorService = retrieveVendorService;
}

@GetMapping("/vendors")
@ResponseStatus(HttpStatus.OK)
public RetrieveVendorResponse retrieveVendors() {
return new RetrieveVendorResponse(retrieveVendorService.retrieveVendors());
}

@PostMapping("/vendors")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package camp.woowak.lab.web.dto.response.customer;

import java.util.List;

import camp.woowak.lab.customer.service.dto.CustomerDTO;

public record RetrieveCustomerResponse(List<CustomerDTO> customers) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package camp.woowak.lab.web.dto.response.vendor;

import java.util.List;

import camp.woowak.lab.vendor.service.dto.VendorDTO;

public record RetrieveVendorResponse(List<VendorDTO> vendors) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package camp.woowak.lab.customer.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.List;
import java.util.UUID;

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;

import camp.woowak.lab.customer.repository.CustomerRepository;
import camp.woowak.lab.customer.service.dto.CustomerDTO;
import camp.woowak.lab.fixture.CustomerFixture;

@ExtendWith(MockitoExtension.class)
class RetrieveCustomerServiceTest implements CustomerFixture {
@InjectMocks
private RetrieveCustomerService retrieveCustomerService;

@Mock
private CustomerRepository customerRepository;

@Test
@DisplayName("전체 Customer 조회 테스트 - 성공")
void testRetrieveAllCustomers() {
// given
given(customerRepository.findAll()).willReturn(
List.of(createCustomer(UUID.randomUUID()), createCustomer(UUID.randomUUID())));
// when
List<CustomerDTO> result = retrieveCustomerService.retrieveAllCustomers();

// then
assertEquals(2, result.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package camp.woowak.lab.vendor.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.List;

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;

import camp.woowak.lab.fixture.VendorFixture;
import camp.woowak.lab.vendor.repository.VendorRepository;
import camp.woowak.lab.vendor.service.dto.VendorDTO;
import camp.woowak.lab.web.authentication.NoOpPasswordEncoder;

@ExtendWith(MockitoExtension.class)
class RetrieveVendorServiceTest implements VendorFixture {
@InjectMocks
private RetrieveVendorService retrieveVendorService;

@Mock
private VendorRepository vendorRepository;

@Test
@DisplayName("전체 판매자 조회 테스트 - 성공")
void testRetrieveVendors() {
// given
given(vendorRepository.findAll()).willReturn(
List.of(createVendor(createPayAccount(), new NoOpPasswordEncoder())));
// when
List<VendorDTO> result = retrieveVendorService.retrieveVendors();
// then
assertEquals(1, result.size());
verify(vendorRepository).findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.util.List;
import java.util.UUID;

import org.junit.jupiter.api.Assertions;
Expand All @@ -26,9 +27,12 @@
import camp.woowak.lab.customer.exception.CustomerAuthenticationException;
import camp.woowak.lab.customer.exception.CustomerErrorCode;
import camp.woowak.lab.customer.exception.DuplicateEmailException;
import camp.woowak.lab.customer.service.RetrieveCustomerService;
import camp.woowak.lab.customer.service.SignInCustomerService;
import camp.woowak.lab.customer.service.SignUpCustomerService;
import camp.woowak.lab.customer.service.command.SignInCustomerCommand;
import camp.woowak.lab.customer.service.dto.CustomerDTO;
import camp.woowak.lab.fixture.CustomerFixture;
import camp.woowak.lab.web.authentication.LoginCustomer;
import camp.woowak.lab.web.dto.request.customer.SignInCustomerRequest;
import camp.woowak.lab.web.dto.request.customer.SignUpCustomerRequest;
Expand All @@ -37,7 +41,7 @@

@WebMvcTest(CustomerApiController.class)
@MockBean(JpaMetamodelMappingContext.class)
class CustomerApiControllerTest {
class CustomerApiControllerTest implements CustomerFixture {

@Autowired
private MockMvc mockMvc;
Expand All @@ -48,6 +52,9 @@ class CustomerApiControllerTest {
@MockBean
private SignInCustomerService signInCustomerService;

@MockBean
private RetrieveCustomerService retrieveCustomerService;

@Autowired
private ObjectMapper objectMapper;

Expand Down Expand Up @@ -274,4 +281,19 @@ void testLoginFail() throws Exception {
.andExpect(jsonPath("$.errorCode").value(CustomerErrorCode.AUTHENTICATION_FAILED.getErrorCode()));
}

@Test
@DisplayName("전체 구매자 조회 테스트 - 성공")
void testRetrieveCustomers() throws Exception {
// given
given(retrieveCustomerService.retrieveAllCustomers()).willReturn(
List.of(createCustomer(UUID.randomUUID()), createCustomer(UUID.randomUUID()))
.stream()
.map(CustomerDTO::new)
.toList());
// when & then
mockMvc.perform(get("/customers")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.customers").isArray());
}
}
Loading

0 comments on commit e19b259

Please sign in to comment.