Skip to content

Commit c605aff

Browse files
committed
Terceiro Commit: Adição da funcionalidade de adicionar novos cargos
1 parent 13025e7 commit c605aff

File tree

7 files changed

+178
-19
lines changed

7 files changed

+178
-19
lines changed

src/App.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require("express")
22
const app = express();
33
const functionary = require("./routes/functionaryRouter");
4+
const occupation = require("./routes/occupationRouter");
45

56
const path = require("path");
67
const PORT = 3000;
@@ -9,10 +10,16 @@ const bodyParser = require("body-parser");
910

1011
app.use(bodyParser.json());
1112

13+
14+
1215
app.use("/", express.static(path.join(__dirname, "frontend")));
1316

1417
app.use("/functionary", functionary);
1518

19+
app.use("/occupation", occupation);
20+
21+
22+
1623

1724
app.listen(PORT, ()=>{
1825
console.log("Server Running...", PORT);

src/controllers/functionaryController.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const functionaryData = require("../models/db");
1+
const functionaryData = require("../models/db").functionary;
22

33

44
const controller = {
55
all: function(req, res){
66
res.json(functionaryData);
77
},
88
add: function(req, res){
9+
910
const functionary = {
1011
id: generateId(),
1112
name: req.body.name,
@@ -31,7 +32,7 @@ const controller = {
3132

3233
}
3334
else{
34-
35+
3536
}
3637

3738

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const occupationData = require("../models/db").occupation;
2+
3+
const controller = {
4+
all: function(req, res){
5+
res.json(occupationData);
6+
},
7+
add: function(req, res){
8+
9+
const occupation = {
10+
id: generateId(),
11+
occupation: req.body.occupation
12+
}
13+
14+
//validação dos dados
15+
if(req.body.occupation != "" && req.body.occupation != null && req.body.occupation != undefined){
16+
for(item in occupationData){
17+
if(item.occupation == req.body.occupation){
18+
res.json({valid: false, message: "Já existe um cargo semelhante cadastrado."});
19+
}
20+
}
21+
}
22+
else{
23+
res.json({valid: false, message: "Nome do cargo não pode estar vazio."});
24+
}
25+
26+
//adicionando um novo cargo no "banco de dados"
27+
occupationData.push(occupation);
28+
},
29+
remove: function(req, res){
30+
31+
}
32+
}
33+
34+
35+
module.exports = controller;
36+
37+
38+
function generateId(){
39+
return Math.random().toString(36).substring(2,9);
40+
}

src/frontend/index.html

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@
1212
</head>
1313
<body style="background-color: #00000010;">
1414

15-
<!-- Button trigger modal -->
16-
<!-- <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
17-
Launch demo modal
18-
</button> -->
19-
20-
<!-- Modal -->
21-
<div class="modal fade" id="form" tabindex="-1" aria-labelledby="label" aria-hidden="true">
15+
<!-- modal para cadastro de funcionarios -->
16+
<div class="modal fade" id="form-functionary" tabindex="-1" aria-labelledby="label" aria-hidden="true">
2217
<div class="modal-dialog modal-dialog-centered">
2318
<div class="modal-content">
2419
<div class="modal-header">
@@ -48,7 +43,36 @@ <h1 class="modal-title fs-5" id="label">Adicionar Novo Funcionário</h1>
4843
</form>
4944
</div>
5045
<div class="modal-footer justify-content-center">
51-
<button onclick="validForm()" type="button" class="d-block w-100 btn btn-primary">Salvar</button>
46+
<button onclick="validFunctionary()" type="button" class="d-block w-100 btn btn-primary">Salvar</button>
47+
</div>
48+
</div>
49+
</div>
50+
</div>
51+
52+
<!-- modal para cadastro de cargos -->
53+
<div class="modal fade" id="form-occupation" tabindex="-1" aria-labelledby="label" aria-hidden="true">
54+
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
55+
<div class="modal-content">
56+
<div class="modal-header">
57+
<h1 class="modal-title fs-5" id="label">Adicionar Novo Cargo</h1>
58+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
59+
</div>
60+
<div class="modal-body">
61+
<b class="">Lista de Cargos Existentes</b>
62+
<ul id="occupation-list" class="list-group mt-2">
63+
64+
<!-- JavaScript content -->
65+
66+
</ul>
67+
</div>
68+
<div class="modal-footer justify-content-center">
69+
70+
<div class="d-block w-100 form-floating mb-2">
71+
<input type="text" class="form-control border-primary" id="new-occupation" placeholder="Nova Função">
72+
<label for="occupation">Nova Função</label>
73+
</div>
74+
75+
<button onclick="validOccupation()" type="button" class="d-block w-100 btn btn-primary">Salvar</button>
5276
</div>
5377
</div>
5478
</div>
@@ -57,7 +81,11 @@ <h1 class="modal-title fs-5" id="label">Adicionar Novo Funcionário</h1>
5781
<div class="container-md my-5 px-2 py-4 rounded bg-light" style="height: 85vh;">
5882
<div id="header" class="d-flex justify-content-between">
5983
<h4>Controle de Funcionários</h4>
60-
<a type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#form">Adicionar</a>
84+
<div>
85+
<a onclick="loadOccupation()" type="button" class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#form-occupation">Adicionar Função</a>
86+
<a type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#form-functionary">Adicionar Funcionário</a>
87+
</div>
88+
6189
</div>
6290
<div id="body" class="mt-4">
6391

src/frontend/script.js

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ function load(){
2727

2828
})
2929
}
30+
3031
load();
3132

32-
function validForm(){
33+
function validFunctionary(){
3334
const functionary = {
3435
name: document.getElementById("name").value,
3536
occupation: document.getElementById("occupation").value,
@@ -39,11 +40,11 @@ function validForm(){
3940
//console.log(functionary);
4041

4142
if(functionary.name != '' && functionary.occupation != '' && functionary.salary != ''){
42-
save(functionary);
43+
saveFunctionary(functionary);
4344
}
4445
}
4546

46-
function save(functionary){
47+
function saveFunctionary(functionary){
4748

4849
functionary.salary = parseFloat(functionary.salary).toFixed(2);
4950

@@ -53,7 +54,7 @@ function save(functionary){
5354
body: JSON.stringify(functionary)
5455
}
5556

56-
fetch("http://localhost:3000/user/login", options).then(res =>{
57+
fetch("http://localhost:3000/functionary/add", options).then(res =>{
5758
return res.json();
5859
}).then((result) =>{
5960

@@ -65,8 +66,6 @@ function save(functionary){
6566

6667

6768

68-
69-
7069
function edit(id){
7170
console.log(id)
7271

@@ -77,4 +76,59 @@ function remove(id){
7776
console.log(id)
7877

7978

79+
}
80+
81+
82+
83+
84+
// espaco para os métodos relacionados com as funções de trabalhadores
85+
function loadOccupation(){
86+
let list = document.getElementById("occupation-list");
87+
document.getElementById("new-occupation").value = "";
88+
89+
fetch("http://localhost:3000/occupation/all").then((res) => {return res.json()}).then((data) =>{
90+
list.innerHTML = "";
91+
92+
data.forEach((item) =>{
93+
list.innerHTML +=
94+
`
95+
<li class="list-group-item d-flex justify-content-between">
96+
<p class="mb-0">${item.occupation}</p>
97+
<a onclick="removeOccupation(${item.id})" class="d-flex aling-items-center"><img src="./icons/delete.png" width="20"></a>
98+
</li>
99+
`
100+
})
101+
})
102+
}
103+
104+
function validOccupation(){
105+
let occupation = document.getElementById("new-occupation").value;
106+
107+
if(occupation != ""){
108+
saveOccupation({occupation: occupation})
109+
}
110+
else{
111+
alert("Cargo não pode estar vazio.");
112+
}
113+
}
114+
115+
function saveOccupation(occupation){
116+
const options = {
117+
headers: {"Content-Type": "application/json"},
118+
method: "POST",
119+
body: JSON.stringify(occupation)
120+
}
121+
122+
fetch("http://localhost:3000/occupation/add", options).then(res =>{
123+
return res.json();
124+
}).then((result) =>{
125+
126+
console.log(result);
127+
128+
if(!result.valid)
129+
alert(`ERRO: ${result.message}`);
130+
});
131+
132+
133+
loadOccupation()
80134
}

src/models/db.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*Este arquivo simula a conexão de um banco de dados, porém neste projeto não haverá nenhum DB já que o intuito é apenas a configuração de um ambiente de backend utilizando o nodejs. Dito isso, o conteúdo da aplicação ficará armazenada localmente no servidor em arrays, e sempre será apagada quando o servidor for interrompido. */
22

3-
let functionary = [
3+
let Functionary = [
44
{
55
id: 1,
66
name: "Vinícius Gonzaga Guilherme",
@@ -15,4 +15,22 @@ let functionary = [
1515
}
1616
];
1717

18-
module.exports = functionary;
18+
let Occupation = [
19+
{
20+
id: "fwrfqe",
21+
occupation: "Desenvoledor(a)"
22+
},
23+
{
24+
id: "3n49j2",
25+
occupation: "Analista de Dados"
26+
},
27+
{
28+
id: "jruh39",
29+
occupation: "Designer"
30+
}
31+
]
32+
33+
module.exports = {
34+
functionary: Functionary,
35+
occupation: Occupation
36+
};

src/routes/occupationRouter.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
4+
const occupationController = require("../controllers/occupationController");
5+
6+
router.get("/all", occupationController.all);
7+
8+
router.post("/add", occupationController.add);
9+
10+
11+
module.exports = router;

0 commit comments

Comments
 (0)