Skip to content

Commit

Permalink
Mensajes - Implementado "MessagePrimeNgService"
Browse files Browse the repository at this point in the history
- Ahora todas las peticiones mostrarán un mensaje en caso de error y solo algunas peticiones mostrarán un mensaje en caso de éxito.
  • Loading branch information
nmarulo committed Dec 29, 2024
1 parent 20374c8 commit 4088517
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 16 deletions.
14 changes: 12 additions & 2 deletions src/app/services/pages/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<LoginAuthenticationRes>(`${this._authURI}/login`, request);
return this.httpClient.post<LoginAuthenticationRes>(`${this._authURI}/login`, request)
.pipe(
tap({
error: ({error}) => this.messagePrimeNgService.showError(error.error.detail)
})
);
}

}
22 changes: 19 additions & 3 deletions src/app/services/pages/products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<FindAllShoppingListProductsRes>(`${this._shoppingListProductsURI}/${id}`);
return this.httpClient.get<FindAllShoppingListProductsRes>(`${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')
})
);
}

}
68 changes: 59 additions & 9 deletions src/app/services/pages/shopping-lists.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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'
Expand All @@ -20,50 +22,98 @@ 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) {
return this.httpClient.get<FindAllShoppingListRes>(this._shoppingListsURI, {
params: {
...request
}
});
})
.pipe(
tap({
error: ({error}) => this.messagePrimeNgService.showError(error.error.detail)
})
);
}

findById(id: number, request?: FindByIdProductListReq) {
return this.httpClient.get<FindByIdShoppingListRes>(`${this._shoppingListsURI}/${id}`, {
params: {
...request
}
});
})
.pipe(
tap({
error: ({error}) => this.messagePrimeNgService.showError(error.error.detail)
})
);
}

update(id: number, request: UpdateShoppingListReq) {
return this.httpClient.put<UpdateShoppingListRes>(`${this._shoppingListsURI}/${id}`, request);
return this.httpClient.put<UpdateShoppingListRes>(`${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<SaveShoppingListRes>(this._shoppingListsURI, request);
return this.httpClient.post<SaveShoppingListRes>(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<void>(`${this._shoppingListsURI}/${id}`);
return this.httpClient.delete<void>(`${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) {
return this.httpClient.get<FindByIdProductListRes>(`${this._shoppingListsURI}/${id}/products`, {
params: {
...request
}
});
})
.pipe(
tap({
error: ({error}) => this.messagePrimeNgService.showError(error.error.detail)
})
);
}

updateSelectedProducts(id: number, request: ProductsSelectedReq) {
return this.httpClient.put<UpdateShoppingListRes>(`${this._shoppingListsURI}/${id}/products-selected`, request);
return this.httpClient.put<UpdateShoppingListRes>(`${this._shoppingListsURI}/${id}/products-selected`, request)
.pipe(
tap({
error: ({error}) => this.messagePrimeNgService.showError(error.error.detail),
complete: () => this.messagePrimeNgService.showSuccess('Productos desmarcados')
})
);
}
}
14 changes: 12 additions & 2 deletions src/app/services/pages/unit-types.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<FindAllUnitTypesRes>(this._unitTypesURI);
return this.httpClient.get<FindAllUnitTypesRes>(this._unitTypesURI)
.pipe(
tap({
error: ({error}) => this.messagePrimeNgService.showError(error.error.detail)
})
);
}

}

0 comments on commit 4088517

Please sign in to comment.