diff --git a/src/app/core/services/api-response.interface.ts b/src/app/core/services/api-response.interface.ts new file mode 100644 index 0000000..3db5cbb --- /dev/null +++ b/src/app/core/services/api-response.interface.ts @@ -0,0 +1,3 @@ +export interface ApiResponse { + data: T; +} diff --git a/src/app/core/services/catalog/catalog.interface.ts b/src/app/core/services/catalog/catalog.interface.ts new file mode 100644 index 0000000..0faebb2 --- /dev/null +++ b/src/app/core/services/catalog/catalog.interface.ts @@ -0,0 +1,11 @@ +export interface Product { + id: number; + tenant_codigo: string; + categoria_id: number; + slug: string; + nombre: string; + descripcion?: string; + precio: string; + created_at?: string; + updated_at?: string; +} diff --git a/src/app/core/services/catalog/catalog.service.ts b/src/app/core/services/catalog/catalog.service.ts new file mode 100644 index 0000000..3cbd974 --- /dev/null +++ b/src/app/core/services/catalog/catalog.service.ts @@ -0,0 +1,32 @@ +import { inject, Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { map, Observable } from 'rxjs'; + +import { ApiResponse } from '../api-response.interface'; +import { TenantService } from '../tenant.service'; +import { Product } from './catalog.interface'; + +@Injectable({ + providedIn: 'root' +}) +export class CatalogService { + private readonly http = inject(HttpClient); + private readonly tenantService = inject(TenantService); + + private get tenantApiUrl(): string { + return this.tenantService.getTenantApiUrl(); + } + + + getProductos(): Observable { + return this.http + .get>(`${this.tenantApiUrl}/productos`) + .pipe(map((response) => response.data)); + } + + getProducto(id: number): Observable { + return this.http + .get>(`${this.tenantApiUrl}/productos/${id}`) + .pipe(map((response) => response.data)); + } +} diff --git a/src/app/core/services/tenant.interface.ts b/src/app/core/services/tenant.interface.ts index 81babb3..f884e5c 100644 --- a/src/app/core/services/tenant.interface.ts +++ b/src/app/core/services/tenant.interface.ts @@ -1,3 +1,5 @@ +import { ApiResponse } from './api-response.interface'; + export interface Tenant { id: number; codigo: string; @@ -11,6 +13,5 @@ export interface Tenant { footer_logo: string; } -export interface TenantBootstrapResponse { - data: Tenant; -} +export type TenantBootstrapResponse = ApiResponse; + diff --git a/src/app/core/services/tenant.service.ts b/src/app/core/services/tenant.service.ts index 0eaf772..2fa2de1 100644 --- a/src/app/core/services/tenant.service.ts +++ b/src/app/core/services/tenant.service.ts @@ -40,6 +40,15 @@ export class TenantService { return this.tenantState(); } + getTenantApiUrl(): string { + const tenant = this.tenantState(); + if (!tenant) { + throw new Error('No se pudo resolver el tenant activo.'); + } + return `${environment.url}tenants/${tenant.codigo}`; + } + + async bootstrap(): Promise { if (this.statusState() !== 'idle') { return;