-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
70 lines (52 loc) · 1.64 KB
/
routes.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
import express from 'express'
import { createOrUpdate, deleteUserById, getUserById, readAllUsers } from './db.js'
const router = express.Router()
// READ ALL Users
router.get('/users', async(req, res) => {
const { success, data } = await readAllUsers()
if(success){
return res.json({success, data})
}
return res.status(500).json({success:false, messsage: "Error"})
})
// Get User by ID
router.get('/user/:id', async(req, res) => {
const { id } = req.params
const { success, data } = await getUserById(id)
console.log(data)
if(success){
return res.json({success, data})
}
return res.status(500).json({success: false, message: "Error"})
})
// Create User
router.post('/user', async(req, res) => {
console.log("post new user calisti")
try {
const { success, data } = await createOrUpdate(req.body)
return res.json({success, data})
} catch (error) {
return res.status(500).json({success: false, message: error})
}
})
// Update User by ID
router.put('/user/:id', async(req, res) => {
const user = req.body
const { id } = req.params
user.id = parseInt(id)
const { success, data } = await createOrUpdate(user)
if(success){
return res.json({success, data})
}
return res.status(500).json({success: false, message: "Error"})
})
// Delete User by Id
router.delete('/user/:id', async (req, res) => {
const { id } = req.params
const { success, data } = await deleteUserById(id)
if (success) {
return res.json({ success, data })
}
return res.status(500).json({ success: false, message: 'Error'})
})
export default router