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

[feat] 전체 회원 조회 #100

Merged
merged 15 commits into from
Aug 17, 2024
Merged
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
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;
}

kimhyun5u marked this conversation as resolved.
Show resolved Hide resolved
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