diff --git a/src/app/core/services/api-pagination-query-params.interface.ts b/src/app/core/services/api-pagination-query-params.interface.ts new file mode 100644 index 0000000..995a20a --- /dev/null +++ b/src/app/core/services/api-pagination-query-params.interface.ts @@ -0,0 +1,4 @@ +export interface ApiPaginationQueryParams { + page?: number; + per_page?: number; +} diff --git a/src/app/core/services/catalog/catalog.service.ts b/src/app/core/services/catalog/catalog.service.ts index 3cbd974..89d5769 100644 --- a/src/app/core/services/catalog/catalog.service.ts +++ b/src/app/core/services/catalog/catalog.service.ts @@ -1,11 +1,19 @@ 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 { ApiPaginationQueryParams } from '../api-pagination-query-params.interface'; +import { ApiPaginatedResponse } from '../api-paginated-response.interface'; import { ApiResponse } from '../api-response.interface'; import { TenantService } from '../tenant.service'; import { Product } from './catalog.interface'; +type HttpParamValue = + | string + | number + | boolean + | readonly (string | number | boolean)[]; + @Injectable({ providedIn: 'root' }) @@ -18,10 +26,13 @@ export class CatalogService { } - getProductos(): Observable { - return this.http - .get>(`${this.tenantApiUrl}/productos`) - .pipe(map((response) => response.data)); + getProductos( + params?: ApiPaginationQueryParams + ): Observable> { + return this.http.get>( + `${this.tenantApiUrl}/productos`, + { params: this.buildHttpParams(params) } + ); } getProducto(id: number): Observable { @@ -29,4 +40,23 @@ export class CatalogService { .get>(`${this.tenantApiUrl}/productos/${id}`) .pipe(map((response) => response.data)); } + + private buildHttpParams(params?: ApiPaginationQueryParams): HttpParams { + if (!params) { + return new HttpParams(); + } + + const fromObject = Object.entries(params).reduce>( + (acc, [key, value]) => { + if (value !== undefined) { + acc[key] = value; + } + + return acc; + }, + {} + ); + + return new HttpParams({ fromObject }); + } }