Skip to content

Commit

Permalink
feat: add update products
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Brasileiro authored and Fabio Brasileiro committed Oct 28, 2024
1 parent 547224d commit a8ca0a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/app/core/services/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { environment } from 'src/environments/environment';
})
export class ProductService {
private apiUrl = `${environment.apiUrl}/api/products`;
private apiUrlUpdate = `${environment.apiUrl}/api/`;

constructor(private http: HttpClient) { }

Expand All @@ -23,13 +22,25 @@ export class ProductService {


updateProducts(formData: any): Observable<any> {
const {id}= formData
const token = localStorage.getItem('token');
const headers = new HttpHeaders({
Authorization: `Bearer ${token}`,
});

return this.http.get<any>(this.apiUrl, { headers });
return this.http.put<any>(`${this.apiUrl}/${id}`, formData, { headers });
}

deleteProducts(formData: any): Observable<any> {
const { id } = formData
const token = localStorage.getItem('token');
const headers = new HttpHeaders({
Authorization: `Bearer ${token}`,
});

return this.http.delete<any>(`${this.apiUrl}/${id}`, { headers });
}

// Método para enviar os dados do produto para a API
sendDataToApi(data: any): Observable<any> {
console.log("🚀 ~ ProductService ~ sendDataToApi ~ data:", data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,9 @@ export class AddProductComponent implements OnInit {
formData.id = this.product.id; // Inclui o ID do produto a ser atualizado
this.productService.updateProducts(formData).subscribe(
(response) => {
const index = this.products.findIndex(p => p.id === formData.id);
if (index !== -1) this.products[index] = response;
console.log(response)
// const index = this.products.findIndex(p => p.id === formData.id);
// if (index !== -1) this.products[index] = response;
this.showSuccess('Product updated successfully');
this.closeDialog();
},
Expand Down

0 comments on commit a8ca0a6

Please sign in to comment.