Skip to content

Commit

Permalink
Merge pull request #47 from mdsreq-fga-unb/feature/us03
Browse files Browse the repository at this point in the history
Finalização da Feature 03
  • Loading branch information
DanielRogs authored Aug 24, 2024
2 parents 651c40b + 6b1b384 commit 4a3ef25
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 5 deletions.
50 changes: 48 additions & 2 deletions riso_backend/src/controllers/unit.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ const findAllUnit = async (req, res) => {
}
}

const findByCodeUnit = async (req, res) => {
try {
const { code } = req.params;
const unit = await unitService.findByCode(code);
if (!unit) {
return res.status(404).send({ message: 'Unit not found' });
}
return res.status(200).send(unit);
} catch (err) {
return res.status(500).send({ message: err.message });
}
};

const createUnit = async (req, res) => {
try{
const {
Expand All @@ -22,14 +35,46 @@ const createUnit = async (req, res) => {
cnpj,
state,
street,
complement
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);
// Função para gerar um código aleatório de 5 dígitos alfanuméricos
const generateCode = () => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < 5; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};

// Função para gerar um código único, garantindo que ele não se repita
const generateUniqueCode = async () => {
let unique = false;
let newCode;
while (!unique) {
newCode = generateCode();
const existingUnit = await unitService.findByCode(newCode);
if (!existingUnit) {
unique = true;
}
}
return newCode;
};

// Se o código não for passado no body, geramos um automaticamente
const generatedCode = await generateUniqueCode();

const unitData = {
...req.body,
code: generatedCode
};

const unit = await unitService.createService(unitData);

return res.status(200).send({message: 'Unity and company successfully created ', unit:unit});
}catch (err){
Expand Down Expand Up @@ -83,6 +128,7 @@ const deleteUnit = async (req,res) => {

export default {
findAllUnit,
findByCodeUnit,
createUnit,
updateUnit,
deleteUnit,
Expand Down
5 changes: 5 additions & 0 deletions riso_backend/src/models/Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const UnitSchema = new mongoose.Schema({
number: {
type: Number,
required: false
},
code: {
type: String,
required: true,
unique: true,
}
}, {timestamps:true});

Expand Down
1 change: 1 addition & 0 deletions riso_backend/src/routes/unit.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import unitController from '../controllers/unit.controller.js';
const unitRouter = express.Router()

unitRouter.get('/findAllUnit', unitController.findAllUnit);
unitRouter.get('/findByCode/:code', unitController.findByCodeUnit);
unitRouter.post('/createUnit',unitController.createUnit);
unitRouter.patch('/updateUnit/:id', unitController.updateUnit);
unitRouter.delete('/deleteUnit/:id',unitController.deleteUnit);
Expand Down
2 changes: 2 additions & 0 deletions riso_backend/src/services/unit.service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Unit from "../models/Unit.js";

const findAllService = () => Unit.find();
const findByCode = (code) => Unit.findOne({ code });
const createService = (body) => Unit.create(body);
const updateService = (params, body) => Unit.updateOne(params, body, {new: true});
const deleteService = (params) => Unit.deleteOne(params);

export default {
findAllService,
findByCode,
createService,
updateService,
deleteService,
Expand Down
2 changes: 1 addition & 1 deletion riso_backend/src/services/unitUser.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UnitUser from "../models/UnitUser.js";

const findAllService = () => UnitUser.find();
const createService = (body) => UnitUser.create(body);
const createService = (params, body) => UnitUser.create(params, body);
const findByUserIdService = (body) => UnitUser.find(body);
const deleteService = (params) => UnitUser.deleteOne(params);

Expand Down
2 changes: 1 addition & 1 deletion riso_frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function App() {
<Routes>
<Route path='/' element={<Autentication />} />
<Route path='/homePage' element={<HomePage />} />
<Route path="/CadastroEmpresa" element={<RegisterUnit />} />
<Route path='/registerUnit' element={<RegisterUnit/>}/>
</Routes>
</Router>
);
Expand Down
9 changes: 9 additions & 0 deletions riso_frontend/src/components/addUnityModal/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

function addUnityModal() {
return (
<h1>Você Logou</h1>
);
}

export default addUnityModal;
2 changes: 1 addition & 1 deletion riso_frontend/src/pages/Autentication/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function Autentication() {
<div style={{height: 10}}></div>
)}

<a class="password" href="/CadastroEmpresa">Esqueci minha senha?</a>
<a class="password" href="/RegisterUnit">Esqueci minha senha?</a>
<button class="btn btn-second" onClick={login}>Entrar</button>
</form>
</div>
Expand Down
9 changes: 9 additions & 0 deletions riso_frontend/src/services/unityServices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import axios from 'axios';

const baseURL = 'http://localhost:3000';

export function findOne (code) {
const response = axios.get(`${baseURL}/unidadeEmpresa/findOneUnity`, { params: { code }});

return response;
}

0 comments on commit 4a3ef25

Please sign in to comment.