Implementar modulo de Roles y Usuarios
This commit is contained in:
13
backend/.env
Normal file
13
backend/.env
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
PORT=5000
|
||||||
|
|
||||||
|
MONGO_URI=mongodb://localhost:27017/sistema_combustible
|
||||||
|
|
||||||
|
JWT_SECRET=super_secret_key
|
||||||
|
|
||||||
|
SQL_SERVER=ABIGAILV\SQLEXPRESS
|
||||||
|
|
||||||
|
SQL_DATABASE=empresa_pan
|
||||||
|
|
||||||
|
SQL_USER=sa
|
||||||
|
|
||||||
|
SQL_PASSWORD=123456
|
||||||
@ -4,6 +4,7 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"dev": "ts-node-dev --respawn src/server.ts",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
|
|||||||
@ -0,0 +1,35 @@
|
|||||||
|
import express from "express";
|
||||||
|
import cors from "cors";
|
||||||
|
import roleRoutes from "./modules/roles/role.routes";
|
||||||
|
import userRoutes from "./modules/user/user.routes";
|
||||||
|
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
app.use(cors());
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
app.use("/api/roles", roleRoutes);
|
||||||
|
app.use("/api/users", userRoutes)
|
||||||
|
|
||||||
|
|
||||||
|
app.use(express.urlencoded({
|
||||||
|
extended:true
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get(
|
||||||
|
"/api/health",
|
||||||
|
(
|
||||||
|
req,
|
||||||
|
res
|
||||||
|
) => {
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
success:true,
|
||||||
|
message: "API funcionando correctamente"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export default app;
|
||||||
18
backend/src/config/mongodb.ts
Normal file
18
backend/src/config/mongodb.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
export const connectMongo = async (): Promise <void> => {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
await mongoose.connect(process.env.MONGO_URI!);
|
||||||
|
console.log("MongoDB conectado");
|
||||||
|
|
||||||
|
} catch(error){
|
||||||
|
|
||||||
|
console.error("Error MongoDB", error);
|
||||||
|
|
||||||
|
process.exit(1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
30
backend/src/config/sqlserver.ts
Normal file
30
backend/src/config/sqlserver.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*import sql from "mssql";
|
||||||
|
|
||||||
|
|
||||||
|
const sqlConfig:sql.config = {
|
||||||
|
|
||||||
|
user: process.env.SQL_USER,
|
||||||
|
password: process.env.SQL_PASSWORD,
|
||||||
|
server:process.env.SQL_SERVER!,
|
||||||
|
database:process.env.SQL_DATABASE,
|
||||||
|
|
||||||
|
options:{
|
||||||
|
trustServerCertificate:true,
|
||||||
|
encrypt:false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export const connectSqlServer = async() => {
|
||||||
|
|
||||||
|
try{
|
||||||
|
await sql.connect(sqlConfig);
|
||||||
|
console.log("SQL Server Conectado");
|
||||||
|
|
||||||
|
}catch(error){
|
||||||
|
console.error("Error SQL Server", error);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};*/
|
||||||
|
|
||||||
30
backend/src/modules/roles/role.controller.ts
Normal file
30
backend/src/modules/roles/role.controller.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
|
||||||
|
import { RoleService } from "./role.service";
|
||||||
|
|
||||||
|
const roleService = new RoleService();
|
||||||
|
|
||||||
|
export class RoleController {
|
||||||
|
|
||||||
|
async getAll(
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
){
|
||||||
|
const roles =
|
||||||
|
await roleService.getAll();
|
||||||
|
res.json(roles);
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(
|
||||||
|
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
){
|
||||||
|
const role =
|
||||||
|
await roleService.create(
|
||||||
|
req.body
|
||||||
|
);
|
||||||
|
|
||||||
|
res.status(201).json(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
6
backend/src/modules/roles/role.interface.ts
Normal file
6
backend/src/modules/roles/role.interface.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export interface IRole {
|
||||||
|
|
||||||
|
roleName:string;
|
||||||
|
description:string;
|
||||||
|
active:boolean;
|
||||||
|
}
|
||||||
26
backend/src/modules/roles/role.model.ts
Normal file
26
backend/src/modules/roles/role.model.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import mongoose, { Schema } from "mongoose";
|
||||||
|
|
||||||
|
const roleSchema = new mongoose.Schema({
|
||||||
|
|
||||||
|
roleName:{
|
||||||
|
type:String,
|
||||||
|
required:true,
|
||||||
|
unique:true
|
||||||
|
},
|
||||||
|
|
||||||
|
description:{
|
||||||
|
type:String
|
||||||
|
},
|
||||||
|
|
||||||
|
active:{
|
||||||
|
type:Boolean,
|
||||||
|
default:true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
timestamps:true
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export default mongoose.model("Role", roleSchema);
|
||||||
13
backend/src/modules/roles/role.routes.ts
Normal file
13
backend/src/modules/roles/role.routes.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
|
||||||
|
import { RoleController } from "./role.controller";
|
||||||
|
|
||||||
|
const roleRoutes = Router();
|
||||||
|
|
||||||
|
const controller = new RoleController();
|
||||||
|
|
||||||
|
roleRoutes.get("/", controller.getAll);
|
||||||
|
|
||||||
|
roleRoutes.post("/", controller.create);
|
||||||
|
|
||||||
|
export default roleRoutes ;
|
||||||
12
backend/src/modules/roles/role.service.ts
Normal file
12
backend/src/modules/roles/role.service.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import roleModel from "./role.model";
|
||||||
|
|
||||||
|
export class RoleService {
|
||||||
|
async getAll() {
|
||||||
|
|
||||||
|
return await roleModel.find();
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(data:any) {
|
||||||
|
return await roleModel.create(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
66
backend/src/modules/user/user.controller.ts
Normal file
66
backend/src/modules/user/user.controller.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
|
||||||
|
import { UserService } from "./user.service";
|
||||||
|
|
||||||
|
const userService = new UserService();
|
||||||
|
|
||||||
|
export class UserController {
|
||||||
|
|
||||||
|
async getAll(
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
) {
|
||||||
|
const users =
|
||||||
|
await userService.getAll();
|
||||||
|
|
||||||
|
res.json(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getById(
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
){
|
||||||
|
const user =
|
||||||
|
await userService.getById(
|
||||||
|
req.params.id as string
|
||||||
|
);
|
||||||
|
|
||||||
|
res.json(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
async create (
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
) {
|
||||||
|
const user =
|
||||||
|
await userService.create(
|
||||||
|
req.body
|
||||||
|
);
|
||||||
|
res.status(201).json(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
){
|
||||||
|
const user =
|
||||||
|
await userService.update(
|
||||||
|
req.params.id as string,
|
||||||
|
req.body
|
||||||
|
);
|
||||||
|
|
||||||
|
res.json(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
async detele (
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
) {
|
||||||
|
await userService.delete(
|
||||||
|
req.params.id as string
|
||||||
|
);
|
||||||
|
|
||||||
|
res.json({message: "Usuario eliminado"});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
backend/src/modules/user/user.interface.ts
Normal file
11
backend/src/modules/user/user.interface.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { ObjectId } from "mongoose";
|
||||||
|
|
||||||
|
export interface IUser {
|
||||||
|
|
||||||
|
employeeId:number;
|
||||||
|
userName:string;
|
||||||
|
passwordHash:string;
|
||||||
|
roleId:ObjectId;
|
||||||
|
active:boolean;
|
||||||
|
lastLogin:Date;
|
||||||
|
}
|
||||||
46
backend/src/modules/user/user.model.ts
Normal file
46
backend/src/modules/user/user.model.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
|
||||||
|
const userSchema = new mongoose.Schema({
|
||||||
|
|
||||||
|
employeeId:{
|
||||||
|
type:Number,
|
||||||
|
required:true,
|
||||||
|
unique:true,
|
||||||
|
},
|
||||||
|
|
||||||
|
userName:{
|
||||||
|
type:String,
|
||||||
|
required:true,
|
||||||
|
unique:true,
|
||||||
|
},
|
||||||
|
|
||||||
|
passwordHash:{
|
||||||
|
type:String,
|
||||||
|
required:true
|
||||||
|
},
|
||||||
|
|
||||||
|
roleId:{
|
||||||
|
type:mongoose.Types.ObjectId,
|
||||||
|
ref:"Role",
|
||||||
|
required:true
|
||||||
|
},
|
||||||
|
|
||||||
|
active:{
|
||||||
|
type:Boolean,
|
||||||
|
default:true
|
||||||
|
},
|
||||||
|
|
||||||
|
lastLogin:{
|
||||||
|
type:Date
|
||||||
|
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
timestamps:true
|
||||||
|
}
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
export default mongoose.model("User", userSchema);
|
||||||
19
backend/src/modules/user/user.routes.ts
Normal file
19
backend/src/modules/user/user.routes.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
|
||||||
|
import { UserController } from "./user.controller";
|
||||||
|
|
||||||
|
const userRoutes = Router();
|
||||||
|
|
||||||
|
const controller = new UserController();
|
||||||
|
|
||||||
|
userRoutes.get("/", controller.getAll);
|
||||||
|
|
||||||
|
userRoutes.get("/:id", controller.getById);
|
||||||
|
|
||||||
|
userRoutes.post("/", controller.create);
|
||||||
|
|
||||||
|
userRoutes.put("/:id", controller.update);
|
||||||
|
|
||||||
|
userRoutes.delete("/:id", controller.detele);
|
||||||
|
|
||||||
|
export default userRoutes;
|
||||||
46
backend/src/modules/user/user.service.ts
Normal file
46
backend/src/modules/user/user.service.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import userModel from "./user.model";
|
||||||
|
|
||||||
|
export class UserService {
|
||||||
|
|
||||||
|
async getAll() {
|
||||||
|
|
||||||
|
return await userModel
|
||||||
|
.find()
|
||||||
|
.populate("roleId");
|
||||||
|
}
|
||||||
|
|
||||||
|
async getById(
|
||||||
|
id:string
|
||||||
|
) {
|
||||||
|
return await userModel
|
||||||
|
.find()
|
||||||
|
.populate("roleId");
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(
|
||||||
|
data:any
|
||||||
|
) {
|
||||||
|
return await userModel.create(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(
|
||||||
|
id:string,
|
||||||
|
data:any
|
||||||
|
) {
|
||||||
|
return await userModel.findByIdAndUpdate(
|
||||||
|
id,
|
||||||
|
data,
|
||||||
|
{
|
||||||
|
new:true
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(
|
||||||
|
id:string
|
||||||
|
) {
|
||||||
|
return await userModel.findByIdAndDelete(
|
||||||
|
id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
import dotenv from "dotenv";
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
import { connectMongo } from "./config/mongodb";
|
||||||
|
//import { connectSqlServer } from "./config/sqlserver";
|
||||||
|
import app from "./app";
|
||||||
|
|
||||||
|
const PORT = process.env.PORT || 5000;
|
||||||
|
|
||||||
|
const startServer = async() => {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
await connectMongo();
|
||||||
|
//await connectSqlServer();
|
||||||
|
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
|
||||||
|
console.log(`Servidor ejecutandose en puerto ${PORT}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
}catch(error){
|
||||||
|
|
||||||
|
console.error("Error al iniciar el servidor", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
startServer();
|
||||||
@ -9,7 +9,7 @@
|
|||||||
// See also https://aka.ms/tsconfig/module
|
// See also https://aka.ms/tsconfig/module
|
||||||
"module": "nodenext",
|
"module": "nodenext",
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"types": [],
|
"types": ["node"],
|
||||||
// For nodejs:
|
// For nodejs:
|
||||||
// "lib": ["esnext"],
|
// "lib": ["esnext"],
|
||||||
// "types": ["node"],
|
// "types": ["node"],
|
||||||
@ -35,7 +35,7 @@
|
|||||||
// Recommended Options
|
// Recommended Options
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"jsx": "react-jsx",
|
"jsx": "react-jsx",
|
||||||
"verbatimModuleSyntax": true,
|
//"verbatimModuleSyntax": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"noUncheckedSideEffectImports": true,
|
"noUncheckedSideEffectImports": true,
|
||||||
"moduleDetection": "force",
|
"moduleDetection": "force",
|
||||||
|
|||||||
Reference in New Issue
Block a user