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 CHANGED
@@ -107,6 +107,12 @@ node op admin:generate
107
107
 
108
108
  Copy **config/default.json.example** to **config/default.json** file.
109
109
 
110
+ Command:
111
+
112
+ ```bash
113
+ node op conf:generate
114
+ ```
115
+
110
116
  ### Seed database
111
117
 
112
118
  Load data from JSON or CSV file.
@@ -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
@@ -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
 
@@ -0,0 +1,7 @@
1
+ import User from './user.js';
2
+
3
+ const db = {};
4
+
5
+ db.User = User;
6
+
7
+ export default db;
@@ -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/authcontroller.js';
5
- import UserController from '../controllers/usercontroller.js';
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}controller.js`
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,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sip",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "main": "index.js",
5
5
  "bin": {
6
6
  "create-sip": "create-sip.js"
@@ -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