162 lines
4.0 KiB
TypeScript
162 lines
4.0 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 { SessioninInfo } from '../models/sessionin-info';
|
|
import { SessioninInfoPagination } from '../models/sessionin-info-pagination';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class SessioninInfoService {
|
|
// Private
|
|
private __pagination = new BehaviorSubject<
|
|
SessioninInfoPagination | undefined
|
|
>(undefined);
|
|
private __sessioninInfo = new BehaviorSubject<SessioninInfo | undefined>(
|
|
undefined
|
|
);
|
|
private __sessioninInfos = new BehaviorSubject<SessioninInfo[] | undefined>(
|
|
undefined
|
|
);
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
constructor(private _httpClient: HttpClient) {}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Accessors
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Getter for pagination
|
|
*/
|
|
get pagination$(): Observable<SessioninInfoPagination | undefined> {
|
|
return this.__pagination.asObservable();
|
|
}
|
|
|
|
/**
|
|
* Getter for sessioninInfo
|
|
*/
|
|
get sessioninInfo$(): Observable<SessioninInfo | undefined> {
|
|
return this.__sessioninInfo.asObservable();
|
|
}
|
|
|
|
/**
|
|
* Getter for sessioninInfos
|
|
*/
|
|
get sessioninInfos$(): Observable<SessioninInfo[] | undefined> {
|
|
return this.__sessioninInfos.asObservable();
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Public methods
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Get SessioninInfos
|
|
*
|
|
*
|
|
* @param page
|
|
* @param size
|
|
* @param sort
|
|
* @param order
|
|
* @param search
|
|
*/
|
|
getSessioninInfos(
|
|
page: number = 0,
|
|
size: number = 10,
|
|
sort: string = 'name',
|
|
order: 'asc' | 'desc' | '' = 'asc',
|
|
search: string = ''
|
|
): Observable<{
|
|
pagination: SessioninInfoPagination;
|
|
sessioninInfos: SessioninInfo[];
|
|
}> {
|
|
return this._httpClient
|
|
.get<{
|
|
pagination: SessioninInfoPagination;
|
|
sessioninInfos: SessioninInfo[];
|
|
}>('api/apps/report/sessionin-info/sessionin-infos', {
|
|
params: {
|
|
page: '' + page,
|
|
size: '' + size,
|
|
sort,
|
|
order,
|
|
search,
|
|
},
|
|
})
|
|
.pipe(
|
|
tap((response) => {
|
|
this.__pagination.next(response.pagination);
|
|
this.__sessioninInfos.next(response.sessioninInfos);
|
|
})
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get product by id
|
|
*/
|
|
getSessioninInfoById(id: string | null): Observable<SessioninInfo> {
|
|
return this.__sessioninInfos.pipe(
|
|
take(1),
|
|
map((sessioninInfos) => {
|
|
// Find the product
|
|
const sessioninInfo =
|
|
sessioninInfos?.find((item) => item.id === id) || undefined;
|
|
|
|
// Update the product
|
|
this.__sessioninInfo.next(sessioninInfo);
|
|
|
|
// Return the product
|
|
return sessioninInfo;
|
|
}),
|
|
switchMap((product) => {
|
|
if (!product) {
|
|
return throwError('Could not found product with id of ' + id + '!');
|
|
}
|
|
|
|
return of(product);
|
|
})
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create product
|
|
*/
|
|
createSessioninInfo(): Observable<SessioninInfo> {
|
|
return this.sessioninInfos$.pipe(
|
|
take(1),
|
|
switchMap((sessioninInfos) =>
|
|
this._httpClient
|
|
.post<SessioninInfo>('api/apps/report/sessionin-info/product', {})
|
|
.pipe(
|
|
map((newSessioninInfo) => {
|
|
// Update the sessioninInfos with the new product
|
|
if (!!sessioninInfos) {
|
|
this.__sessioninInfos.next([
|
|
newSessioninInfo,
|
|
...sessioninInfos,
|
|
]);
|
|
}
|
|
|
|
// Return the new product
|
|
return newSessioninInfo;
|
|
})
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|