Last active
October 18, 2020 17:17
-
-
Save nicolasleal570/1971242d5fd866150030618153778d1d to your computer and use it in GitHub Desktop.
Editando un poco el servicio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { User } from '../models/user'; | |
@Injectable({ | |
providedIn: 'root', | |
}) | |
export class UsersService { | |
/** | |
* LISTA DE USUARIOS | |
*/ | |
users: Array<User> = []; | |
/** | |
* CONSTRUCTOR DEL SERVICIO | |
*/ | |
constructor() {} | |
/** | |
* MÉTODO PARA OBTENER TODOS LOS USUARIOS | |
*/ | |
getAllUsers(): Array<User> { | |
return this.users; | |
} | |
/** | |
* MÉTODO PARA OBTENER UN USUARIO ESPECÍFICO | |
* @param userId ID del usuario que queremos obtener | |
*/ | |
getUserById(userId: number): User { | |
return this.users.find((usr) => usr.id === userId); | |
} | |
/** | |
* MÉTODO PARA CREAR UN USUARIO NUEVO | |
* @param user datos del nuevo usuario | |
*/ | |
createNewUser(user: User): void { | |
this.users.push(user); | |
} | |
/** | |
* MÉTODO PARA MODIFICAR UN USUARIO ESPECÍFICO | |
* @param userId | |
* @param data | |
*/ | |
updateUser(userId: number, data: User): User { | |
let user = this.users.find((usr) => usr.id === userId); | |
user = { | |
...user, | |
...data, | |
}; | |
return user; | |
} | |
/** | |
* MÉTODO PARA ELIMINAR UN USUARIO ESPECÍFICO | |
* @param userId ID del usuario que queremos eliminar | |
*/ | |
deleteUser(userId: number): Array<User> { | |
return this.users.filter((usr) => usr.id !== userId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment