Skip to content

Commit

Permalink
feat: polish pagedresult
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Oct 31, 2023
1 parent ee01394 commit 2147175
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public record PagedResult<T>(
@JsonProperty("isLast") boolean isLast,
@JsonProperty("hasNext") boolean hasNext,
@JsonProperty("hasPrevious") boolean hasPrevious) {
public PagedResult(Page<T> page) {

public <R> PagedResult(List<T> data, Page<R> page) {
this(
page.getContent(),
data,
page.getTotalElements(),
page.getNumber() + 1, // for user page number starts from 1
page.getTotalPages(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional
@Transactional(readOnly = true)
@Loggable
public class CustomerService {

private final CustomerRepository customerRepository;
private final CustomerMapper customerMapper;

@Transactional(readOnly = true)
public PagedResult<CustomerResponse> findAllCustomers(FindCustomersQuery findCustomersQuery) {
log.info(
"Fetching findAllCustomers for pageNo {} with pageSize {}, sorting By {} {}",
Expand All @@ -42,18 +41,10 @@ public PagedResult<CustomerResponse> findAllCustomers(FindCustomersQuery findCus

Pageable pageable = createPageable(findCustomersQuery);
Page<Customer> page = customerRepository.findAll(pageable);

List<CustomerResponse> customerResponseList =
customerMapper.toListResponse(page.getContent());

return new PagedResult<>(
customerResponseList,
page.getTotalElements(),
page.getNumber() + 1, // for user page number starts from 1
page.getTotalPages(),
page.isFirst(),
page.isLast(),
page.hasNext(),
page.hasPrevious());
return new PagedResult<>(customerResponseList, page);
}

private Pageable createPageable(FindCustomersQuery findCustomersQuery) {
Expand All @@ -66,21 +57,21 @@ private Pageable createPageable(FindCustomersQuery findCustomersQuery) {
return PageRequest.of(pageNo, findCustomersQuery.pageSize(), sort);
}

@Transactional(readOnly = true)
public Optional<CustomerResponse> findCustomerById(Long id) {
return customerRepository.findById(id).map(customerMapper::toResponse);
}

@Transactional(readOnly = true)
public Optional<CustomerResponse> findCustomerByName(String name) {
return customerRepository.findByName(name);
}

@Transactional
public CustomerResponse saveCustomer(CustomerRequest customerRequest) {
Customer customer = customerMapper.toEntity(customerRequest);
return customerMapper.toResponse(customerRepository.save(customer));
}

@Transactional
public CustomerResponse updateCustomer(Long id, CustomerRequest customerRequest) {
Customer customer =
customerRepository
Expand All @@ -97,6 +88,7 @@ public CustomerResponse updateCustomer(Long id, CustomerRequest customerRequest)
return customerMapper.toResponse(updatedCustomer);
}

@Transactional
public void deleteCustomerById(Long id) {
customerRepository.deleteById(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.example.paymentservice.exception.CustomerNotFoundException;
import com.example.paymentservice.repositories.CustomerRepository;
import com.example.paymentservice.utils.AppConstants;
import io.micrometer.core.annotation.Timed;
import java.math.BigDecimal;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -23,6 +24,7 @@ public class PaymentOrderManageService {
private final CustomerRepository customerRepository;
private final KafkaTemplate<Long, OrderDto> kafkaTemplate;

@Timed(percentiles = 1.0)
public void reserve(OrderDto orderDto) {
log.debug(
"Reserving Order with Id :{} in payment service with payload {}",
Expand Down Expand Up @@ -55,6 +57,7 @@ public void reserve(OrderDto orderDto) {
AppConstants.PAYMENT_ORDERS_TOPIC);
}

@Timed(percentiles = 1.0)
public void confirm(OrderDto orderDto) {
log.debug(
"Confirming Order with Id :{} in payment service with payload {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
Expand Down Expand Up @@ -68,8 +69,9 @@ void setUp() {

@Test
void shouldFetchAllCustomers() throws Exception {
Page<CustomerResponse> page = new PageImpl<>(getCustomerResponseList());
PagedResult<CustomerResponse> postPagedResult = new PagedResult<>(page);
Page<Customer> page = new PageImpl<>(customerList);
PagedResult<CustomerResponse> postPagedResult =
new PagedResult<>(getCustomerResponseList(), page);
FindCustomersQuery findCustomersQuery = new FindCustomersQuery(0, 10, "id", "desc");
given(customerService.findAllCustomers(findCustomersQuery)).willReturn(postPagedResult);

Expand Down Expand Up @@ -124,7 +126,7 @@ void shouldReturn404WhenFetchingNonExistingCustomerById() throws Exception {
.andExpect(status().isNotFound())
.andExpect(
header().string(
"Content-Type",
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("https://api.customers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Customer Not Found")))
Expand All @@ -143,7 +145,7 @@ void shouldReturn404WhenFetchingNonExistingCustomerByName() throws Exception {
.andExpect(status().isNotFound())
.andExpect(
header().string(
"Content-Type",
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("https://api.customers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Customer Not Found")))
Expand Down Expand Up @@ -172,15 +174,18 @@ void shouldCreateNewCustomer() throws Exception {

@Test
void shouldReturn400WhenCreateNewCustomerWithoutNameAndEmail() throws Exception {
CustomerRequest customer = new CustomerRequest(null, null, null, 1);
CustomerRequest customerRequest = new CustomerRequest(null, null, null, 1);

this.mockMvc
.perform(
post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customer)))
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isBadRequest())
.andExpect(header().string("Content-Type", is("application/problem+json")))
.andExpect(
header().string(
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(
jsonPath(
"$.type",
Expand Down Expand Up @@ -237,7 +242,7 @@ void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception {
.andExpect(status().isNotFound())
.andExpect(
header().string(
"Content-Type",
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("https://api.customers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Customer Not Found")))
Expand Down Expand Up @@ -269,7 +274,7 @@ void shouldReturn404WhenDeletingNonExistingCustomer() throws Exception {
.andExpect(status().isNotFound())
.andExpect(
header().string(
"Content-Type",
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("https://api.customers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Customer Not Found")))
Expand Down

0 comments on commit 2147175

Please sign in to comment.