-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
124 lines (94 loc) · 2.75 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require('express');
const app = express();
app.use(express.json());
const port = process.env.PORT || 3000;
// Require para usar Prisma
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
//CORS
const cors = require("cors");
const corsOptions = {
origin: "http://localhost:8081"
}
app.use(cors(corsOptions));
//Rutas/Endpoints
app.get('/', (req, res) => {
res.json({message: 'alive'});
});
app.listen(port, () => {
console.log(`Listening to requests on port ${port}`);
});
app.get('/explorers', async (req, res) => {
const allExplorers = await prisma.explorer.findMany({});
res.json(allExplorers);
});
app.get('/explorers/:id', async (req, res) => {
const id = req.params.id;
const explorer = await prisma.explorer.findUnique({where: {id: parseInt(id)}});
res.json(explorer);
});
app.post('/explorers', async (req, res) => {
const explorer = {
name: req.body.name,
username: req.body.username,
mission: req.body.mission
};
const message = 'Explorer creado.';
await prisma.explorer.create({data: explorer});
return res.json({message});
});
app.put('/explorers/:id', async (req, res) => {
const id = parseInt(req.params.id);
await prisma.explorer.update({
where: {
id: id
},
data: {
mission: req.body.mission
}
})
return res.json({message: "Actualizado correctamente"});
});
app.delete('/explorers/:id', async (req, res) => {
const id = parseInt(req.params.id);
await prisma.explorer.delete({where: {id: id}});
return res.json({message: "Eliminado correctamente"});
});
app.get('/commanders', async (req, res) => {
const allECommanders = await prisma.commander.findMany({});
res.json(allECommanders);
});
app.get('/commanders/:id', async (req, res) => {
const id = req.params.id;
const commander = await prisma.commander.findUnique({where: {id: parseInt(id)}});
res.json(commander);
});
app.post('/commanders', async (req, res) => {
const commander = {
name: req.body.name,
lang: req.body.lang,
missionCommander: req.body.missionCommander,
enrollments: req.body.enrollments,
hasCertification: req.body.hasCertification
};
const message = 'Commander creado.';
await prisma.commander.create({data: commander});
return res.json({message});
});
app.put('/commanders/:id', async (req, res) => {
const id = parseInt(req.params.id);
await prisma.commander.update({
where: {
id: id
},
data: {
missionCommander: req.body.missionCommander
}
})
return res.json({message: "Actualizado correctamente"});
});
app.delete('/commanders/:id', async (req, res) => {
const id = parseInt(req.params.id);
await prisma.commander.delete({where: {id: id}});
return res.json({message: "Eliminado correctamente"});
});