products.service.ts (1095B)
1 import { HttpClient, HttpParams } from '@angular/common/http'; 2 import { inject, Injectable } from '@angular/core'; 3 import { Observable } from 'rxjs'; 4 5 @Injectable({ 6 providedIn: 'root', 7 }) 8 export class ProductService { 9 private http = inject(HttpClient); 10 11 // Approach 1: Using TypeScript Decorators 12 13 getProducts(page: number = 1, pageSize: number = 10): Observable<PaginatedResponse<ProductCard>> { 14 const params = new HttpParams().set('page', page.toString()).set('page_size', pageSize.toString()); 15 16 return this.http.get<PaginatedResponse<ProductCard>>('/api/products', { params }); 17 } 18 19 //@Busy({ message: 'Loading product details...' }) 20 getProduct(id: string): Observable<Product> { 21 return this.http.get<Product>(`/api/products/${id}`); 22 } 23 24 //@Busy({ message: 'Searching products...' }) 25 searchProducts(query: string, page: number = 1): Observable<PaginatedResponse<ProductCard>> { 26 const params = new HttpParams().set('q', query).set('page', page.toString()); 27 28 return this.http.get<PaginatedResponse<ProductCard>>('/api/products/search', { params }); 29 } 30 }