diff --git a/backend/.env b/backend/.env new file mode 100644 index 0000000..082cb8f --- /dev/null +++ b/backend/.env @@ -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 \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index 1138bfc..d6683ce 100644 --- a/backend/package.json +++ b/backend/package.json @@ -4,6 +4,7 @@ "description": "", "main": "index.js", "scripts": { + "dev": "ts-node-dev --respawn src/server.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], diff --git a/backend/src/app.ts b/backend/src/app.ts index e69de29..997bec9 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -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; \ No newline at end of file diff --git a/backend/src/config/mongodb.ts b/backend/src/config/mongodb.ts new file mode 100644 index 0000000..0eab675 --- /dev/null +++ b/backend/src/config/mongodb.ts @@ -0,0 +1,18 @@ +import mongoose from "mongoose"; + +export const connectMongo = async (): Promise => { + + try { + + await mongoose.connect(process.env.MONGO_URI!); + console.log("MongoDB conectado"); + + } catch(error){ + + console.error("Error MongoDB", error); + + process.exit(1); + + } + +}; \ No newline at end of file diff --git a/backend/src/config/sqlserver.ts b/backend/src/config/sqlserver.ts new file mode 100644 index 0000000..5e330b5 --- /dev/null +++ b/backend/src/config/sqlserver.ts @@ -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); + + } + +};*/ + diff --git a/backend/src/modules/roles/role.controller.ts b/backend/src/modules/roles/role.controller.ts new file mode 100644 index 0000000..7646a6c --- /dev/null +++ b/backend/src/modules/roles/role.controller.ts @@ -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); + } +} \ No newline at end of file diff --git a/backend/src/modules/roles/role.interface.ts b/backend/src/modules/roles/role.interface.ts new file mode 100644 index 0000000..02000ab --- /dev/null +++ b/backend/src/modules/roles/role.interface.ts @@ -0,0 +1,6 @@ +export interface IRole { + + roleName:string; + description:string; + active:boolean; +} \ No newline at end of file diff --git a/backend/src/modules/roles/role.model.ts b/backend/src/modules/roles/role.model.ts new file mode 100644 index 0000000..dceb07c --- /dev/null +++ b/backend/src/modules/roles/role.model.ts @@ -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); \ No newline at end of file diff --git a/backend/src/modules/roles/role.routes.ts b/backend/src/modules/roles/role.routes.ts new file mode 100644 index 0000000..e17e5a2 --- /dev/null +++ b/backend/src/modules/roles/role.routes.ts @@ -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 ; \ No newline at end of file diff --git a/backend/src/modules/roles/role.service.ts b/backend/src/modules/roles/role.service.ts new file mode 100644 index 0000000..f36586f --- /dev/null +++ b/backend/src/modules/roles/role.service.ts @@ -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); + } +} \ No newline at end of file diff --git a/backend/src/modules/user/user.controller.ts b/backend/src/modules/user/user.controller.ts new file mode 100644 index 0000000..3abdee8 --- /dev/null +++ b/backend/src/modules/user/user.controller.ts @@ -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"}); + } + +} \ No newline at end of file diff --git a/backend/src/modules/user/user.interface.ts b/backend/src/modules/user/user.interface.ts new file mode 100644 index 0000000..bb16751 --- /dev/null +++ b/backend/src/modules/user/user.interface.ts @@ -0,0 +1,11 @@ +import { ObjectId } from "mongoose"; + +export interface IUser { + + employeeId:number; + userName:string; + passwordHash:string; + roleId:ObjectId; + active:boolean; + lastLogin:Date; +} \ No newline at end of file diff --git a/backend/src/modules/user/user.model.ts b/backend/src/modules/user/user.model.ts new file mode 100644 index 0000000..65d10f5 --- /dev/null +++ b/backend/src/modules/user/user.model.ts @@ -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); \ No newline at end of file diff --git a/backend/src/modules/user/user.routes.ts b/backend/src/modules/user/user.routes.ts new file mode 100644 index 0000000..df28ea9 --- /dev/null +++ b/backend/src/modules/user/user.routes.ts @@ -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; \ No newline at end of file diff --git a/backend/src/modules/user/user.service.ts b/backend/src/modules/user/user.service.ts new file mode 100644 index 0000000..1510cb4 --- /dev/null +++ b/backend/src/modules/user/user.service.ts @@ -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 + ); + } +} \ No newline at end of file diff --git a/backend/src/server.ts b/backend/src/server.ts index e69de29..90ee09f 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -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(); \ No newline at end of file diff --git a/backend/tsconfig.json b/backend/tsconfig.json index cec4a3a..6f44b56 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -9,7 +9,7 @@ // See also https://aka.ms/tsconfig/module "module": "nodenext", "target": "esnext", - "types": [], + "types": ["node"], // For nodejs: // "lib": ["esnext"], // "types": ["node"], @@ -35,7 +35,7 @@ // Recommended Options "strict": true, "jsx": "react-jsx", - "verbatimModuleSyntax": true, + //"verbatimModuleSyntax": true, "isolatedModules": true, "noUncheckedSideEffectImports": true, "moduleDetection": "force",