mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-31 09:37:15 +01:00
Merged HIMA-38 to development
This commit is contained in:
@@ -8,7 +8,8 @@ import { GetProducts } from 'src/app/core/actions/product.actions';
|
||||
import { Router } from '@angular/router';
|
||||
import { ItemDTO } from 'projects/cat-service/src/lib';
|
||||
import { procuctsMock } from 'mocks/products.mock';
|
||||
import { mapItemDtoToProduct } from 'src/app/core/mappings/product.mapping';
|
||||
import { ProductMapping } from '../../core/mappings/product.mapping';
|
||||
import { map, flatMap, combineAll } from 'rxjs/operators';
|
||||
|
||||
@Component({
|
||||
selector: 'app-search-results',
|
||||
@@ -22,7 +23,8 @@ export class SearchResultsComponent implements OnInit {
|
||||
|
||||
constructor(
|
||||
private store: Store<AppState>,
|
||||
private router: Router
|
||||
private router: Router,
|
||||
private productMapping: ProductMapping
|
||||
) { }
|
||||
|
||||
ngOnInit() {
|
||||
@@ -48,16 +50,9 @@ export class SearchResultsComponent implements OnInit {
|
||||
}
|
||||
|
||||
loadProducts() {
|
||||
this.store.select(state => state.itemsDTO).subscribe(
|
||||
(data: ItemDTO[]) => this.products = this.itemDtoToPruductMapper(data)
|
||||
);
|
||||
this.store.select(state => state.itemsDTO).pipe(
|
||||
map(items => items.map(item => this.productMapping.fromItemDTO(item)))
|
||||
).subscribe(data => this.products = data);
|
||||
}
|
||||
|
||||
itemDtoToPruductMapper(itemsDto: ItemDTO[]) {
|
||||
const products: Product[] = [];
|
||||
// if (itemsDto) {
|
||||
// itemsDto.forEach(t => products.push(mapItemDtoToProduct(t)));
|
||||
// }
|
||||
return [...procuctsMock, ...products];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,71 @@
|
||||
import { Product } from '../models/product.model';
|
||||
import { isNullOrUndefined } from 'util';
|
||||
import { ItemDTO, AvailabilityType } from 'projects/cat-service/src/lib';
|
||||
import { AvailabilityDTO, PriceDTO } from 'cat-service';
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
|
||||
const itemFormatMap = {
|
||||
'CD': 'Hörbuch',
|
||||
'GEB': 'Buch',
|
||||
'TK': 'Taschenbuch'
|
||||
'KT': 'Taschenbuch',
|
||||
'SPL': 'Spiel'
|
||||
};
|
||||
|
||||
export function mapItemDtoToProduct(item: ItemDTO): Product {
|
||||
|
||||
if (isNullOrUndefined(item)) {
|
||||
throw new Error('argument item:ItemDTO is null or undefined.');
|
||||
}
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ProductMapping {
|
||||
|
||||
if (!Array.isArray(item.av)) {
|
||||
throw new Error('item.av is not an array of AvailabilityDTO');
|
||||
}
|
||||
|
||||
const availability = item.av.find(av => av.status === AvailabilityType.Available);
|
||||
let currency = '';
|
||||
let price = 0;
|
||||
|
||||
if (!isNullOrUndefined(availability)) {
|
||||
const pr = availability.price.value;
|
||||
|
||||
if (!!pr) {
|
||||
currency = pr.currency;
|
||||
price = pr.value;
|
||||
fromItemDTO(item: ItemDTO): Product {
|
||||
if (isNullOrUndefined(item)) {
|
||||
throw new Error('argument item:ItemDTO is null or undefined.');
|
||||
}
|
||||
|
||||
let availability: AvailabilityDTO;
|
||||
let currency = '';
|
||||
let price = 0;
|
||||
|
||||
if (Array.isArray(item.av)) {
|
||||
availability = item.av.find(av => av.status === AvailabilityType.Available);
|
||||
}
|
||||
|
||||
let priceDto: PriceDTO;
|
||||
if (!!availability) {
|
||||
priceDto = availability.price;
|
||||
}
|
||||
|
||||
if (!!priceDto && priceDto.value) {
|
||||
price = priceDto.value.value || price;
|
||||
currency = priceDto.value.currency || currency;
|
||||
}
|
||||
|
||||
let itemsInStock = 0;
|
||||
|
||||
if (Array.isArray(item.st)) {
|
||||
itemsInStock = item.st.reduce((aggr, si) => aggr + si.inStock, 0);
|
||||
}
|
||||
|
||||
return {
|
||||
author: item.pr.contributors,
|
||||
availability: !!availability,
|
||||
currency,
|
||||
price,
|
||||
id: item.id,
|
||||
itemsInStock,
|
||||
err: '',
|
||||
category: item.pr.productGroup,
|
||||
icon: '',
|
||||
notAvailableReason: '',
|
||||
publisher: item.pr.manufacturer,
|
||||
recommandation: false,
|
||||
serial: item.pr.serial,
|
||||
slogan: item.pr.subtitle,
|
||||
title: item.pr.title,
|
||||
type: itemFormatMap[item.pr.format],
|
||||
typeIcon: item.pr.format,
|
||||
location: '',
|
||||
publicationDate: item.pr.publicationDate
|
||||
};
|
||||
}
|
||||
|
||||
const itemsInStock = item.st.reduce((aggr, si) => aggr + si.inStock, 0);
|
||||
|
||||
return {
|
||||
author: item.pr.contributors,
|
||||
availability: !!availability,
|
||||
currency,
|
||||
price,
|
||||
id: item.id,
|
||||
itemsInStock,
|
||||
err: '',
|
||||
category: item.pr.productGroup,
|
||||
icon: '',
|
||||
notAvailableReason: '',
|
||||
publisher: item.pr.manufacturer,
|
||||
recommandation: false,
|
||||
serial: item.pr.serial,
|
||||
slogan: item.pr.subtitle,
|
||||
title: item.pr.title,
|
||||
type: itemFormatMap[item.pr.format],
|
||||
typeIcon: item.pr.format,
|
||||
location: '',
|
||||
publicationDate: item.pr.publicationDate
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
ItemDTO,
|
||||
PagedApiResponse
|
||||
} from 'projects/cat-service/src/lib';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
|
||||
@Injectable({
|
||||
@@ -64,10 +65,9 @@ export class ProductService {
|
||||
const queryToken = <QueryTokenDTO> {
|
||||
input: {qs: searchTerm}
|
||||
};
|
||||
let items$: Observable<ItemDTO[]>;
|
||||
this.searchService.search(queryToken).subscribe(
|
||||
(data: PagedApiResponse<ItemDTO>) => items$ = of(data.result)
|
||||
|
||||
return this.searchService.search(queryToken).pipe(
|
||||
map(response => response.result)
|
||||
);
|
||||
return items$;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user