From 40885173541769f98e8858a2e90a4a956bedbae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Marulanda=20P?= Date: Mon, 30 Dec 2024 00:25:04 +0100 Subject: [PATCH] =?UTF-8?q?Mensajes=20-=20Implementado=20"MessagePrimeNgSe?= =?UTF-8?q?rvice"=20-=20Ahora=20todas=20las=20peticiones=20mostrar=C3=A1n?= =?UTF-8?q?=20un=20mensaje=20en=20caso=20de=20error=20y=20solo=20algunas?= =?UTF-8?q?=20peticiones=20mostrar=C3=A1n=20un=20mensaje=20en=20caso=20de?= =?UTF-8?q?=20=C3=A9xito.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../services/pages/authentication.service.ts | 14 +++- src/app/services/pages/products.service.ts | 22 +++++- .../services/pages/shopping-lists.service.ts | 68 ++++++++++++++++--- src/app/services/pages/unit-types.service.ts | 14 +++- 4 files changed, 102 insertions(+), 16 deletions(-) diff --git a/src/app/services/pages/authentication.service.ts b/src/app/services/pages/authentication.service.ts index d8e0a24..671eb08 100644 --- a/src/app/services/pages/authentication.service.ts +++ b/src/app/services/pages/authentication.service.ts @@ -3,6 +3,8 @@ import {HttpClient} from '@angular/common/http'; import {environment} from '@env/environment'; import {LoginAuthenticationReq} from '@app/models/login-authentication-req'; import {LoginAuthenticationRes} from '@app/models/login-authentication-res'; +import {tap} from 'rxjs'; +import {MessagePrimeNgService} from '@app/services/external/message-prime-ng.service'; @Injectable({ providedIn: 'root' @@ -11,11 +13,19 @@ export class AuthenticationService { private readonly _authURI = `${environment.DESPENSA_REST_API_URL}/auth`; - constructor(private httpClient: HttpClient) { + constructor( + private httpClient: HttpClient, + private messagePrimeNgService: MessagePrimeNgService + ) { } login(request: LoginAuthenticationReq) { - return this.httpClient.post(`${this._authURI}/login`, request); + return this.httpClient.post(`${this._authURI}/login`, request) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail) + }) + ); } } diff --git a/src/app/services/pages/products.service.ts b/src/app/services/pages/products.service.ts index 4b31715..40e51e0 100644 --- a/src/app/services/pages/products.service.ts +++ b/src/app/services/pages/products.service.ts @@ -3,6 +3,8 @@ import {environment} from '@env/environment'; import {HttpClient} from '@angular/common/http'; import {FindAllShoppingListProductsRes} from '@app/models/find-all-shopping-list-products-res'; import {SaveShoppingListProductReq} from '@app/models/save-shopping-list-product-req'; +import {tap} from 'rxjs'; +import {MessagePrimeNgService} from '@app/services/external/message-prime-ng.service'; @Injectable({ providedIn: 'root' @@ -11,15 +13,29 @@ export class ProductsService { private readonly _shoppingListProductsURI = `${environment.DESPENSA_REST_API_URL}/products/shopping-list`; - constructor(private httpClient: HttpClient) { + constructor( + private httpClient: HttpClient, + private messagePrimeNgService: MessagePrimeNgService + ) { } findAllShoppingList(id: number) { - return this.httpClient.get(`${this._shoppingListProductsURI}/${id}`); + return this.httpClient.get(`${this._shoppingListProductsURI}/${id}`) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail) + }) + ); } saveShoppingList(request: SaveShoppingListProductReq) { - return this.httpClient.post(this._shoppingListProductsURI, request); + return this.httpClient.post(this._shoppingListProductsURI, request) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail), + complete: () => this.messagePrimeNgService.showSuccess('Producto agregado') + }) + ); } } diff --git a/src/app/services/pages/shopping-lists.service.ts b/src/app/services/pages/shopping-lists.service.ts index 4933f07..049fd8e 100644 --- a/src/app/services/pages/shopping-lists.service.ts +++ b/src/app/services/pages/shopping-lists.service.ts @@ -1,5 +1,6 @@ import {Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; +import {tap} from 'rxjs'; import {FindAllShoppingListRes} from '@app/models/find-all-shopping-list-res'; import {FindByIdShoppingListRes} from '@app/models/find-by-id-shopping-list-res'; import {environment} from '@env/environment'; @@ -12,6 +13,7 @@ import {PagingAndSortingReq} from '@app/models/paging-and-sorting-req'; import {FindByIdProductListReq} from '@app/models/find-by-id-product-list-req'; import {FindByIdProductListRes} from '@app/models/find-by-id-product-list-res'; import {ProductsSelectedReq} from '@app/models/products-selected-req'; +import {MessagePrimeNgService} from '@app/services/external/message-prime-ng.service'; @Injectable({ providedIn: 'root' @@ -20,7 +22,10 @@ export class ShoppingListsService { private readonly _shoppingListsURI = `${environment.DESPENSA_REST_API_URL}/shopping-lists`; - constructor(private httpClient: HttpClient) { + constructor( + private httpClient: HttpClient, + private messagePrimeNgService: MessagePrimeNgService + ) { } findAll(request: PagingAndSortingReq) { @@ -28,7 +33,12 @@ export class ShoppingListsService { params: { ...request } - }); + }) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail) + }) + ); } findById(id: number, request?: FindByIdProductListReq) { @@ -36,23 +46,52 @@ export class ShoppingListsService { params: { ...request } - }); + }) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail) + }) + ); } update(id: number, request: UpdateShoppingListReq) { - return this.httpClient.put(`${this._shoppingListsURI}/${id}`, request); + return this.httpClient.put(`${this._shoppingListsURI}/${id}`, request) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail), + complete: () => this.messagePrimeNgService.showSuccess('Lista de compra actualizada') + }) + ); } deleteProducts(id: number, request: DeleteProductsShoppingListReq) { - return this.httpClient.delete(`${this._shoppingListsURI}/${id}/products`, {body: request}); + return this.httpClient.delete(`${this._shoppingListsURI}/${id}/products`, {body: request}) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail), + complete: () => this.messagePrimeNgService.showSuccess('Producto eliminado de la lista de compra') + }) + ); } save(request: SaveShoppingListReq) { - return this.httpClient.post(this._shoppingListsURI, request); + return this.httpClient.post(this._shoppingListsURI, request) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail), + complete: () => this.messagePrimeNgService.showSuccess('Lista de compra creada') + }) + ); } delete(id: number) { - return this.httpClient.delete(`${this._shoppingListsURI}/${id}`); + return this.httpClient.delete(`${this._shoppingListsURI}/${id}`) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail), + complete: () => this.messagePrimeNgService.showSuccess('Lista de compra eliminada') + }) + ); } findAllProducts(id: number, request: FindByIdProductListReq) { @@ -60,10 +99,21 @@ export class ShoppingListsService { params: { ...request } - }); + }) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail) + }) + ); } updateSelectedProducts(id: number, request: ProductsSelectedReq) { - return this.httpClient.put(`${this._shoppingListsURI}/${id}/products-selected`, request); + return this.httpClient.put(`${this._shoppingListsURI}/${id}/products-selected`, request) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail), + complete: () => this.messagePrimeNgService.showSuccess('Productos desmarcados') + }) + ); } } diff --git a/src/app/services/pages/unit-types.service.ts b/src/app/services/pages/unit-types.service.ts index 7953ef1..0c9f8df 100644 --- a/src/app/services/pages/unit-types.service.ts +++ b/src/app/services/pages/unit-types.service.ts @@ -2,6 +2,8 @@ import {Injectable} from '@angular/core'; import {environment} from '@env/environment'; import {HttpClient} from '@angular/common/http'; import {FindAllUnitTypesRes} from '@app/models/find-all-unit-types-res'; +import {tap} from 'rxjs'; +import {MessagePrimeNgService} from '@app/services/external/message-prime-ng.service'; @Injectable({ providedIn: 'root' @@ -10,11 +12,19 @@ export class UnitTypesService { private readonly _unitTypesURI = `${environment.DESPENSA_REST_API_URL}/unit-types`; - constructor(private httpClient: HttpClient) { + constructor( + private httpClient: HttpClient, + private messagePrimeNgService: MessagePrimeNgService + ) { } findAll() { - return this.httpClient.get(this._unitTypesURI); + return this.httpClient.get(this._unitTypesURI) + .pipe( + tap({ + error: ({error}) => this.messagePrimeNgService.showError(error.error.detail) + }) + ); } }