feat: add ApiPaginationQueryParams interface and update CatalogService to support pagination

This commit is contained in:
2026-06-29 16:21:19 -03:00
parent fe4eebb685
commit 5715977b25
2 changed files with 39 additions and 5 deletions

View File

@@ -0,0 +1,4 @@
export interface ApiPaginationQueryParams {
page?: number;
per_page?: number;
}

View File

@@ -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<Product[]> {
return this.http
.get<ApiResponse<Product[]>>(`${this.tenantApiUrl}/productos`)
.pipe(map((response) => response.data));
getProductos(
params?: ApiPaginationQueryParams
): Observable<ApiPaginatedResponse<Product[]>> {
return this.http.get<ApiPaginatedResponse<Product[]>>(
`${this.tenantApiUrl}/productos`,
{ params: this.buildHttpParams(params) }
);
}
getProducto(id: number): Observable<Product> {
@@ -29,4 +40,23 @@ export class CatalogService {
.get<ApiResponse<Product>>(`${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<Record<string, HttpParamValue>>(
(acc, [key, value]) => {
if (value !== undefined) {
acc[key] = value;
}
return acc;
},
{}
);
return new HttpParams({ fromObject });
}
}