Skip to content

Commit

Permalink
Update product service
Browse files Browse the repository at this point in the history
  • Loading branch information
nntthuy-axonivy committed Aug 1, 2024
1 parent 3c280d9 commit d0535ed
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ProductController(ProductService productService, GitHubService gitHubServ
public ResponseEntity<PagedModel<ProductModel>> findProducts(@RequestParam(name = TYPE) String type,
@RequestParam(required = false, name = KEYWORD) String keyword,
@RequestParam(name = LANGUAGE) String language, @RequestParam(name = IS_REST_DESIGNER) Boolean isRestDesigner, Pageable pageable) {
Page<Product> results = productService.findAllProducts(type, keyword, language, isRestDesigner, pageable);
Page<Product> results = productService.findProducts(type, keyword, language, isRestDesigner, pageable);
if (results.isEmpty()) {
return generateEmptyPagedModel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.data.domain.Pageable;

public interface ProductService {
Page<Product> findAllProducts(String type, String keyword, String language, Boolean isRestDesigner,Pageable pageable);
Page<Product> findProducts(String type, String keyword, String language, Boolean isRestDesigner, Pageable pageable);

boolean syncLatestDataFromMarketRepo();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -103,15 +98,19 @@ public ProductServiceImpl(ProductRepository productRepository, GHAxonIvyMarketRe
}

@Override
public Page<Product> findProducts(String type, String keyword, String language, Pageable pageable) {
public Page<Product> findProducts(String type, String keyword, String language, Boolean isRestDesigner, Pageable pageable) {
final var typeOption = TypeOption.of(type);
final var searchPageable = refinePagination(language, pageable);
var searchCriteria = new ProductSearchCriteria();
searchCriteria.setType(typeOption);
searchCriteria.setListed(true);
searchCriteria.setKeyword(keyword);
searchCriteria.setLanguage(Language.of(language));
searchCriteria.setType(typeOption);
if (BooleanUtils.isTrue(isRestDesigner)) {
searchCriteria.setType(TypeOption.CONNECTORS);
searchCriteria.setLanguage(Language.of(Locale.ENGLISH.toLanguageTag()));
} else {
searchCriteria.setType(typeOption);
searchCriteria.setLanguage(Language.of(language));
}
return productRepository.searchByCriteria(searchCriteria, searchPageable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void setup() {
void testFindProductsAsEmpty() {
PageRequest pageable = PageRequest.of(0, 20);
Page<Product> mockProducts = new PageImpl<>(List.of(), pageable, 0);
when(service.findAllProducts(any(), any(), any(), any() , any())).thenReturn(mockProducts);
when(service.findProducts(any(), any(), any(), any() , any())).thenReturn(mockProducts);
when(pagedResourcesAssembler.toEmptyModel(any(), any())).thenReturn(PagedModel.empty());
var result = productController.findProducts(TypeOption.ALL.getOption(), null, "en", false, pageable);
assertEquals(HttpStatus.OK, result.getStatusCode());
Expand All @@ -87,7 +87,7 @@ void testFindProducts() {
Product mockProduct = createProductMock();

Page<Product> mockProducts = new PageImpl<>(List.of(mockProduct), pageable, 1);
when(service.findAllProducts(any(), any(), any(), any(), any())).thenReturn(mockProducts);
when(service.findProducts(any(), any(), any(), any(), any())).thenReturn(mockProducts);
assembler = new ProductModelAssembler();
var mockProductModel = assembler.toModel(mockProduct);
var mockPagedModel = PagedModel.of(List.of(mockProductModel), new PageMetadata(1, 0, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@ void testFindProducts() {
// Start testing by All
when(productRepository.searchByCriteria(any(), any(Pageable.class))).thenReturn(mockResultReturn);
// Executes
var result = productService.findProducts(TypeOption.ALL.getOption(), keyword, language, PAGEABLE);
var result = productService.findProducts(TypeOption.ALL.getOption(), keyword, language, false, PAGEABLE);
assertEquals(mockResultReturn, result);

// Start testing by Connector
// Executes
result = productService.findProducts(TypeOption.CONNECTORS.getOption(), keyword, language, PAGEABLE);
result = productService.findProducts(TypeOption.CONNECTORS.getOption(), keyword, language, true, PAGEABLE);
assertEquals(mockResultReturn, result);

// Start testing by Other
// Executes
result = productService.findProducts(TypeOption.DEMOS.getOption(), keyword, language, PAGEABLE);
result = productService.findProducts(TypeOption.DEMOS.getOption(), keyword, language, false, PAGEABLE);
assertEquals(2, result.getSize());
}

Expand Down Expand Up @@ -260,7 +260,7 @@ void testFindAllProductsWithKeyword() {
language = "en";
when(productRepository.searchByCriteria(any(), any(Pageable.class))).thenReturn(mockResultReturn);
// Executes
var result = productService.findProducts(TypeOption.ALL.getOption(), keyword, language, PAGEABLE);
var result = productService.findProducts(TypeOption.ALL.getOption(), keyword, language, false, PAGEABLE);
assertEquals(mockResultReturn, result);
verify(productRepository).searchByCriteria(any(), any(Pageable.class));

Expand All @@ -270,7 +270,7 @@ void testFindAllProductsWithKeyword() {
.filter(product -> product.getNames().get(Language.EN.getValue()).equals(SAMPLE_PRODUCT_NAME))
.collect(Collectors.toList())));
// Executes
result = productService.findProducts(TypeOption.ALL.getOption(), SAMPLE_PRODUCT_NAME, language, PAGEABLE);
result = productService.findProducts(TypeOption.ALL.getOption(), SAMPLE_PRODUCT_NAME, language, false, PAGEABLE);
assertTrue(result.hasContent());
assertEquals(SAMPLE_PRODUCT_NAME, result.getContent().get(0).getNames().get(Language.EN.getValue()));

Expand All @@ -281,7 +281,7 @@ void testFindAllProductsWithKeyword() {
&& product.getType().equals(TypeOption.CONNECTORS.getCode()))
.collect(Collectors.toList())));
// Executes
result = productService.findProducts(TypeOption.CONNECTORS.getOption(), SAMPLE_PRODUCT_NAME, language, PAGEABLE);
result = productService.findProducts(TypeOption.CONNECTORS.getOption(), SAMPLE_PRODUCT_NAME, language, false, PAGEABLE);
assertTrue(result.hasContent());
assertEquals(SAMPLE_PRODUCT_NAME, result.getContent().get(0).getNames().get(Language.EN.getValue()));
}
Expand Down Expand Up @@ -345,7 +345,7 @@ void testSearchProducts() {
when(productRepository.searchByCriteria(any(), any(Pageable.class))).thenReturn(
mockResultReturn);

var result = productService.findProducts(type, keyword, language, simplePageable);
var result = productService.findProducts(type, keyword, language, false, simplePageable);
assertEquals(result, mockResultReturn);
verify(productRepository).searchByCriteria(any(), any(Pageable.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import {
import { ProductCardComponent } from './product-card.component';
import { Product } from '../../../shared/models/product.model';
import { Language } from '../../../shared/enums/language.enum';
import { ProductComponent } from '../product.component';
import { ProductService } from '../product.service';
import { inject } from '@angular/core';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import {
provideHttpClient,
withInterceptorsFromDi
} from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
import { of } from 'rxjs';

const products = MOCK_PRODUCTS._embedded.products as Product[];
const noDeNameAndNoLogoUrlProducts =
Expand All @@ -15,11 +25,20 @@ const noDeNameAndNoLogoUrlProducts =
describe('ProductCardComponent', () => {
let component: ProductCardComponent;
let fixture: ComponentFixture<ProductCardComponent>;
let mockActivatedRoute: any;

beforeEach(async () => {
mockActivatedRoute = { queryParams: of({ showPopup: 'true' }) };
await TestBed.configureTestingModule({
imports: [ProductCardComponent, TranslateModule.forRoot()],
providers: [TranslateService]
providers: [
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
TranslateService,
ProductService,
ProductComponent,
{ provide: ActivatedRoute, useValue: mockActivatedRoute }
]
}).compileComponents();

fixture = TestBed.createComponent(ProductCardComponent);
Expand Down
16 changes: 5 additions & 11 deletions marketplace-ui/src/app/modules/product/product.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ProductComponent implements AfterViewInit, OnDestroy {
criteria: Criteria = {
search: '',
type: TypeOption.All_TYPES,
isRestDesigner: false
isRestDesigner: false,
sort: SortOption.STANDARD,
language: Language.EN
};
Expand All @@ -71,23 +71,17 @@ export class ProductComponent implements AfterViewInit, OnDestroy {
@ViewChild('observer', { static: true }) observerElement!: ElementRef;

constructor() {
console.log(this.route);
let phuc = '';
this.route.queryParams.subscribe(params => {
if ('resultsOnly' in params && this.isDesignerEnvironment) {
this.isRestClient.set(true);
} else {
this.isRestClient.set(false);
}
this.isRestClient.set(
params['resultsOnly'] && this.isDesignerEnvironment
);

if (params['search'] != null) {
phuc = params['search'];
console.log(params['search']);
this.criteria.search = params['search'];
}
});

this.loadProductItems();
console.log(phuc);
this.subscriptions.push(
this.searchTextChanged
.pipe(debounceTime(SEARCH_DEBOUNCE_TIME))
Expand Down

0 comments on commit d0535ed

Please sign in to comment.