feat: add ApiPaginationQueryParams interface and update CatalogService to support pagination
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
export interface ApiPaginationQueryParams {
|
||||||
|
page?: number;
|
||||||
|
per_page?: number;
|
||||||
|
}
|
||||||
@@ -1,11 +1,19 @@
|
|||||||
import { inject, Injectable } from '@angular/core';
|
import { inject, Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||||
import { map, Observable } from 'rxjs';
|
import { map, Observable } from 'rxjs';
|
||||||
|
|
||||||
|
import { ApiPaginationQueryParams } from '../api-pagination-query-params.interface';
|
||||||
|
import { ApiPaginatedResponse } from '../api-paginated-response.interface';
|
||||||
import { ApiResponse } from '../api-response.interface';
|
import { ApiResponse } from '../api-response.interface';
|
||||||
import { TenantService } from '../tenant.service';
|
import { TenantService } from '../tenant.service';
|
||||||
import { Product } from './catalog.interface';
|
import { Product } from './catalog.interface';
|
||||||
|
|
||||||
|
type HttpParamValue =
|
||||||
|
| string
|
||||||
|
| number
|
||||||
|
| boolean
|
||||||
|
| readonly (string | number | boolean)[];
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
@@ -18,10 +26,13 @@ export class CatalogService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
getProductos(): Observable<Product[]> {
|
getProductos(
|
||||||
return this.http
|
params?: ApiPaginationQueryParams
|
||||||
.get<ApiResponse<Product[]>>(`${this.tenantApiUrl}/productos`)
|
): Observable<ApiPaginatedResponse<Product[]>> {
|
||||||
.pipe(map((response) => response.data));
|
return this.http.get<ApiPaginatedResponse<Product[]>>(
|
||||||
|
`${this.tenantApiUrl}/productos`,
|
||||||
|
{ params: this.buildHttpParams(params) }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getProducto(id: number): Observable<Product> {
|
getProducto(id: number): Observable<Product> {
|
||||||
@@ -29,4 +40,23 @@ export class CatalogService {
|
|||||||
.get<ApiResponse<Product>>(`${this.tenantApiUrl}/productos/${id}`)
|
.get<ApiResponse<Product>>(`${this.tenantApiUrl}/productos/${id}`)
|
||||||
.pipe(map((response) => response.data));
|
.pipe(map((response) => response.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private buildHttpParams(params?: ApiPaginationQueryParams): HttpParams {
|
||||||
|
if (!params) {
|
||||||
|
return new HttpParams();
|
||||||
|
}
|
||||||
|
|
||||||
|
const fromObject = Object.entries(params).reduce<Record<string, HttpParamValue>>(
|
||||||
|
(acc, [key, value]) => {
|
||||||
|
if (value !== undefined) {
|
||||||
|
acc[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
|
||||||
|
return new HttpParams({ fromObject });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user