Skip to content

Commit

Permalink
#1128 [BACKOFFICE] [Bugs] Add some boxes in the homepage of the backo…
Browse files Browse the repository at this point in the history
…ffice (#1138)
  • Loading branch information
nashtech-tuannguyenhuu1 authored Oct 8, 2024
1 parent b190def commit 20e163e
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 62 deletions.
28 changes: 27 additions & 1 deletion order/src/it/java/com/yas/order/service/OrderServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.yas.commonlibrary.IntegrationTestConfiguration;
import com.yas.commonlibrary.exception.NotFoundException;
import com.yas.order.OrderApplication;
import com.yas.commonlibrary.IntegrationTestConfiguration;
import com.yas.order.model.Order;
import com.yas.order.model.enumeration.OrderStatus;
import com.yas.order.model.enumeration.PaymentStatus;
import com.yas.order.repository.OrderItemRepository;
import com.yas.order.repository.OrderRepository;
import com.yas.order.viewmodel.order.OrderBriefVm;
import com.yas.order.viewmodel.order.OrderItemPostVm;
import com.yas.order.viewmodel.order.OrderListVm;
import com.yas.order.viewmodel.order.OrderPostVm;
Expand Down Expand Up @@ -281,4 +282,29 @@ void testAcceptOrder_whenNotFound_throwNotFoundException() {
}


@Test
void testGetLatestOrders_WhenHasListOrderListVm_returnListOrderListVm() {
orderService.createOrder(orderPostVm);
List<OrderBriefVm> orderList = orderService.getLatestOrders(1);
assertNotNull(orderList);
}

@Test
void testGetLatestOrders_WhenCountLessThen1_returnEmpty() {
List<OrderBriefVm> newResponse = orderService.getLatestOrders(-1);
assertEquals(0, newResponse.size());
}

@Test
void testGetLatestOrders_WhenCountIs0_returnEmpty() {
List<OrderBriefVm> newResponse = orderService.getLatestOrders(0);
assertEquals(0, newResponse.size());
}

@Test
void testGetLatestOrders_WhenProductsEmpty_returnEmpty() {
List<OrderBriefVm> newResponse = orderService.getLatestOrders(5);
assertEquals(0, newResponse.size());
}

}
3 changes: 2 additions & 1 deletion order/src/it/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ spring.liquibase.enabled=false

spring.security.oauth2.resourceserver.jwt.issuer-uri=test
springdoc.oauthflow.authorization-url=test
springdoc.oauthflow.token-url=test
springdoc.oauthflow.token-url=test
spring.jpa.open-in-view=true
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.yas.order.model.enumeration.OrderStatus;
import com.yas.order.service.OrderService;
import com.yas.order.viewmodel.order.OrderBriefVm;
import com.yas.order.viewmodel.order.OrderExistsByProductAndUserGetVm;
import com.yas.order.viewmodel.order.OrderGetVm;
import com.yas.order.viewmodel.order.OrderListVm;
Expand Down Expand Up @@ -85,4 +86,9 @@ public ResponseEntity<OrderListVm> getOrders(
pageNo,
pageSize));
}

@GetMapping("/backoffice/orders/latest/{count}")
public ResponseEntity<List<OrderBriefVm>> getLatestOrders(@PathVariable int count) {
return ResponseEntity.ok(orderService.getLatestOrders(count));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ Page<Order> findOrderByWithMulCriteria(
Pageable pageable);

Optional<Order> findByCheckoutId(String checkoutId);

@Query("SELECT o FROM Order o ORDER BY o.createdOn DESC")
List<Order> getLatestOrders(Pageable pageable);
}
18 changes: 18 additions & 0 deletions order/src/main/java/com/yas/order/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ public OrderListVm getAllOrder(ZonedDateTime createdFrom,
return new OrderListVm(orderVms, orderPage.getTotalElements(), orderPage.getTotalPages());
}

public List<OrderBriefVm> getLatestOrders(int count) {

if (count <= 0) {
return List.of();
}

Pageable pageable = PageRequest.of(0, count);
List<Order> orders = orderRepository.getLatestOrders(pageable);

if (CollectionUtils.isEmpty(orders)) {
return List.of();
}

return orders.stream()
.map(OrderBriefVm::fromModel)
.toList();
}

public OrderExistsByProductAndUserGetVm isOrderCompletedWithUserIdAndProductId(final Long productId) {

String userId = AuthenticationUtils.getCurrentUserId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.yas.order.model.enumeration.PaymentMethod;
import com.yas.order.model.enumeration.PaymentStatus;
import com.yas.order.service.OrderService;
import com.yas.order.viewmodel.order.OrderBriefVm;
import com.yas.order.viewmodel.order.OrderExistsByProductAndUserGetVm;
import com.yas.order.viewmodel.order.OrderGetVm;
import com.yas.order.viewmodel.order.OrderItemPostVm;
Expand All @@ -31,6 +32,7 @@
import com.yas.order.viewmodel.orderaddress.OrderAddressVm;
import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -205,6 +207,19 @@ void testGetOrders_whenRequestIsValid_thenReturnOrderListVm() throws Exception {
.json(objectWriter.writeValueAsString(orderListVm)));
}

@Test
void testGetLatestOrders_whenRequestIsValid_thenReturnOrderListVm() throws Exception {

List<OrderBriefVm> list = new ArrayList<>();
when(orderService.getLatestOrders(1)).thenReturn(list);

mockMvc.perform(get("/backoffice/orders/latest/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.content()
.json(objectWriter.writeValueAsString(list)));
}

private OrderVm getOrderVm() {

OrderAddressVm shippingAddress = new OrderAddressVm(
Expand Down
56 changes: 53 additions & 3 deletions product/src/it/java/com/yas/product/service/ProductServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.yas.product.viewmodel.product.ProductThumbnailGetVm;
import com.yas.product.viewmodel.product.ProductThumbnailVm;
import com.yas.product.viewmodel.product.ProductsGetVm;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -50,7 +51,7 @@
@Import(IntegrationTestConfiguration.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class ProductServiceIT {

private final ZonedDateTime CREATED_ON = ZonedDateTime.now();
@Autowired
private ProductRepository productRepository;
@Autowired
Expand Down Expand Up @@ -130,6 +131,7 @@ private void generateTestData() {
product.setBrand(brand1);
product.setPrice(5.0);
}
product.setCreatedOn(CREATED_ON.minusDays(i));
products.add(product);
}
productRepository.saveAll(products);
Expand Down Expand Up @@ -360,8 +362,8 @@ void getProductsByMultiQuery_WhenFilterByBrandNameAndProductName_ThenSuccess() {
Double startPrice = 1.0;
Double endPrice = 10.0;
String productName = "product2";
ProductsGetVm result
= productService.getProductsByMultiQuery(pageNo, pageSize, productName, category2.getSlug(), startPrice, endPrice);
ProductsGetVm result = productService.getProductsByMultiQuery(pageNo,
pageSize, productName, category2.getSlug(), startPrice, endPrice);

// Assert result
assertEquals(1, result.productContent().size());
Expand Down Expand Up @@ -391,4 +393,52 @@ void getProductsByBrandIds_WhenFindAllByBrandIds_ThenSuccess() {
assertEquals("product1", actualResponse.getFirst().name());
assertEquals("slug1", actualResponse.getFirst().slug());
}


@Test
void testGetLatestProducts_WhenHasListProductListVm_returnListProductListVm() {

List<Product> actualResponse = productRepository.findAll();

assertEquals(10, actualResponse.size());

actualResponse.getFirst().setCreatedOn(CREATED_ON.minusDays(1));
actualResponse.get(1).setCreatedOn(CREATED_ON.minusDays(2));
actualResponse.get(2).setCreatedOn(CREATED_ON.minusDays(3));
actualResponse.get(3).setCreatedOn(CREATED_ON.minusDays(4));
actualResponse.get(4).setCreatedOn(CREATED_ON.minusDays(5));
actualResponse.get(5).setCreatedOn(CREATED_ON.minusDays(6));
actualResponse.get(6).setCreatedOn(CREATED_ON.minusDays(7));
actualResponse.get(7).setCreatedOn(CREATED_ON.minusDays(8));
actualResponse.get(8).setCreatedOn(CREATED_ON.minusDays(9));
productRepository.saveAll(actualResponse);

List<ProductListVm> newResponse = productService.getLatestProducts(5);
assertEquals(5, newResponse.size());
assertEquals("product10", newResponse.getFirst().name());
assertEquals("product1", newResponse.get(1).name());
assertEquals("product2", newResponse.get(2).name());
assertEquals("product3", newResponse.get(3).name());
assertEquals("product4", newResponse.get(4).name());
}

@Test
void testGetLatestProducts_WhenCountLessThen1_returnEmpty() {
List<ProductListVm> newResponse = productService.getLatestProducts(-1);
assertEquals(0, newResponse.size());
}

@Test
void testGetLatestProducts_WhenCountIs0_returnEmpty() {
List<ProductListVm> newResponse = productService.getLatestProducts(0);
assertEquals(0, newResponse.size());
}

@Test
void testGetLatestProducts_WhenProductsEmpty_returnEmpty() {
tearDown();
List<ProductListVm> newResponse = productService.getLatestProducts(5);
assertEquals(0, newResponse.size());
}

}
1 change: 0 additions & 1 deletion product/src/it/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ spring.liquibase.enabled=false
spring.security.oauth2.resourceserver.jwt.issuer-uri=test
springdoc.oauthflow.authorization-url=test
springdoc.oauthflow.token-url=test
spring.jpa.open-in-view=true

cors.allowed-origins=*
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,11 @@ public ResponseEntity<List<ProductListVm>> getProductByBrands(
@RequestParam("ids") List<Long> brandIds) {
return ResponseEntity.ok(productService.getProductByBrandIds(brandIds));
}

@GetMapping("/backoffice/products/latest/{count}")
public ResponseEntity<List<ProductListVm>> getLatestProducts(@PathVariable int count) {
return ResponseEntity.ok(productService.getLatestProducts(count));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ List<Product> findProductForWarehouse(@Param("name") String name, @Param("sku")

@Query("SELECT p FROM Product p JOIN p.brand b WHERE b.id IN :brandIds ORDER BY p.id ASC")
List<Product> findByBrandIdsIn(@Param("brandIds") List<Long> brandIds);

@Query("SELECT p FROM Product p ORDER BY p.createdOn DESC")
List<Product> getLatestProducts(Pageable pageable);
}
18 changes: 18 additions & 0 deletions product/src/main/java/com/yas/product/service/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,24 @@ public ProductDetailVm getProductById(long productId) {
);
}

public List<ProductListVm> getLatestProducts(int count) {

if (count <= 0) {
return List.of();
}

Pageable pageable = PageRequest.of(0, count);
List<Product> products = productRepository.getLatestProducts(pageable);

if (CollectionUtils.isEmpty(products)) {
return List.of();
}

return products.stream()
.map(ProductListVm::fromModel)
.toList();
}

public List<ProductThumbnailVm> getProductsByBrand(String brandSlug) {
List<ProductThumbnailVm> productThumbnailVms = new ArrayList<>();
Brand brand = brandRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.yas.product.viewmodel.product.ProductPutVm;
import com.yas.product.viewmodel.product.ProductQuantityPutVm;
import java.time.ZonedDateTime;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,11 +27,8 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = ProductController.class)
@ContextConfiguration(classes = ProductApplication.class)
Expand Down Expand Up @@ -258,4 +256,26 @@ void testGetProductByBrands_Success() throws Exception {
// Verify interaction with the service
verify(productService, times(1)).getProductByBrandIds(anyList());
}

@Test
void testGetLatestProducts_Success() throws Exception {

List<ProductListVm> mockProductList = List.of(
new ProductListVm(3L, "Product 3", "product3", true, true,
false, true, 100.0, ZonedDateTime.now(), 1L),
new ProductListVm(4L, "Product 4", "product4", true, true,
false, true, 200.0, ZonedDateTime.now(), 1L)
);
when(productService.getLatestProducts(1)).thenReturn(mockProductList);

mockMvc.perform(MockMvcRequestBuilders.get("/backoffice/products/latest/1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(3L))
.andExpect(jsonPath("$[0].name").value("Product 3"))
.andExpect(jsonPath("$[1].id").value(4L))
.andExpect(jsonPath("$[1].name").value("Product 4"));

verify(productService, times(1)).getLatestProducts(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.yas.rating.viewmodel.ResponeStatusVm;
import jakarta.validation.Valid;
import java.time.ZonedDateTime;
import java.util.List;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand Down Expand Up @@ -46,6 +47,11 @@ public ResponseEntity<RatingListVm> getRatingListWithFilter(
pageNo, pageSize));
}

@GetMapping("/backoffice/ratings/latest/{count}")
public ResponseEntity<List<RatingVm>> getLatestRatings(@PathVariable int count) {
return ResponseEntity.ok(ratingService.getLatestRatings(count));
}

@DeleteMapping("/backoffice/ratings/{id}")
public ResponseEntity<ResponeStatusVm> deleteRating(@PathVariable Long id) {
return ResponseEntity.ok(ratingService.deleteRating(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ Page<Rating> getRatingListWithFilter(
List<Object[]> getTotalStarsAndTotalRatings(@Param("productId") long productId);

boolean existsByCreatedByAndProductId(String createdBy, Long productId);

@Query("SELECT r FROM Rating r ORDER BY r.createdOn DESC")
List<Rating> getLatestRatings(Pageable pageable);
}
19 changes: 19 additions & 0 deletions rating/src/main/java/com/yas/rating/service/RatingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

@Slf4j
Expand Down Expand Up @@ -72,6 +73,24 @@ public RatingListVm getRatingListWithFilter(String proName, String cusName,
return new RatingListVm(ratingVmList, ratings.getTotalElements(), ratings.getTotalPages());
}

public List<RatingVm> getLatestRatings(int count) {

if (count <= 0) {
return List.of();
}

Pageable pageable = PageRequest.of(0, count);
List<Rating> ratings = ratingRepository.getLatestRatings(pageable);

if (CollectionUtils.isEmpty(ratings)) {
return List.of();
}

return ratings.stream()
.map(RatingVm::fromModel)
.toList();
}

public RatingVm createRating(RatingPostVm ratingPostVm) {
String userId = AuthenticationUtils.extractUserId();

Expand Down
Loading

0 comments on commit 20e163e

Please sign in to comment.