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": [ "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", "id": "b5be",
"name": "Simone", "name": "Simone",
@ -49,6 +9,16 @@
"province": "Torino", "province": "Torino",
"phone": "454545455", "phone": "454545455",
"age": 12 "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 { HttpClient } from "@angular/common/http";
import {environment} from '../../environments/environment' import {environment} from '../../environments/environment'
import { Observable } from "rxjs"; import { Observable } from "rxjs";
import { Router } from "@angular/router";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class UserService{ export class UserService{
apiurl = environment.APIURL apiurl = environment.APIURL
constructor(private http:HttpClient){ constructor(private http:HttpClient, private router:Router){
} }
@ -29,7 +30,10 @@ export class UserService{
} }
deleteUser(user: User){ deleteUser(user: User){
alert(user.id);
return this.http.delete<User[]>(this.apiurl + '/' + user.id); return this.http.delete<User[]>(this.apiurl + '/' + user.id);
} }
createUser(user: UserInterface): Observable<User []>{ createUser(user: UserInterface): Observable<User []>{

View File

@ -31,32 +31,27 @@ export class UserDetailComponent implements OnInit {
ngOnInit(){ ngOnInit(){
this.route.params.subscribe( param => { this.route.params.subscribe( param => {
if(param['id']){
const id = Number(param['id']); const id = Number(param['id']);
this.userService.getUser(id) this.userService.getUser(id)
.subscribe(user => this.user = user); .subscribe(user => this.user = user);
} }
) } )
} }
saveUser(){ saveUser(){
alert(this.user.id) let obs;
if(this.user.id >0){ if(this.user.id >0){
alert(this.user.id) obs=this.userService.updateUser(this.user);
this.userService.updateUser(this.user);
} }
else{ else{
obs=this.userService.createUser(this.user);
this.userService.createUser(this.user);
} }
this.router.navigateByUrl('/users') obs.subscribe(resp => {
this.router.navigateByUrl('/users')
})
} }
resetForm(form: { reset: () => void; }){ resetForm(form: { reset: () => void; }){
if(this.user.id === 0){ 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 { UserService } from "../services/user.service";
import { User } from "../classes/user"; import { User } from "../classes/user";
import { Observable } from "rxjs"; import { Observable } from "rxjs";
import { Router } from "@angular/router";
@Component( { @Component( {
selector: 'app-users', selector: 'app-users',
@ -21,7 +22,7 @@ export class UsersComponent implements OnInit {
public users$:Observable<User[]> = this.service.getUsers(); public users$:Observable<User[]> = this.service.getUsers();
@Output () updateUser = new EventEmitter<User>(); @Output () updateUser = new EventEmitter<User>();
constructor(private service: UserService){ constructor(private service: UserService, private router: Router){
} }
ngOnInit(){ ngOnInit(){
@ -29,7 +30,11 @@ export class UsersComponent implements OnInit {
} }
onDeleteUser(user:User){ onDeleteUser(user:User){
this.service.deleteUser(user); this.service.deleteUser(user).subscribe(res =>{
location.reload();
}
);
} }