Actualización CRUD, módulo Usuarios
This commit is contained in:
@ -1,5 +1,4 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
|
||||||
import { UserService } from "./user.service";
|
import { UserService } from "./user.service";
|
||||||
|
|
||||||
const userService = new UserService();
|
const userService = new UserService();
|
||||||
@ -10,57 +9,116 @@ export class UserController {
|
|||||||
req:Request,
|
req:Request,
|
||||||
res:Response
|
res:Response
|
||||||
) {
|
) {
|
||||||
const users =
|
try {
|
||||||
await userService.getAll();
|
const users = await userService.getAll();
|
||||||
|
|
||||||
res.json(users);
|
res.json(users);
|
||||||
|
|
||||||
|
}catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al obtener usuarios",
|
||||||
|
error
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getById(
|
async getById(
|
||||||
req:Request,
|
req:Request,
|
||||||
res:Response
|
res:Response
|
||||||
){
|
){
|
||||||
const user =
|
try {
|
||||||
await userService.getById(
|
|
||||||
|
const user = await userService.getById(
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({
|
||||||
|
message: "Usuario no encontrado"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
res.json(user);
|
res.json(user);
|
||||||
|
|
||||||
|
} catch(error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al obtener usuarios",
|
||||||
|
error
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async create (
|
async create (
|
||||||
req:Request,
|
req:Request,
|
||||||
res:Response
|
res:Response
|
||||||
) {
|
) {
|
||||||
const user =
|
try {
|
||||||
await userService.create(
|
const user =
|
||||||
req.body
|
await userService.create(
|
||||||
);
|
req.body
|
||||||
res.status(201).json(user);
|
);
|
||||||
|
res.status(201).json(user);
|
||||||
|
|
||||||
|
}catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al crear usuario",
|
||||||
|
error
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(
|
async update(
|
||||||
req:Request,
|
req:Request,
|
||||||
res:Response
|
res:Response
|
||||||
){
|
){
|
||||||
const user =
|
try{
|
||||||
await userService.update(
|
const user =
|
||||||
req.params.id as string,
|
await userService.update(
|
||||||
req.body
|
req.params.id as string,
|
||||||
);
|
req.body
|
||||||
|
);
|
||||||
|
|
||||||
res.json(user);
|
if(!user){
|
||||||
|
return res.status(404).json({
|
||||||
|
message:"Usuario no encontrado"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
res.status(200).json(user);
|
||||||
|
}catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al actualizar usuario",
|
||||||
|
error
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async detele (
|
async delete (
|
||||||
req:Request,
|
req:Request,
|
||||||
res:Response
|
res:Response
|
||||||
) {
|
) {
|
||||||
await userService.delete(
|
|
||||||
req.params.id as string
|
|
||||||
);
|
|
||||||
|
|
||||||
res.json({message: "Usuario eliminado"});
|
try{
|
||||||
|
const user = await userService.delete(
|
||||||
|
req.params.id as string
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!user){
|
||||||
|
return res.status(404).json({
|
||||||
|
message:"Usuario no encontrado"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
message: "Usuario eliminado"});
|
||||||
|
|
||||||
|
}catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al eliminar usuario",
|
||||||
|
error
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,11 +1,16 @@
|
|||||||
import { ObjectId } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
|
|
||||||
export interface IUser {
|
export interface IUser {
|
||||||
|
|
||||||
employeeId:number;
|
employeeId: number;
|
||||||
userName:string;
|
|
||||||
passwordHash:string;
|
userName: string;
|
||||||
roleId:ObjectId;
|
|
||||||
active:boolean;
|
passwordHash: string;
|
||||||
lastLogin:Date;
|
|
||||||
|
roleId: Types.ObjectId;
|
||||||
|
|
||||||
|
active: boolean;
|
||||||
|
|
||||||
|
lastLogin?: Date;
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ const userSchema = new mongoose.Schema({
|
|||||||
},
|
},
|
||||||
|
|
||||||
roleId:{
|
roleId:{
|
||||||
type:mongoose.Types.ObjectId,
|
type:mongoose.Schema.Types.ObjectId,
|
||||||
ref:"Role",
|
ref:"Role",
|
||||||
required:true
|
required:true
|
||||||
},
|
},
|
||||||
|
|||||||
@ -11,10 +11,10 @@ userRouter.get("/", authMiddleware, controller.getAll);
|
|||||||
|
|
||||||
userRouter.get("/:id", authMiddleware, controller.getById);
|
userRouter.get("/:id", authMiddleware, controller.getById);
|
||||||
|
|
||||||
userRouter.post("/", authMiddleware, controller.create);
|
userRouter.post("/", controller.create);
|
||||||
|
|
||||||
userRouter.put("/:id", controller.update);
|
userRouter.put("/:id", authMiddleware, controller.update);
|
||||||
|
|
||||||
userRouter.delete("/:id", controller.detele);
|
userRouter.delete("/:id", authMiddleware, controller.delete);
|
||||||
|
|
||||||
export default userRouter;
|
export default userRouter;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
import bcrypt from "bcrypt";
|
import bcrypt from "bcrypt";
|
||||||
import userModel from "./user.model";
|
import userModel from "./user.model";
|
||||||
|
import { IUser } from "./user.interface";
|
||||||
|
|
||||||
export class UserService {
|
export class UserService {
|
||||||
|
|
||||||
@ -14,12 +15,12 @@ export class UserService {
|
|||||||
id:string
|
id:string
|
||||||
) {
|
) {
|
||||||
return await userModel
|
return await userModel
|
||||||
.find()
|
.findById(id)
|
||||||
.populate("roleId");
|
.populate("roleId");
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(
|
async create(
|
||||||
data:any
|
data:IUser
|
||||||
) {
|
) {
|
||||||
const passwordHash = await bcrypt.hash(
|
const passwordHash = await bcrypt.hash(
|
||||||
data.passwordHash,
|
data.passwordHash,
|
||||||
@ -33,8 +34,14 @@ export class UserService {
|
|||||||
|
|
||||||
async update(
|
async update(
|
||||||
id:string,
|
id:string,
|
||||||
data:any
|
data:Partial<IUser>
|
||||||
) {
|
) {
|
||||||
|
if(data.passwordHash){
|
||||||
|
data.passwordHash = await bcrypt.hash(
|
||||||
|
data.passwordHash,
|
||||||
|
10
|
||||||
|
);
|
||||||
|
}
|
||||||
return await userModel.findByIdAndUpdate(
|
return await userModel.findByIdAndUpdate(
|
||||||
id,
|
id,
|
||||||
data,
|
data,
|
||||||
|
|||||||
Reference in New Issue
Block a user