create-sip 1.0.1 → 1.1.0
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/README.md +6 -0
- package/expressapi/app/controllers/userController.js +94 -0
- package/expressapi/app/index.js +6 -0
- package/expressapi/app/models/modrels.js +7 -0
- package/expressapi/app/models/user.js +2 -11
- package/expressapi/app/routes/api.js +2 -2
- package/expressapi/op +1 -1
- package/expressapi/templates/controllerTemplate.js +8 -4
- package/expressapi/templates/modelTemplate.js +0 -10
- package/package.json +1 -1
- package/expressapi/app/controllers/usercontroller.js +0 -25
- /package/expressapi/app/controllers/{authcontroller.js → authController.js} +0 -0
package/README.md
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import bcrypt from 'bcryptjs'
|
|
2
|
+
import User from '../models/user.js'
|
|
3
|
+
|
|
4
|
+
const UserController = {
|
|
5
|
+
async index(req, res) {
|
|
6
|
+
try {
|
|
7
|
+
UserController.tryIndex(req, res)
|
|
8
|
+
}catch(error) {
|
|
9
|
+
res.status(500)
|
|
10
|
+
res.json({
|
|
11
|
+
success: false,
|
|
12
|
+
message: 'Error! The query is failed!'
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
async tryIndex(req, res) {
|
|
17
|
+
const users = await User.findAll()
|
|
18
|
+
res.status(200)
|
|
19
|
+
res.json({
|
|
20
|
+
success: true,
|
|
21
|
+
data: users
|
|
22
|
+
})
|
|
23
|
+
},
|
|
24
|
+
async show(req, res) {
|
|
25
|
+
try {
|
|
26
|
+
await UserController.tryShow(req, res)
|
|
27
|
+
}catch(error) {
|
|
28
|
+
res.status(500)
|
|
29
|
+
res.json({
|
|
30
|
+
success: false,
|
|
31
|
+
message: 'Error! The query is failed!'
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
async tryShow(req, res) {
|
|
36
|
+
const user = await User.findByPk(req.params.id)
|
|
37
|
+
res.status(200)
|
|
38
|
+
res.json({
|
|
39
|
+
success: true,
|
|
40
|
+
data: user
|
|
41
|
+
})
|
|
42
|
+
},
|
|
43
|
+
async create(req, res) {
|
|
44
|
+
var clientError = false;
|
|
45
|
+
try {
|
|
46
|
+
if(!req.body.name ||
|
|
47
|
+
!req.body.email ||
|
|
48
|
+
!req.body.password ||
|
|
49
|
+
!req.body.password_confirmation) {
|
|
50
|
+
clientError = true
|
|
51
|
+
throw new Error('Error! Bad request data!')
|
|
52
|
+
}
|
|
53
|
+
if(req.body.password != req.body.password_confirmation) {
|
|
54
|
+
clientError = true
|
|
55
|
+
throw new Error('Error! The two password is not same!')
|
|
56
|
+
}
|
|
57
|
+
const user = await User.findOne({
|
|
58
|
+
where: { name: req.body.name }
|
|
59
|
+
})
|
|
60
|
+
if(user) {
|
|
61
|
+
clientError = true
|
|
62
|
+
throw new Error('Error! User already exists: ' + user.name)
|
|
63
|
+
}
|
|
64
|
+
await UserController.tryCreate(req, res)
|
|
65
|
+
}catch(error) {
|
|
66
|
+
if (clientError) {
|
|
67
|
+
res.status(400)
|
|
68
|
+
}else {
|
|
69
|
+
res.status(500)
|
|
70
|
+
}
|
|
71
|
+
res.json({
|
|
72
|
+
success: false,
|
|
73
|
+
message: 'Error! The query is failed!',
|
|
74
|
+
error: error.message
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
async tryCreate(req, res) {
|
|
79
|
+
const newUser = {
|
|
80
|
+
name: req.body.name,
|
|
81
|
+
email: req.body.email,
|
|
82
|
+
password: bcrypt.hashSync(req.body.password)
|
|
83
|
+
}
|
|
84
|
+
const userData = await User.create(newUser)
|
|
85
|
+
res.status(201)
|
|
86
|
+
res.json({
|
|
87
|
+
success: true,
|
|
88
|
+
data: userData
|
|
89
|
+
})
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export default UserController
|
package/expressapi/app/index.js
CHANGED
|
@@ -4,6 +4,12 @@ import cors from 'cors'
|
|
|
4
4
|
import fs from 'fs'
|
|
5
5
|
import router from './routes/api.js'
|
|
6
6
|
import { readJson } from '../tools/readjson.js'
|
|
7
|
+
import './models/modrels.js'
|
|
8
|
+
import sequelize from './database/database.js'
|
|
9
|
+
|
|
10
|
+
await sequelize.authenticate({
|
|
11
|
+
alter: true
|
|
12
|
+
})
|
|
7
13
|
|
|
8
14
|
const config = await readJson('config/default.json')
|
|
9
15
|
|
|
@@ -2,19 +2,10 @@ import { DataTypes } from 'sequelize'
|
|
|
2
2
|
import sequelize from '../database/database.js'
|
|
3
3
|
|
|
4
4
|
const User = sequelize.define('user', {
|
|
5
|
-
id: {
|
|
6
|
-
type: DataTypes.INTEGER,
|
|
7
|
-
autoIncrement: true,
|
|
8
|
-
primaryKey: true
|
|
9
|
-
},
|
|
10
5
|
name: { type: DataTypes.STRING, allowNull: false },
|
|
11
6
|
email: { type: DataTypes.STRING, allowNull: true },
|
|
12
|
-
password: { type: DataTypes.STRING , allowNull: false }
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
sequelize.sync({
|
|
17
|
-
force: false
|
|
7
|
+
password: { type: DataTypes.STRING , allowNull: false },
|
|
8
|
+
roleId: { type: DataTypes.INTEGER, defaultValue: 0 }
|
|
18
9
|
})
|
|
19
10
|
|
|
20
11
|
export default User
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import Router from 'express'
|
|
2
2
|
const router = Router()
|
|
3
3
|
|
|
4
|
-
import AuthController from '../controllers/
|
|
5
|
-
import UserController from '../controllers/
|
|
4
|
+
import AuthController from '../controllers/authController.js';
|
|
5
|
+
import UserController from '../controllers/userController.js';
|
|
6
6
|
import verifyToken from '../middlewares/authjwt.js';
|
|
7
7
|
|
|
8
8
|
router.post('/register', AuthController.register)
|
package/expressapi/op
CHANGED
|
@@ -83,7 +83,7 @@ async function copyController(className) {
|
|
|
83
83
|
const lowerName = className.toLowerCase()
|
|
84
84
|
|
|
85
85
|
const src = 'templates/controllerTemplate.js'
|
|
86
|
-
const dest = `app/controllers/${lowerName}
|
|
86
|
+
const dest = `app/controllers/${lowerName}Controller.js`
|
|
87
87
|
|
|
88
88
|
if(await startCheckIfFileExists(dest)) {
|
|
89
89
|
process.exit(1);
|
|
@@ -8,7 +8,8 @@ const ThingController = {
|
|
|
8
8
|
res.status(500)
|
|
9
9
|
res.json({
|
|
10
10
|
success: false,
|
|
11
|
-
message: 'Error! The query is failed!'
|
|
11
|
+
message: 'Error! The query is failed!',
|
|
12
|
+
error: error.message
|
|
12
13
|
})
|
|
13
14
|
}
|
|
14
15
|
},
|
|
@@ -27,7 +28,8 @@ const ThingController = {
|
|
|
27
28
|
res.status(500)
|
|
28
29
|
res.json({
|
|
29
30
|
success: false,
|
|
30
|
-
message: 'Error! The query is failed!'
|
|
31
|
+
message: 'Error! The query is failed!',
|
|
32
|
+
error: error.message
|
|
31
33
|
})
|
|
32
34
|
}
|
|
33
35
|
},
|
|
@@ -46,7 +48,8 @@ const ThingController = {
|
|
|
46
48
|
res.status(500)
|
|
47
49
|
res.json({
|
|
48
50
|
success: false,
|
|
49
|
-
message: 'Error! The query is failed!'
|
|
51
|
+
message: 'Error! The query is failed!',
|
|
52
|
+
error: error.message
|
|
50
53
|
})
|
|
51
54
|
}
|
|
52
55
|
},
|
|
@@ -98,7 +101,8 @@ const ThingController = {
|
|
|
98
101
|
res.status(500)
|
|
99
102
|
res.json({
|
|
100
103
|
success: false,
|
|
101
|
-
message: 'Error! The query is failed!'
|
|
104
|
+
message: 'Error! The query is failed!',
|
|
105
|
+
error: error.message
|
|
102
106
|
})
|
|
103
107
|
}
|
|
104
108
|
},
|
|
@@ -2,17 +2,7 @@ import { DataTypes } from 'sequelize'
|
|
|
2
2
|
import sequelize from '../database/database.js'
|
|
3
3
|
|
|
4
4
|
const Thing = sequelize.define('thing', {
|
|
5
|
-
id: {
|
|
6
|
-
type: DataTypes.INTEGER,
|
|
7
|
-
autoIncrement: true,
|
|
8
|
-
primaryKey: true
|
|
9
|
-
},
|
|
10
5
|
name: { type: DataTypes.STRING, allowNull: false }
|
|
11
6
|
})
|
|
12
7
|
|
|
13
|
-
|
|
14
|
-
sequelize.sync({
|
|
15
|
-
force: false
|
|
16
|
-
})
|
|
17
|
-
|
|
18
8
|
export default Thing
|
package/package.json
CHANGED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import User from '../models/user.js'
|
|
2
|
-
|
|
3
|
-
const UserController = {
|
|
4
|
-
async index(req, res) {
|
|
5
|
-
try {
|
|
6
|
-
UserController.tryIndex(req, res)
|
|
7
|
-
}catch(error) {
|
|
8
|
-
res.status(500)
|
|
9
|
-
res.json({
|
|
10
|
-
success: false,
|
|
11
|
-
message: 'Error! The query is failed!'
|
|
12
|
-
})
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
async tryIndex(req, res) {
|
|
16
|
-
const users = await User.findAll()
|
|
17
|
-
res.status(200)
|
|
18
|
-
res.json({
|
|
19
|
-
success: true,
|
|
20
|
-
data: users
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export default UserController
|
|
File without changes
|