From bc7fd9fc4bf1bc234e7e31e2410710cca400f55d Mon Sep 17 00:00:00 2001 From: "s.ventura" Date: Fri, 19 Jan 2024 12:31:32 +0100 Subject: [PATCH] update delete and creation user --- db.json | 50 ++++---------------- src/app/services/user.service.ts | 6 ++- src/app/user-detail/user-detail.component.ts | 25 ++++------ src/app/users/users.component.ts | 9 +++- 4 files changed, 32 insertions(+), 58 deletions(-) diff --git a/db.json b/db.json index ae1895ea..0d40756c 100644 --- a/db.json +++ b/db.json @@ -1,45 +1,5 @@ { "users": [ - { - "id": "1", - "name": "Hidran1", - "lastname": "Arias", - "email": "hidran@gmail.com", - "fiscalcode": "RSAHRN72M22Z444S", - "province": "Torino", - "phone": "454545455", - "age": 43 - }, - { - "id": "2", - "name": "Hidran2", - "lastname": "Arias", - "email": "hidran@gmail.com", - "fiscalcode": "RSAHRN72M22Z444S", - "province": "Torino", - "phone": "454545455", - "age": 43 - }, - { - "id": "3", - "name": "Hidran3", - "lastname": "Arias", - "email": "hidran@gmail.com", - "fiscalcode": "RSAHRN72M22Z444S", - "province": "Torino", - "phone": "454545455", - "age": 43 - }, - { - "id": "4", - "name": "Hidran4", - "lastname": "Arias", - "email": "hidran@gmail.com", - "fiscalcode": "RSAHRN72M22Z444S", - "province": "Torino", - "phone": "454545455", - "age": 43 - }, { "id": "b5be", "name": "Simone", @@ -49,6 +9,16 @@ "province": "Torino", "phone": "454545455", "age": 12 + }, + { + "id": 0, + "name": "Giulia", + "lastname": "Diomede", + "email": "flavio.bonta@gmail.com", + "fiscalcode": "675t967t6g", + "province": "pe", + "phone": "23453453454", + "age": 34 } ] } \ No newline at end of file diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index 28863be8..af7910e0 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -4,12 +4,13 @@ import { UserInterface } from "../interfaces/user"; import { HttpClient } from "@angular/common/http"; import {environment} from '../../environments/environment' import { Observable } from "rxjs"; +import { Router } from "@angular/router"; @Injectable({ providedIn: 'root' }) export class UserService{ apiurl = environment.APIURL - constructor(private http:HttpClient){ + constructor(private http:HttpClient, private router:Router){ } @@ -29,7 +30,10 @@ export class UserService{ } deleteUser(user: User){ + alert(user.id); return this.http.delete(this.apiurl + '/' + user.id); + + } createUser(user: UserInterface): Observable{ diff --git a/src/app/user-detail/user-detail.component.ts b/src/app/user-detail/user-detail.component.ts index ba189d01..c9aa7cee 100644 --- a/src/app/user-detail/user-detail.component.ts +++ b/src/app/user-detail/user-detail.component.ts @@ -31,32 +31,27 @@ export class UserDetailComponent implements OnInit { ngOnInit(){ this.route.params.subscribe( param => { + if(param['id']){ const id = Number(param['id']); this.userService.getUser(id) .subscribe(user => this.user = user); } - ) + } ) + } - saveUser(){ - alert(this.user.id) - - + saveUser(){ + let obs; if(this.user.id >0){ - alert(this.user.id) - this.userService.updateUser(this.user); - + obs=this.userService.updateUser(this.user); } else{ - - this.userService.createUser(this.user); - - + obs=this.userService.createUser(this.user); } - this.router.navigateByUrl('/users') - - + obs.subscribe(resp => { + this.router.navigateByUrl('/users') + }) } resetForm(form: { reset: () => void; }){ if(this.user.id === 0){ diff --git a/src/app/users/users.component.ts b/src/app/users/users.component.ts index d339f1b1..17a88e7a 100644 --- a/src/app/users/users.component.ts +++ b/src/app/users/users.component.ts @@ -2,6 +2,7 @@ import { Component, EventEmitter, OnInit, Output } from "@angular/core"; import { UserService } from "../services/user.service"; import { User } from "../classes/user"; import { Observable } from "rxjs"; +import { Router } from "@angular/router"; @Component( { selector: 'app-users', @@ -21,7 +22,7 @@ export class UsersComponent implements OnInit { public users$:Observable = this.service.getUsers(); @Output () updateUser = new EventEmitter(); - constructor(private service: UserService){ + constructor(private service: UserService, private router: Router){ } ngOnInit(){ @@ -29,7 +30,11 @@ export class UsersComponent implements OnInit { } onDeleteUser(user:User){ - this.service.deleteUser(user); + this.service.deleteUser(user).subscribe(res =>{ + location.reload(); + } + ); + }