update delete and creation user

This commit is contained in:
Simone Ventura 2024-01-19 12:31:32 +01:00
parent b18af7a943
commit bc7fd9fc4b
4 changed files with 32 additions and 58 deletions

50
db.json
View File

@ -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
}
]
}

View File

@ -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<User[]>(this.apiurl + '/' + user.id);
}
createUser(user: UserInterface): Observable<User []>{

View File

@ -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)
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);
}
obs.subscribe(resp => {
this.router.navigateByUrl('/users')
})
}
resetForm(form: { reset: () => void; }){
if(this.user.id === 0){

View File

@ -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<User[]> = this.service.getUsers();
@Output () updateUser = new EventEmitter<User>();
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();
}
);
}