@igea/oac_backend 1.0.9 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/controllers/users.js +119 -0
- package/src/index.js +4 -1
- package/src/models/users.js +62 -0
package/package.json
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const router = express.Router();
|
|
3
|
+
const Users = require('../models/users');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
// Get all users
|
|
7
|
+
router.get('/', async (req, res) => {
|
|
8
|
+
Users.get().then(users => {
|
|
9
|
+
res.json({
|
|
10
|
+
success: true,
|
|
11
|
+
data: users,
|
|
12
|
+
message: null
|
|
13
|
+
});
|
|
14
|
+
}).catch(err => {
|
|
15
|
+
res.status(500).json({
|
|
16
|
+
success: false,
|
|
17
|
+
data: null,
|
|
18
|
+
message: `Error: ${err}`
|
|
19
|
+
});
|
|
20
|
+
})
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Get user by ID
|
|
24
|
+
router.get('/:id', async (req, res) => {
|
|
25
|
+
Users.get({
|
|
26
|
+
id: req.params.id
|
|
27
|
+
}).then(user => {
|
|
28
|
+
if(user){
|
|
29
|
+
res.json({
|
|
30
|
+
success: true,
|
|
31
|
+
data: users,
|
|
32
|
+
message: null
|
|
33
|
+
});
|
|
34
|
+
}else{
|
|
35
|
+
res.status(404).json({
|
|
36
|
+
success: false,
|
|
37
|
+
data: null,
|
|
38
|
+
message: 'User not found'
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}).catch(err => {
|
|
42
|
+
res.status(500).json({
|
|
43
|
+
success: false,
|
|
44
|
+
data: null,
|
|
45
|
+
message: `Error: ${err}`
|
|
46
|
+
});
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Create new user
|
|
51
|
+
router.post('/', async (req, res) => {
|
|
52
|
+
const user = req.body;
|
|
53
|
+
Users.add(user).then(id => {
|
|
54
|
+
res.status(201).json({
|
|
55
|
+
success: true,
|
|
56
|
+
data: id,
|
|
57
|
+
message: null
|
|
58
|
+
});
|
|
59
|
+
}).catch(err => {
|
|
60
|
+
res.status(500).json({
|
|
61
|
+
success: false,
|
|
62
|
+
data: null,
|
|
63
|
+
message: `Error: ${err}`
|
|
64
|
+
});
|
|
65
|
+
})
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Update existing user
|
|
69
|
+
router.put('/:id', async (req, res) => {
|
|
70
|
+
|
|
71
|
+
const user = req.body;
|
|
72
|
+
Users.update(user).then(count=>{
|
|
73
|
+
if (count === 0) {
|
|
74
|
+
res.status(404).json({
|
|
75
|
+
success: false,
|
|
76
|
+
data: null,
|
|
77
|
+
message: 'User not found'
|
|
78
|
+
});
|
|
79
|
+
}else{
|
|
80
|
+
res.json({
|
|
81
|
+
success: true,
|
|
82
|
+
data: null,
|
|
83
|
+
message: 'User updated successfully'
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}).catch(err=>{
|
|
87
|
+
res.status(500).json({
|
|
88
|
+
success: false,
|
|
89
|
+
data: null,
|
|
90
|
+
message: `Error: ${err}`
|
|
91
|
+
});
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
router.delete('/:id', async (req, res) => {
|
|
96
|
+
Users.delete(req.params.id).then(deletedCount=>{
|
|
97
|
+
if (deletedCount === 0) {
|
|
98
|
+
res.status(404).json({
|
|
99
|
+
success: false,
|
|
100
|
+
data: null,
|
|
101
|
+
message: 'User not found'
|
|
102
|
+
});
|
|
103
|
+
}else{
|
|
104
|
+
res.json({
|
|
105
|
+
success: true,
|
|
106
|
+
data: null,
|
|
107
|
+
message: 'User deleted successfully'
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}).catch(err=>{
|
|
111
|
+
res.status(500).json({
|
|
112
|
+
success: false,
|
|
113
|
+
data: null,
|
|
114
|
+
message: `Error: ${err}`
|
|
115
|
+
});
|
|
116
|
+
})
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
module.exports = router;
|
package/src/index.js
CHANGED
|
@@ -12,7 +12,7 @@ const jwtLib = jwtLibFactory({
|
|
|
12
12
|
`/${serviceName}/auth/echo`,
|
|
13
13
|
`/${serviceName}/health`
|
|
14
14
|
],
|
|
15
|
-
signOptions: { expiresIn: '
|
|
15
|
+
signOptions: { expiresIn: '15m' }
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
const app = express();
|
|
@@ -47,6 +47,9 @@ getPort.default({
|
|
|
47
47
|
const authRouter = require('./controllers/auth.js')(jwtLib)
|
|
48
48
|
app.use(`/${serviceName}/auth`, authRouter)
|
|
49
49
|
|
|
50
|
+
const usersRouter = require('./controllers/users.js')
|
|
51
|
+
app.use(`/${serviceName}/users`, usersRouter)
|
|
52
|
+
|
|
50
53
|
app.listen(port, async () => {
|
|
51
54
|
console.log(`${serviceName} listening on port ${port}`)
|
|
52
55
|
await register()
|
package/src/models/users.js
CHANGED
|
@@ -34,6 +34,68 @@ class Users {
|
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
static get(options){
|
|
38
|
+
return new Promise(async (resolve, reject) => {
|
|
39
|
+
try{
|
|
40
|
+
let fields = '*'
|
|
41
|
+
if(options.id){
|
|
42
|
+
const users = await db(table).select(fields);
|
|
43
|
+
const safeUsers = users.map(({ password, ...rest }) => rest);
|
|
44
|
+
resolve(safeUsers)
|
|
45
|
+
}else{
|
|
46
|
+
const user = await db(table).select(fields)
|
|
47
|
+
.where({ id: options.id }).first();
|
|
48
|
+
if(user){
|
|
49
|
+
const { password, ...safeUser } = user;
|
|
50
|
+
resolve(safeUser)
|
|
51
|
+
}else{
|
|
52
|
+
resolve(null)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}catch(e){
|
|
56
|
+
reject(e)
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static add(user){
|
|
62
|
+
return new Promise(async (resolve, reject) => {
|
|
63
|
+
try{
|
|
64
|
+
user.password = db.raw('md5(?)', [user.password])
|
|
65
|
+
const [id] = await db(table).insert(user).returning('id');
|
|
66
|
+
resolve(id)
|
|
67
|
+
}catch(e){
|
|
68
|
+
reject(e)
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static delete(id){
|
|
74
|
+
return new Promise(async (resolve, reject) => {
|
|
75
|
+
try{
|
|
76
|
+
const deletedCount = await db(table).where({ id }).del();
|
|
77
|
+
resolve(deletedCount)
|
|
78
|
+
}catch(e){
|
|
79
|
+
reject(e)
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static update(user){
|
|
85
|
+
return new Promise(async (resolve, reject) => {
|
|
86
|
+
try{
|
|
87
|
+
if(user.password)
|
|
88
|
+
user.password = db.raw('md5(?)', [user.password])
|
|
89
|
+
const count = await db(table)
|
|
90
|
+
.where({ id: user.id })
|
|
91
|
+
.update(user);
|
|
92
|
+
resolve(count)
|
|
93
|
+
}catch(e){
|
|
94
|
+
reject(e)
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
37
99
|
}
|
|
38
100
|
|
|
39
101
|
module.exports = Users
|