Skip to content

Commit

Permalink
feat: configurando tudo
Browse files Browse the repository at this point in the history
Co-authored-by: MarcoTulioSoares <[email protected]>
Co-authored-by: LucasAvelar2711 <[email protected]>
  • Loading branch information
3 people committed Aug 22, 2024
1 parent 07979bd commit 7e0a82c
Show file tree
Hide file tree
Showing 16 changed files with 348 additions and 159 deletions.
57 changes: 0 additions & 57 deletions riso_backend/src/controllers/unidadeEmpresa.controller.js

This file was deleted.

89 changes: 89 additions & 0 deletions riso_backend/src/controllers/unit.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import unitService from "../services/unit.service.js";

const findAllUnit = async (req, res) => {
try{
const unit = await unitService.findAllService();

if (unit.length === 0) {
return res.status(200).send({message: 'Any user was created'});
}

return res.status(201).send({ unit: unit });
}catch(err) {
return res.status(500).send({ message: err.message });
}
}

const createUnit = async (req, res) => {
try{
const {
name,
numberOfficials,
cnpj,
state,
street,
complement
} = req.body;

if (!name || !numberOfficials || !cnpj || !state || !street || !complement) {
return res.status(400).send({ message: 'Submit all fields for unit', substatus: 1});
};

const unit = await unitService.createService(req.body);

return res.status(200).send({message: 'Unity and company successfully created ', unit:unit});
}catch (err){
if (err.code === 11000) { // Código de erro para violação de chave única em MongoDB/Mongoose
return res.status(400).send({ message: 'CNPJ already in use', substatus: 2 });
}

return res.status(500).send({message: err.message});
}
}

const updateUnit = async (req, res) => {
try{
const {id} = req.params;

if(!id) {
return res.status(404).send({message: 'Unit ID is required'});
}

const unit = await unitService.updateService({_id: id}, req.body);

if (!unit) {
return res.status(404).send({ message: 'Unit not found' });
}

return res.status(200).send({message: 'Unit successfully updated ', unit:unit});
}catch (err){
return res.status(500).send({message: err.message});
}
}

const deleteUnit = async (req,res) => {
try{
const {id} = req.params;

if (!id) {
return res.status(404).send({message: 'Unit ID is required'});
}

const unit = await unitService.deleteService({ _id: id});

if (!unit) {
return res.status(404).send({ message: 'This unit does not exist' });
}

return res.status(200).send({ message: 'Unit was deleted successfully', unit });
}catch(err){
return res.status(500).send({ message: err.message });
}
}

export default {
findAllUnit,
createUnit,
updateUnit,
deleteUnit,
};
84 changes: 84 additions & 0 deletions riso_backend/src/controllers/unitUser.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import unitUserService from "../services/unitUser.service.js";

const findAllUnitUser = async (req, res) => {
try{
const unitUser = await unitUserService.findAllService();

if (unitUser.length === 0) {
return res.status(200).send({message: 'Any UnitUser was created'});
}

return res.status(201).send({ unitUser: unitUser });
}catch(err) {
return res.status(500).send({ message: err.message });
}
}

const createUnitUser = async (req, res) => {
try{
const {
user,
unit,
} = req.body;

if (!user || !unit) {
return res.status(400).send({ message: 'Submit all fields for userUnit', substatus: 1});
};

const unitUser = await unitUserService.createService(req.body);

return res.status(200).send({message: 'Unity and company successfully created ', unitUser:unitUser});
}catch (err){
if (err.code === 11000) { // Código de erro para violação de chave única em MongoDB/Mongoose
return res.status(400).send({ message: 'Match User and Unit already registrated', substatus: 2 });
}

return res.status(500).send({message: err.message});
}
}

const findByUserIdUnitUser = async (req, res) => {
try {
const {id} = req.body;

if(!id){
return res.status(400).send({ message: 'User ID is required'})
}

const unitUser = await unitUserService.findById({user:id});
if (!unitUser) {
return res.status(404).send({ message: 'This user dont have units associations'});
}

return res.status(200).send({message: 'UserUnit was found it', unitUser:unitUser});
}catch(err) {
return res.status(500).send({ message: err.message });
}
}

const deleteUnitUser = async (req,res) => {
try{
const {id} = req.params;

if (!id) {
return res.status(404).send({message: 'UnitUser ID is required'});
}

const unitUser = await unitUserService.deleteService({ _id: id});

if (!unit) {
return res.status(404).send({ message: 'This unitUser does not exist' });
}

return res.status(200).send({ message: 'UnitUser was deleted successfully', unitUser });
}catch(err){
return res.status(500).send({ message: err.message });
}
}

export default {
findAllUnitUser,
createUnitUser,
findByUserIdUnitUser,
deleteUnitUser,
};
33 changes: 0 additions & 33 deletions riso_backend/src/models/UnidadeEmpresa.js

This file was deleted.

36 changes: 36 additions & 0 deletions riso_backend/src/models/Unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import mongoose from 'mongoose';

const UnitSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
numberOfficials: {
type: Number,
required: true
},
cnpj: {
type: String,
required: true,
unique: true,
},
state: {
type: String,
required: true
},
street: {
type: String,
required: true
},
complement: {
type: String,
required: true
},
number: {
type: Number,
required: false
}
}, {timestamps:true});

const Unit = mongoose.model('Unit', UnitSchema);
export default Unit;
20 changes: 20 additions & 0 deletions riso_backend/src/models/UnitUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mongoose from 'mongoose';

const UnitUserSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
unit: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Unit',
required: true
}
}, {timestamps: true});

// Garantir que um usuário não possa estar na mesma unidade mais de uma vez
UnitUserSchema.index({ user: 1, unit: 1 }, { unique: true });

const UnitUser = mongoose.model('UnitUser', UnitUserSchema);
export default UnitUser;
7 changes: 4 additions & 3 deletions riso_backend/src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Router } from "express";
import cors from 'cors';
import userRouter from "./user.route.js";
import unidadeEmpresaRouter from "./unidadeEmpresa.route.js";
import unitRouter from "./unit.route.js";
import unitUserRouter from "./unitUser.js";

const router = Router();

router.use("/user", cors(), userRouter);

router.use("/unidadeEmpresa", cors(), unidadeEmpresaRouter);
router.use("/unit", cors(), unitRouter);
router.use("/unitUser", cors(), unitUserRouter);

export default router;
11 changes: 0 additions & 11 deletions riso_backend/src/routes/unidadeEmpresa.route.js

This file was deleted.

11 changes: 11 additions & 0 deletions riso_backend/src/routes/unit.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from 'express';
import unitController from '../controllers/unit.controller.js';

const unitRouter = express.Router()

unitRouter.get('/findAllUnit', unitController.findAllUnit);
unitRouter.post('/createUnit',unitController.createUnit);
unitRouter.patch('/updateUnit/:id', unitController.updateUnit);
unitRouter.delete('/deleteUnit/:id',unitController.deleteUnit);

export default unitRouter;
11 changes: 11 additions & 0 deletions riso_backend/src/routes/unitUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from 'express';
import unitUserController from '../controllers/unitUser.controller.js';

const unitUserRouter = express.Router()

unitUserRouter.get('/findAllUnitUser', unitUserController.findAllUnitUser);
unitUserRouter.post('/createUnitUser',unitUserController.createUnitUser);
unitUserRouter.get('/findByUserId/:id', unitUserController.findByUserIdUnitUser);
unitUserRouter.delete('/deleteUnitUser/:id',unitUserController.deleteUnitUser);

export default unitUserRouter;
Loading

0 comments on commit 7e0a82c

Please sign in to comment.