-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
137 lines (112 loc) · 3.56 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
import express from 'express'
import cors from 'cors'
import bodyParser from 'body-parser'
import { generateToken } from './jwt.js';
import { posts, createpost, deletepost, getpost, updatePost, login } from './db.js';
const PORT = 23075;
const app = express()
app.use(express.json())
app.use(bodyParser.json())
app.use(cors())
app.get('/posts', async (req, res) => {
try {
const result = await posts();
return res.json(result);
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
app.post('/createpost', async (req, res) => {
const { name, release_date, description, image } = req.body;
if (!name || !release_date || !description || !image) {
return res.status(400).json({ error: 'Todos los parametros son requeridos' });
}
try {
const newPost = await createpost(name, release_date, description, image);
if (newPost) {
return res.json(newPost);
} else {
return res.status(401).json({ error: 'Credenciales inválidas' });
}
} catch (err) {
console.error('Error en /createpost:', err);
res.status(500).json({ error: 'Error interno del servidor' });
}
});
app.post('/deletepost', async (req, res) => {
const { post_id } = req.body;
if (!post_id) {
return res.status(400).json({ error: 'El id del post es requerido' });
}
try {
const PostD = await deletepost(post_id);
if (deletepost) {
return res.status(200).json(PostD);
} else {
return res.status(401).json({ error: 'Credenciales inválidas' });
}
} catch (err) {
console.error('Error en /deletepost:', err);
res.status(500).json({ error: 'Error interno del servidor' });
}
});
app.post('/getpost', async (req, res) => {
const { id_post } = req.body;
if (!id_post) {
return res.status(400).json({ error: 'El id del post es requerido' });
}
try {
const post = await getpost(id_post);
if (getpost) {
return res.json(post);
} else {
return res.status(401).json({ error: 'Credenciales inválidas' });
}
} catch (err) {
console.error('Error en /getpost:', err);
res.status(500).json({ error: 'Error interno del servidor' });
}
});
app.post('/updatepost', async (req, res) => {
const { id_post, name, release_date, description, image } = req.body;
if (!id_post || !name || !release_date || !description || !image) {
return res.status(400).json({ error: 'Todos los parametros son requeridos' });
}
try {
const newPost = await updatePost(id_post, name, release_date, description, image);
if (newPost) {
return res.json(newPost);
} else {
return res.status(401).json({ error: 'Credenciales inválidas' });
}
} catch (err) {
console.error('Error en /updatepost:', err);
res.status(500).json({ error: 'Error interno del servidor' });
}
});
app.post('/login', async (req, res) => {
const { user, password } = req.body;
if (!user || !password) {
return res.status(400).json({ error: 'ID y password son requeridos' });
}
try {
const success = await login(user, password);
if (success) {
const token = generateToken(user)
console.log(token)
return res.status(200).json({ success: true, access_token: token })
} else {
return res.status(401).json({ success: false });
}
} catch (err) {
console.error('Error en /login:', err);
res.status(500).json({ error: 'Error interno del servidor' });
}
});
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(PORT, '127.0.0.1', () => {
console.log(`Server listening at http://127.0.0.1:${PORT}`)
})