Skip to content

Commit

Permalink
feat: reduce transfering extra data from database
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Sep 5, 2023
1 parent 0c6dd99 commit b539556
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli.
import com.example.orderservice.entities.Order;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -32,4 +34,7 @@ int updateOrderStatusAndSourceById(
@Param("id") Long orderId,
@Param("status") String status,
@Param("source") String source);

@Query("select o.id from Order o")
Page<Long> findAllOrders(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ public PagedResult<OrderDto> findAllOrders(

// create Pageable instance
Pageable pageable = PageRequest.of(pageNo, pageSize, sort);
// Fetches only ParentEntities
Page<Order> page = orderRepository.findAll(pageable);
// Get orderIds matching pagination
List<Long> orderIds = page.getContent().stream().map(Order::getId).toList();
// Fetches only ParentEntities ids
Page<Long> page = orderRepository.findAllOrders(pageable);
// fetching parentAlongWithChildEntries
List<Order> ordersWithOrderItems = orderRepository.findByIdIn(orderIds);
List<Order> ordersWithOrderItems = orderRepository.findByIdIn(page.getContent());
// Mapping Order to OrderDTO CompletableFuture
List<CompletableFuture<OrderDto>> completableFutureList =
ordersWithOrderItems.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/***
<p>
Licensed under MIT License Copyright (c) 2023 Raja Kolli.
</p>
***/

package com.example.orderservice.repositories;

import static org.assertj.core.api.Assertions.assertThat;

import com.example.orderservice.common.PostGreSQLContainer;
import com.example.orderservice.entities.Order;
import com.example.orderservice.util.TestData;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@ImportTestcontainers(PostGreSQLContainer.class)
@DataJpaTest(properties = "application.catalogServiceUrl=http://dummy")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class OrderRepositoryTest {

@Autowired private OrderRepository orderRepository;
@Autowired private OrderItemRepository orderItemRepository;

@AfterEach
void tearDown() {
this.orderItemRepository.deleteAllInBatch();
this.orderRepository.deleteAllInBatch();
}

@Test
void findAllOrders() {

List<Order> orderList = new ArrayList<>();
for (int i = 0; i <= 15; i++) {
orderList.add(TestData.getOrder());
}
this.orderRepository.saveAll(orderList);

// create Pageable instance
Pageable pageable = PageRequest.of(1, 5, Sort.by("id").ascending());

Page<Long> page = this.orderRepository.findAllOrders(pageable);

assertThat(page).isNotNull();
assertThat(page.getTotalElements()).isEqualTo(16);
assertThat(page.getTotalPages()).isEqualTo(4);
assertThat(page.getNumber()).isEqualTo(1);
assertThat(page.isFirst()).isEqualTo(false);
assertThat(page.isLast()).isEqualTo(false);
assertThat(page.hasNext()).isEqualTo(true);
assertThat(page.hasPrevious()).isEqualTo(true);
assertThat(page.getNumberOfElements()).isEqualTo(5);
assertThat(page.getContent()).isNotEmpty().hasSize(5);
}
}

0 comments on commit b539556

Please sign in to comment.