154 lines
3.6 KiB
TypeScript
154 lines
3.6 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import {
|
|
BehaviorSubject,
|
|
filter,
|
|
map,
|
|
Observable,
|
|
of,
|
|
switchMap,
|
|
take,
|
|
tap,
|
|
throwError,
|
|
} from 'rxjs';
|
|
|
|
import { Service } from '../models/service';
|
|
import { ServicePagination } from '../models/service-pagination';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class ServiceService {
|
|
// Private
|
|
private __pagination = new BehaviorSubject<ServicePagination | undefined>(
|
|
undefined
|
|
);
|
|
private __service = new BehaviorSubject<Service | undefined>(undefined);
|
|
private __services = new BehaviorSubject<Service[] | undefined>(undefined);
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
constructor(private _httpClient: HttpClient) {}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Accessors
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Getter for pagination
|
|
*/
|
|
get pagination$(): Observable<ServicePagination | undefined> {
|
|
return this.__pagination.asObservable();
|
|
}
|
|
|
|
/**
|
|
* Getter for service
|
|
*/
|
|
get service$(): Observable<Service | undefined> {
|
|
return this.__service.asObservable();
|
|
}
|
|
|
|
/**
|
|
* Getter for services
|
|
*/
|
|
get services$(): Observable<Service[] | undefined> {
|
|
return this.__services.asObservable();
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Public methods
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Get Services
|
|
*
|
|
*
|
|
* @param page
|
|
* @param size
|
|
* @param sort
|
|
* @param order
|
|
* @param search
|
|
*/
|
|
getServices(
|
|
page: number = 0,
|
|
size: number = 10,
|
|
sort: string = 'name',
|
|
order: 'asc' | 'desc' | '' = 'asc',
|
|
search: string = ''
|
|
): Observable<{
|
|
pagination: ServicePagination;
|
|
services: Service[];
|
|
}> {
|
|
return this._httpClient
|
|
.get<{
|
|
pagination: ServicePagination;
|
|
services: Service[];
|
|
}>('api/apps/board/service/services', {
|
|
params: {
|
|
page: '' + page,
|
|
size: '' + size,
|
|
sort,
|
|
order,
|
|
search,
|
|
},
|
|
})
|
|
.pipe(
|
|
tap((response) => {
|
|
this.__pagination.next(response.pagination);
|
|
this.__services.next(response.services);
|
|
})
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get product by id
|
|
*/
|
|
getServiceById(id: string | null): Observable<Service> {
|
|
return this.__services.pipe(
|
|
take(1),
|
|
map((services) => {
|
|
// Find the product
|
|
const service = services?.find((item) => item.id === id) || undefined;
|
|
|
|
// Update the product
|
|
this.__service.next(service);
|
|
|
|
// Return the product
|
|
return service;
|
|
}),
|
|
switchMap((product) => {
|
|
if (!product) {
|
|
return throwError('Could not found product with id of ' + id + '!');
|
|
}
|
|
|
|
return of(product);
|
|
})
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create product
|
|
*/
|
|
createService(): Observable<Service> {
|
|
return this.services$.pipe(
|
|
take(1),
|
|
switchMap((services) =>
|
|
this._httpClient
|
|
.post<Service>('api/apps/board/service/product', {})
|
|
.pipe(
|
|
map((newService) => {
|
|
// Update the services with the new product
|
|
if (!!services) {
|
|
this.__services.next([newService, ...services]);
|
|
}
|
|
|
|
// Return the new product
|
|
return newService;
|
|
})
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|