JOE
2018-10-22 7b5b1649944710d9d5525715a75e4abcef6f088d
add view page for role, module
16 files modified
7 files added
1 files deleted
795 ■■■■ changed files
bin/www 2 ●●● patch | view | raw | blame | history
config/config.js 2 ●●● patch | view | raw | blame | history
public/css/style.css 61 ●●●●● patch | view | raw | blame | history
public/css/style.css.map 4 ●●●● patch | view | raw | blame | history
public/css/style.sass 7 ●●●●● patch | view | raw | blame | history
public/css/style.scss 85 ●●●●● patch | view | raw | blame | history
routes/accounts.js 2 ●●● patch | view | raw | blame | history
routes/api/accounts.js 53 ●●●●● patch | view | raw | blame | history
routes/api/index.js 9 ●●●●● patch | view | raw | blame | history
routes/api/modules.js 48 ●●●●● patch | view | raw | blame | history
routes/api/roles.js 48 ●●●●● patch | view | raw | blame | history
routes/api/segment.js 49 ●●●●● patch | view | raw | blame | history
routes/api/taxonomy.js 69 ●●●●● patch | view | raw | blame | history
routes/index.js 10 ●●●●● patch | view | raw | blame | history
routes/modules.js 88 ●●●● patch | view | raw | blame | history
routes/roles.js 79 ●●●● patch | view | raw | blame | history
server.js 23 ●●●●● patch | view | raw | blame | history
util/util.js 25 ●●●●● patch | view | raw | blame | history
views/modules/create.html 19 ●●●● patch | view | raw | blame | history
views/modules/list.html 27 ●●●●● patch | view | raw | blame | history
views/modules/update.html 12 ●●●● patch | view | raw | blame | history
views/roles/create.html 23 ●●●● patch | view | raw | blame | history
views/roles/list.html 28 ●●●● patch | view | raw | blame | history
views/roles/update.html 22 ●●●● patch | view | raw | blame | history
bin/www
....@@ -12,7 +12,7 @@
1212 * Get port from environment and store in Express.
1313 */
1414
15
-var port = normalizePort(process.env.PORT || '3000');
15
+var port = normalizePort(process.env.PORT || '3303');
1616 app.set('port', port);
1717
1818 /**
config/config.js
....@@ -1,4 +1,4 @@
1
-module.exports = {
1
+module.exports = {
22 path: {
33 cdn: 'http://dmp.3ps.io/dmp'
44 },
public/css/style.css
....@@ -1,11 +1,62 @@
11 body {
2
- padding: 60px;
32 font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; }
43
5
-div {
6
- color: #00B7FF; }
4
+fieldset {
5
+ border: 1px solid #1d2531 !important;
6
+ width: 400px !important;
7
+ padding: 10px !important;
8
+ margin: 10px !important; }
79
8
-a {
9
- color: #00B7FF; }
10
+.menu {
11
+ width: 200px;
12
+ height: 100%;
13
+ background: #1d2531;
14
+ position: fixed;
15
+ left: 0;
16
+ top: 0;
17
+ bottom: 0; }
18
+ .menu div {
19
+ color: white;
20
+ padding: 10px 20px; }
21
+
22
+.header-temp {
23
+ position: fixed;
24
+ left: 200px;
25
+ top: 0px;
26
+ height: 30px;
27
+ right: 0;
28
+ padding: 20px; }
29
+ .header-temp a {
30
+ color: #0378d1;
31
+ text-decoration: none;
32
+ font-size: 20px;
33
+ margin-right: 15px;
34
+ cursor: pointer; }
35
+
36
+.content {
37
+ position: fixed;
38
+ left: 200px;
39
+ top: 50px;
40
+ bottom: 0;
41
+ right: 0;
42
+ padding: 0 20px 20px 20px;
43
+ margin-bottom: 50px;
44
+ overflow-y: scroll; }
45
+ .content a {
46
+ text-decoration: none;
47
+ color: #0378d1 !important;
48
+ font-size: 14px !important; }
49
+ .content h3 {
50
+ padding: 5px 20px; }
51
+ .content h3 .test-result {
52
+ color: #0378d1; }
53
+ .content label {
54
+ line-height: 30px; }
55
+ .content .error {
56
+ color: #ff5f1f; }
57
+
58
+.sub-link div {
59
+ font-size: 11px;
60
+ padding-left: 40px; }
1061
1162 /*# sourceMappingURL=style.css.map */
public/css/style.css.map
....@@ -2,8 +2,8 @@
22 "version": 3,
33 "file": "style.css",
44 "sources": [
5
- "style.sass"
5
+ "style.scss"
66 ],
77 "names": [],
8
- "mappings": "AAAA,AAAA,IAAI,CAAC;EACJ,OAAO,EAAE,IAAI;EACb,IAAI,EAAE,kDAAkD,GAAG;;AAC5D,AAAA,GAAG,CAAC;EACH,KAAK,EAAE,OAAO,GAAG;;AAClB,AAAA,CAAC,CAAC;EACD,KAAK,EAAE,OAAO,GAAG"
8
+ "mappings": "AAAA,AAAA,IAAI,CAAC;EACJ,IAAI,EAAE,kDAAkD,GACxD;;AAED,AAAA,QAAQ,CAAC;EACL,MAAM,EAAE,4BAA4B;EACpC,KAAK,EAAE,gBAAgB;EACvB,OAAO,EAAE,eAAe;EACxB,MAAM,EAAE,eAAe,GAC1B;;AAED,AAAA,KAAK,CAAA;EACD,KAAK,EAAE,KAAK;EACZ,MAAM,EAAE,IAAI;EACZ,UAAU,EAAE,OAAO;EACnB,QAAQ,EAAE,KAAK;EACf,IAAI,EAAE,CAAC;EACP,GAAG,EAAE,CAAC;EACN,MAAM,EAAE,CAAC,GAMZ;EAbD,AASI,KATC,CASD,GAAG,CAAC;IACH,KAAK,EAAE,KAAK;IACZ,OAAO,EAAE,SAAS,GAClB;;AAGL,AAAA,YAAY,CAAC;EACZ,QAAQ,EAAE,KAAK;EACf,IAAI,EAAE,KAAK;EACX,GAAG,EAAE,GAAG;EACR,MAAM,EAAE,IAAI;EACZ,KAAK,EAAE,CAAC;EACR,OAAO,EAAE,IAAI,GASb;EAfD,AAQC,YARW,CAQX,CAAC,CAAC;IACE,KAAK,EAAE,OAAO;IACd,eAAe,EAAE,IAAI;IACrB,SAAS,EAAE,IAAI;IACf,YAAY,EAAE,IAAI;IAClB,MAAM,EAAE,OAAO,GAClB;;AAGF,AAAA,QAAQ,CAAC;EACL,QAAQ,EAAE,KAAK;EACf,IAAI,EAAE,KAAK;EACX,GAAG,EAAE,IAAI;EACT,MAAM,EAAE,CAAC;EACT,KAAK,EAAE,CAAC;EACR,OAAO,EAAE,gBAAgB;EACzB,aAAa,EAAE,IAAI;EACnB,UAAU,EAAE,MAAM,GAsBrB;EA9BD,AAUI,QAVI,CAUJ,CAAC,CAAC;IACD,eAAe,EAAE,IAAI;IACrB,KAAK,EAAE,kBAAkB;IACzB,SAAS,EAAE,eAAe,GAC7B;EAdF,AAgBC,QAhBO,CAgBP,EAAE,CAAC;IACC,OAAO,EAAE,QAAQ,GAIjB;IArBL,AAkBK,QAlBG,CAgBP,EAAE,CAEE,YAAY,CAAA;MACX,KAAK,EAAE,OAAO,GACd;EApBN,AAuBC,QAvBO,CAuBP,KAAK,CAAC;IACF,WAAW,EAAE,IAAI,GACpB;EAzBF,AA2BC,QA3BO,CA2BP,MAAM,CAAC;IACN,KAAK,EAAE,OAAO,GACd;;AAGF,AAEC,SAFQ,CAER,GAAG,CAAC;EACA,SAAS,EAAE,IAAI;EACf,YAAY,EAAE,IAAI,GAClB"
99 }
public/css/style.sass
deleted file mode 100644
....@@ -1,7 +0,0 @@
1
-body
2
- padding: 60px
3
- font: 14px "Lucida Grande", Helvetica, Arial, sans-serif
4
-div
5
- color: #00B7FF
6
-a
7
- color: #00B7FF
public/css/style.scss
....@@ -0,0 +1,85 @@
1
+body {
2
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
3
+}
4
+
5
+fieldset {
6
+ border: 1px solid #1d2531 !important;
7
+ width: 400px !important;
8
+ padding: 10px !important;
9
+ margin: 10px !important;
10
+}
11
+
12
+.menu{
13
+ width: 200px;
14
+ height: 100%;
15
+ background: #1d2531;
16
+ position: fixed;
17
+ left: 0;
18
+ top: 0;
19
+ bottom: 0;
20
+
21
+ div {
22
+ color: white;
23
+ padding: 10px 20px;
24
+ }
25
+}
26
+
27
+.header-temp {
28
+ position: fixed;
29
+ left: 200px;
30
+ top: 0px;
31
+ height: 30px;
32
+ right: 0;
33
+ padding: 20px;
34
+
35
+ a {
36
+ color: #0378d1;
37
+ text-decoration: none;
38
+ font-size: 20px;
39
+ margin-right: 15px;
40
+ cursor: pointer;
41
+ }
42
+}
43
+
44
+.content {
45
+ position: fixed;
46
+ left: 200px;
47
+ top: 50px;
48
+ bottom: 0;
49
+ right: 0;
50
+ padding: 0 20px 20px 20px;
51
+ margin-bottom: 50px;
52
+ overflow-y: scroll;
53
+
54
+ a {
55
+ text-decoration: none;
56
+ color: #0378d1 !important;
57
+ font-size: 14px !important;
58
+ }
59
+
60
+ h3 {
61
+ padding: 5px 20px;
62
+ .test-result{
63
+ color: #0378d1;
64
+ }
65
+ }
66
+
67
+ label {
68
+ line-height: 30px;
69
+ }
70
+
71
+ .error {
72
+ color: #ff5f1f;
73
+ }
74
+}
75
+
76
+.sub-link {
77
+
78
+ div {
79
+ font-size: 11px;
80
+ padding-left: 40px;
81
+ }
82
+}
83
+
84
+
85
+
routes/accounts.js
....@@ -23,7 +23,7 @@
2323
2424 router.get('/', function(req, res, next) {
2525 Account.find({}).then(function (accounts) {
26
- res.send(accounts);
26
+ res.json(accounts);
2727 }).catch(err => {
2828 util.reportError(err, res);
2929 });
routes/api/accounts.js
....@@ -0,0 +1,53 @@
1
+const express = require('express');
2
+const router = express.Router();
3
+const cookie = require('cookie');
4
+const bcrypt = require('bcrypt');
5
+const mongoose = require('mongoose');
6
+
7
+const config = require('../../config/config.js');
8
+const util = require('../../util/util.js');
9
+
10
+const Account = require('../../model/account');
11
+
12
+router.post('/', function(req, res, next) {
13
+ const data = Object.assign(req.body) || {};
14
+ let hash = bcrypt.hashSync(data.passwd, 10);
15
+ data.passwd = hash;
16
+
17
+ Account.create(data).then(account => {
18
+ res.json(account);
19
+ }).catch(err => {
20
+ util.reportError(err, res);
21
+ });
22
+});
23
+
24
+router.get('/', function(req, res, next) {
25
+ Account.find({}).then(function (accounts) {
26
+ res.json(accounts);
27
+ }).catch(err => {
28
+ util.reportError(err, res);
29
+ });
30
+});
31
+
32
+router.get('/:account_id', function(req, res, next) {
33
+ Account.findOne({_id: req.params.account_id}).then(function (account) {
34
+ res.json(account);
35
+ }).catch(err => {
36
+ util.reportError(err, res);
37
+ });
38
+});
39
+
40
+router.put('/:account_id', function(req, res, next) {
41
+ const data = Object.assign(req.body) || {};
42
+ Account.updateMany({_id: req.params.account_id}, { $set: data}, function(err, response) {
43
+ err?util.reportError(err, res):res.json({'updated': response.n});
44
+ });
45
+});
46
+
47
+router.delete('/:account_id', function(req, res, next) {
48
+ Account.deleteMany({ _id: req.params.account_id }, function (err, response) {
49
+ err?util.reportError(err, res):res.json({'deleted': response.n});
50
+ });
51
+});
52
+
53
+module.exports = router;
routes/api/index.js
....@@ -0,0 +1,9 @@
1
+const router = require('express').Router();
2
+
3
+router.use('/accounts', require('./accounts'));
4
+router.use('/modules', require('./modules'));
5
+router.use('/roles', require('./roles'));
6
+router.use('/segment', require('./segment'));
7
+router.use('/taxonomy', require('./taxonomy'));
8
+
9
+module.exports = router;
routes/api/modules.js
....@@ -0,0 +1,48 @@
1
+const express = require('express');
2
+const router = express.Router();
3
+const mongoose = require('mongoose');
4
+
5
+const config = require('../../config/config.js');
6
+const util = require('../../util/util.js');
7
+
8
+const Modules = require('../../model/module');
9
+
10
+router.get('/:module_id', function(req, res, next) {
11
+ Modules.findOne({_id: req.params.module_id}).then(function (module) {
12
+ res.json(module);
13
+ }).catch(err => {
14
+ util.reportError(err, res);
15
+ });
16
+});
17
+
18
+router.get('/', function(req, res, next) {
19
+ Modules.find({}).then(function (modules) {
20
+ res.json(modules);
21
+ }).catch(err => {
22
+ util.reportError(err, res);
23
+ });
24
+});
25
+
26
+router.post('/', function(req, res, next) {
27
+ const data = Object.assign(req.body) || {};
28
+ Modules.create(data).then(module => {
29
+ res.json({'module_id': module._id});
30
+ }).catch(err => {
31
+ util.reportError(err, res);
32
+ });
33
+});
34
+
35
+router.put('/:module_id', function(req, res, next) {
36
+ const data = Object.assign(req.body) || {};
37
+ Modules.updateMany({_id: req.params.module_id}, { $set: data}, function(err, response) {
38
+ err?util.reportError(err, res):res.json({'updated': response.n});
39
+ });
40
+});
41
+
42
+router.delete('/:module_id', function(req, res, next) {
43
+ Modules.deleteMany({ _id: req.params.module_id }, function (err, response) {
44
+ err?util.reportError(err, res):res.json({'deleted': response.n});
45
+ });
46
+});
47
+
48
+module.exports = router;
routes/api/roles.js
....@@ -0,0 +1,48 @@
1
+const express = require('express');
2
+const router = express.Router();
3
+const mongoose = require('mongoose');
4
+
5
+const config = require('../../config/config.js');
6
+const util = require('../../util/util.js');
7
+
8
+const Roles = require('../../model/role');
9
+
10
+router.get('/:role_id', function(req, res, next) {
11
+ Roles.findOne({_id: req.params.role_id}).then(function (role) {
12
+ res.json(role);
13
+ }).catch(err => {
14
+ util.reportError(err, res);
15
+ });
16
+});
17
+
18
+router.get('/', function(req, res, next) {
19
+ Roles.find({}).then(function (roles) {
20
+ res.json(roles);
21
+ }).catch(err => {
22
+ util.reportError(err, res);
23
+ });
24
+});
25
+
26
+router.post('/', function(req, res, next) {
27
+ const data = Object.assign(req.body) || {};
28
+ Roles.create(data).then(role => {
29
+ res.json({'role_id': role._id});
30
+ }).catch(err => {
31
+ util.reportError(err, res);
32
+ });
33
+});
34
+
35
+router.put('/:role_id', function(req, res, next) {
36
+ const data = Object.assign(req.body) || {};
37
+ Roles.updateMany({_id: req.params.role_id}, { $set: data}, function(err, response) {
38
+ err?util.reportError(err, res):res.json({'updated': response.n});
39
+ });
40
+});
41
+
42
+router.delete('/:role_id', function(req, res, next) {
43
+ Roles.deleteMany({ _id: req.params.role_id }, function (err, response) {
44
+ err?util.reportError(err, res):res.json({'deleted': response.n});
45
+ });
46
+});
47
+
48
+module.exports = router;
routes/api/segment.js
....@@ -0,0 +1,49 @@
1
+const express = require('express');
2
+const router = express.Router();
3
+const mongoose = require('mongoose');
4
+
5
+// import custom lib
6
+const config = require('../../config/config.js');
7
+const util = require('../../util/util.js');
8
+
9
+const Segment = require('../../model/segmentation');
10
+
11
+router.get('/:segment_id', function(req, res, next) {
12
+ Segment.findOne({_id: req.params.segment_id}).then(function (segment) {
13
+ res.json(segment);
14
+ }).catch(err => {
15
+ util.reportError(err, res);
16
+ });
17
+});
18
+
19
+router.get('/', function(req, res, next) {
20
+ Segment.find({}).then(function (segments) {
21
+ res.json(segments);
22
+ }).catch(err => {
23
+ util.reportError(err, res);
24
+ });
25
+});
26
+
27
+router.post('/', function(req, res, next) {
28
+ const data = Object.assign(req.body) || {};
29
+ Segment.create(data).then(segment => {
30
+ res.json({'segment_id': segment._id});
31
+ }).catch(err => {
32
+ util.reportError(err, res);
33
+ });
34
+});
35
+
36
+router.put('/:segment_id', function(req, res, next) {
37
+ const data = Object.assign(req.body) || {};
38
+ Segment.updateMany({_id: req.params.segment_id}, { $set: data}, function(err, response) {
39
+ err?util.reportError(err, res):res.json({'updated': response.n});
40
+ });
41
+});
42
+
43
+router.delete('/:segment_id', function(req, res, next) {
44
+ Segment.deleteMany({_id: req.params.segment_id}, function (err, response) {
45
+ err?util.reportError(err, res):res.json({'deleted': response.n});
46
+ });
47
+});
48
+
49
+module.exports = router;
routes/api/taxonomy.js
....@@ -0,0 +1,69 @@
1
+const express = require('express');
2
+const router = express.Router();
3
+const mongoose = require('mongoose');
4
+
5
+const config = require('../../config/config.js');
6
+const util = require('../../util/util.js');
7
+
8
+const Taxonomy = require('../../model/taxonomy');
9
+
10
+router.get('/:key_taxonomy_id', function(req, res, next) {
11
+ Taxonomy.findOne({_id: req.params.key_taxonomy_id}).then(function (taxonomy) {
12
+ console.log(taxonomy);
13
+ res.json(taxonomy);
14
+ }).catch(err => {
15
+ util.reportError(err, res);
16
+ });
17
+});
18
+
19
+router.get('/', function(req, res, next) {
20
+ Taxonomy.find({}).then(function (taxonomys) {
21
+ res.json(taxonomys);
22
+ }).catch(err => {
23
+ util.reportError(err, res);
24
+ });
25
+});
26
+
27
+router.get('/search/name', function(req, res, next) {
28
+ Taxonomy.find({keyname: new RegExp(req.query.keyname, 'i')}).then(function (taxonomys) {
29
+ res.json(taxonomys);
30
+ }).catch(err => {
31
+ util.reportError(err, res);
32
+ });
33
+});
34
+
35
+router.get('/search/location', function(req, res, next) {
36
+ const filter = {};
37
+ filter.loc = req.query.lon;
38
+ filter.lat = req.query.lat;
39
+ filter.radius = req.query.radius;
40
+ filter.datetime = req.query.datetime;
41
+ // Taxonomy.find({keyname: new RegExp(req.query.keyname, 'i')}).then(function (taxonomys) {
42
+ res.json(filter);
43
+ // });
44
+});
45
+
46
+
47
+router.post('/', function(req, res, next) {
48
+ const data = Object.assign(req.body) || {};
49
+ Taxonomy.create(data).then(taxonomy => {
50
+ res.json({'key_taxonomy_id': taxonomy._id});
51
+ }).catch(err => {
52
+ util.reportError(err, res);
53
+ });
54
+});
55
+
56
+router.put('/:key_taxonomy_id', function(req, res, next) {
57
+ const data = Object.assign(req.body) || {};
58
+ Taxonomy.updateMany({_id: req.params.key_taxonomy_id}, { $set: data}, function(err, response) {
59
+ err?util.reportError(err, res):res.json({'updated': response.n});
60
+ });
61
+});
62
+
63
+router.delete('/:key_taxonomy_id', function(req, res, next) {
64
+ Taxonomy.deleteMany({ _id: req.params.key_taxonomy_id }, function (err, response) {
65
+ err?util.reportError(err, res):res.json({'deleted': response.n});
66
+ });
67
+});
68
+
69
+module.exports = router;
routes/index.js
....@@ -49,7 +49,6 @@
4949 router.post('/login', function(req, res, next) {
5050 Account.findOne({login_id:req.body.loginId}).then(function (user) {
5151 let result = {cdnRoot: config.path.cdn};
52
-
5352 if(user == null) {
5453 result.invalidUser = true;
5554 res.render('login', result);
....@@ -75,4 +74,13 @@
7574 });
7675 });
7776
77
+
78
+// import web router
79
+router.use('/segment', require('./segment'));
80
+router.use('/taxonomy', require('./taxonomy'));
81
+router.use('/accounts', require('./accounts'));
82
+router.use('/modules', require('./modules'));
83
+router.use('/roles', require('./roles'));
84
+
85
+
7886 module.exports = router;
routes/modules.js
....@@ -6,43 +6,91 @@
66 const util = require('../util/util.js');
77
88 const Modules = require('../model/module');
9
-
10
-router.get('/:module_id', function(req, res, next) {
11
- Modules.findOne({_id: req.params.module_id}).then(function (module) {
12
- res.json(module);
13
- }).catch(err => {
14
- util.reportError(err, res);
15
- });
16
-});
9
+const Roles = require('../model/role');
1710
1811 router.get('/', function(req, res, next) {
1912 Modules.find({}).then(function (modules) {
20
- res.json(modules);
13
+ const param = util.initParam();
14
+ param.modules = modules;
15
+ res.render('modules/list', param);
2116 }).catch(err => {
2217 util.reportError(err, res);
2318 });
2419 });
2520
26
-router.post('/', function(req, res, next) {
27
- const data = Object.assign(req.body) || {};
28
- Modules.create(data).then(module => {
29
- res.json({'module_id': module._id});
30
- }).catch(err => {
21
+router.get('/create', function(req, res, next) {
22
+ Modules.find({}).then(function (modules) {
23
+ const param = util.initParam();
24
+ param.modules = modules;
25
+ res.render('modules/create', param);
26
+ }).catch(err => {
3127 util.reportError(err, res);
3228 });
3329 });
3430
35
-router.put('/:module_id', function(req, res, next) {
31
+router.post('/create', function(req, res, next) {
3632 const data = Object.assign(req.body) || {};
37
- Modules.updateMany({_id: req.params.module_id}, { $set: data}, function(err, response) {
38
- err?util.reportError(err, res):res.json({'updated': response.n});
33
+ Modules.create(data).then(role => {
34
+ res.redirect('/modules/');
35
+ }).catch(err => {
36
+ Modules.find({}).then(function (modules) {
37
+ const param = util.initParam();
38
+ param.error = util.errorJson(err).result_msg;
39
+ param.modules = modules;
40
+ param.data = data;
41
+ res.render('modules/create', param);
42
+ }).catch(err => {
43
+ util.reportError(err, res);
44
+ });
3945 });
4046 });
4147
42
-router.delete('/:module_id', function(req, res, next) {
43
- Modules.deleteMany({ _id: req.params.module_id }, function (err, response) {
44
- err?util.reportError(err, res):res.json({'deleted': response.n});
48
+
49
+router.get('/:role_id', function(req, res, next) {
50
+ Modules.find({}).then(function (modules) {
51
+ const param = util.initParam();
52
+ param.modules = modules;
53
+ Modules.findOne({_id: req.params.role_id}).then(function (module) {
54
+ param.module = module;
55
+ res.render('modules/update', param);
56
+ }).catch(err => {
57
+ util.reportError(err, res);
58
+ });
59
+ }).catch(err => {
60
+ util.reportError(err, res);
4561 });
4662 });
4763
64
+router.post('/:module_id', function(req, res, next) {
65
+ const data = Object.assign(req.body) || {};
66
+ Modules.updateMany({_id: req.params.module_id}, { $set: data}, function(err, response) {
67
+ const param = util.initParam();
68
+ if(err) {
69
+ param.error = util.errorJson(err).result_msg;
70
+ Modules.find({}).then(function (modules) {
71
+ param.modules = modules;
72
+ res.render('modules/update', param);
73
+ }).catch(err => {
74
+ res.render('modules/update', param);
75
+ });
76
+ } else {
77
+ res.redirect('/modules/');
78
+ }
79
+ });
80
+});
81
+
82
+
83
+// router.put('/:module_id', function(req, res, next) {
84
+// const data = Object.assign(req.body) || {};
85
+// Modules.updateMany({_id: req.params.module_id}, { $set: data}, function(err, response) {
86
+// err?util.reportError(err, res):res.json({'updated': response.n});
87
+// });
88
+// });
89
+
90
+// router.delete('/:module_id', function(req, res, next) {
91
+// Modules.deleteMany({ _id: req.params.module_id }, function (err, response) {
92
+// err?util.reportError(err, res):res.json({'deleted': response.n});
93
+// });
94
+// });
95
+
4896 module.exports = router;
routes/roles.js
....@@ -6,43 +6,84 @@
66 const util = require('../util/util.js');
77
88 const Roles = require('../model/role');
9
-
10
-router.get('/:role_id', function(req, res, next) {
11
- Roles.findOne({_id: req.params.role_id}).then(function (role) {
12
- res.json(role);
13
- }).catch(err => {
14
- util.reportError(err, res);
15
- });
16
-});
9
+const Modules = require('../model/module');
1710
1811 router.get('/', function(req, res, next) {
1912 Roles.find({}).then(function (roles) {
20
- res.json(roles);
13
+ const param = util.initParam();
14
+ param.roles = roles;
15
+ res.render('roles/list', param);
2116 }).catch(err => {
2217 util.reportError(err, res);
2318 });
2419 });
2520
26
-router.post('/', function(req, res, next) {
27
- const data = Object.assign(req.body) || {};
28
- Roles.create(data).then(role => {
29
- res.json({'role_id': role._id});
30
- }).catch(err => {
21
+router.get('/create', function(req, res, next) {
22
+ Modules.find({}).then(function (modules) {
23
+ const param = util.initParam();
24
+ param.modules = modules;
25
+ res.render('roles/create', param);
26
+ }).catch(err => {
3127 util.reportError(err, res);
3228 });
3329 });
3430
35
-router.put('/:role_id', function(req, res, next) {
31
+router.post('/create', function(req, res, next) {
32
+ const data = Object.assign(req.body) || {};
33
+ data.welcome_page_modile = util.generatePath(data.welcome_page_modile);
34
+ Roles.create(data).then(role => {
35
+ res.redirect('/roles/');
36
+ }).catch(err => {
37
+ Modules.find({}).then(function (modules) {
38
+ const param = util.initParam();
39
+ param.modules = modules;
40
+ param.error = util.errorJson(err).result_msg;
41
+ param.data = data;
42
+ res.render('roles/create', param);
43
+ }).catch(err => {
44
+ util.reportError(err, res);
45
+ });
46
+ });
47
+});
48
+
49
+router.get('/:role_id', function(req, res, next) {
50
+ Modules.find({}).then(function (modules) {
51
+ const param = util.initParam();
52
+ param.modules = modules;
53
+ Roles.findOne({_id: req.params.role_id}).then(function (role) {
54
+ param.role = role;
55
+ res.render('roles/update', param);
56
+ }).catch(err => {
57
+ util.reportError(err, res);
58
+ });
59
+ }).catch(err => {
60
+ util.reportError(err, res);
61
+ });
62
+});
63
+
64
+router.post('/:role_id', function(req, res, next) {
3665 const data = Object.assign(req.body) || {};
3766 Roles.updateMany({_id: req.params.role_id}, { $set: data}, function(err, response) {
38
- err?util.reportError(err, res):res.json({'updated': response.n});
67
+ const param = util.initParam();
68
+ if(err) {
69
+ param.error = util.errorJson(err).result_msg;
70
+ Modules.find({}).then(function (modules) {
71
+ param.modules = modules;
72
+ res.render('roles/update', param);
73
+ }).catch(err => {
74
+ res.render('roles/update', param);
75
+ });
76
+ } else {
77
+ res.redirect('/roles/');
78
+ }
3979 });
4080 });
4181
4282 router.delete('/:role_id', function(req, res, next) {
43
- Roles.deleteMany({ _id: req.params.role_id }, function (err, response) {
44
- err?util.reportError(err, res):res.json({'deleted': response.n});
45
- });
83
+ res.json(res);
84
+ // Roles.deleteMany({ _id: req.params.role_id }, function (err, response) {
85
+ // err?util.reportError(err, res):res.json({'deleted': response.n});
86
+ // });
4687 });
4788
4889 module.exports = router;
server.js
....@@ -3,16 +3,12 @@
33 const path = require('path');
44 const cookieParser = require('cookie-parser');
55 const logger = require('morgan');
6
-const sassMiddleware = require('node-sass-middleware');
6
+const scssMiddleware = require('node-sass-middleware');
77 const mustacheExpress = require('mustache-express');
88
99 const config = require('./config/config.js');
1010 const indexRouter = require('./routes/index');
11
-const accountsRouter = require('./routes/accounts');
12
-const modulesRouter = require('./routes/modules');
13
-const rolesRouter = require('./routes/roles');
14
-const segmentRouter = require('./routes/segment');
15
-const taxonomyRouter = require('./routes/taxonomy');
11
+const apiRouter = require('./routes/api/index');
1612
1713 // Set up mongoose connection
1814 const mongoose = require('mongoose');
....@@ -40,21 +36,18 @@
4036 app.use(express.json());
4137 app.use(express.urlencoded({ extended: false }));
4238 app.use(cookieParser());
43
-app.use(sassMiddleware({
39
+app.use(scssMiddleware({
4440 src: path.join(__dirname, 'public'),
4541 dest: path.join(__dirname, 'public'),
46
- indentedSyntax: true, // true = .sass and false = .scss
47
- sourceMap: true
42
+ indentedSyntax: false, // true = .sass and false = .scss
43
+ sourceMap: true,
44
+ debug: true
4845 }));
4946 app.use(express.static(path.join(__dirname, 'public')));
5047
5148 //add router
52
-app.use('/', indexRouter);
53
-app.use('/accounts', accountsRouter);
54
-app.use('/modules', modulesRouter);
55
-app.use('/roles', rolesRouter);
56
-app.use('/segment', segmentRouter);
57
-app.use('/taxonomy', taxonomyRouter);
49
+app.use('/', require('./routes/index'));
50
+app.use('/api', require('./routes/api/index'));
5851
5952 // catch 404 and forward to error handler
6053 app.use(function(req, res, next) {
util/util.js
....@@ -3,6 +3,26 @@
33
44 const config = require('../config/config.js');
55
6
+
7
+function generatePath(path){
8
+ console.log(path);
9
+ path = path.replace(/\s/g,'');
10
+ if (path.indexOf("/") != 0) {
11
+ path = "/" + path;
12
+ }
13
+
14
+ if (path.lastIndexOf("/") == (path.length - 1)) {
15
+ path = path.substring(0, path.length - 1);
16
+ }
17
+ return path;
18
+}
19
+
20
+function initParam() {
21
+ const param = {};
22
+ param.cdnRoot = config.path.cdn;
23
+ return param;
24
+}
25
+
626 /****************************************
727 **************** LOGGER *****************
828 *****************************************/
....@@ -24,7 +44,7 @@
2444 replace(/\..+/, '') +' - '+msg);
2545 }
2646
27
-function errorJson(err){
47
+function errorJson(err){
2848 let resultCode = 500;
2949 let resultMsg = 'Internal server error.';
3050 // console.log(err);
....@@ -48,6 +68,7 @@
4868 }
4969
5070 function reportError(err, res){
71
+ console.log(err);
5172 const error = errorJson(err);
5273 return res.status(error.result).send(error);
5374 }
....@@ -95,6 +116,8 @@
95116 }
96117
97118 module.exports = {
119
+ generatePath: generatePath,
120
+ initParam: initParam,
98121 info: info,
99122 infoObj: infoObj,
100123 error: error,
views/modules/create.html
....@@ -2,34 +2,35 @@
22 <div class="content">
33 <form action="create" method="post">
44 <h1>Add Module</h1>
5
- {{#result}}
6
- <h4>{{result}}</h4>
7
- {{/result}}
5
+ {{#error}}
6
+ <h4 class="error">{{error}}</h4>
7
+ {{/error}}
88 <div>
99 <label for="name">Name :</label>
10
- <input type="text" name="name" id="name" placeholder="Name" required/>
10
+ <input type="text" name="name" id="name" placeholder="Name" required
11
+ value="{{data.name}}" />
1112 </div>
1213
1314 <div>
1415 <label for="name">Path :</label>
15
- <input type="text" name="path" id="path" placeholder="Path" required/>
16
+ <input type="text" name="path" id="path" placeholder="Path" required
17
+ value="{{data.path}}" />
1618 </div>
1719
1820 <div>
1921 <label for="description">Description :</label>
20
- <textarea type="text" name="description" id="description" placeholder="Description"></textarea>
22
+ <textarea type="text" name="description" id="description" placeholder="Description">{{data.description}}</textarea>
2123 </div>
2224
2325 <div>
24
- <label for="parentId">Parent Module :</label>
25
- <select id="parentId" name="parentId">
26
+ <label for="parent_id">Parent Module :</label>
27
+ <select id="parent_id" name="parent_id">
2628 <option value="">------</option>
2729 {{#modules}}
2830 <option value="{{id}}">{{name}}</option>
2931 {{/modules}}
3032 </select>
3133 </div>
32
-
3334 <button type="submit">Add Module</button>
3435 </form>
3536 </div>
views/modules/list.html
....@@ -5,9 +5,32 @@
55 {{#modules}}
66 <h3>
77 {{name}}
8
- <a href="{{contextRoot}}/modules/update?id={{id}}"><span>edit</span></a>
9
- <a href="{{contextRoot}}/modules/delete?id={{id}}"><span>delete</span></a>
8
+ <a href="/modules/{{id}}"><span>edit</span></a>
9
+ <a id="{{id}}" class="btn_delete" href="#"><span>delete</span></a>
1010 </h3>
1111 {{/modules}}
1212 </div>
13
+<script>
14
+ $(document).ready(function () {
15
+ $('.btn_delete').on('click', function(){
16
+ if(confirm("Are you sure you want to delete this?")){
17
+ const id = $(this).attr('id');
18
+ $.ajax({
19
+ url: "/api/modules/" + id,
20
+ method:"delete"
21
+ }).done(function(data) {
22
+ if(data.deleted == 1) {
23
+ $('#'+id).parent().remove();
24
+ }
25
+ }).fail(function(error) {
26
+ alert(error);
27
+ });
28
+ }
29
+ else{
30
+ return false;
31
+ }
32
+ });
33
+ });
34
+</script>
35
+</div>
1336 {{>layout/footer}}
views/modules/update.html
....@@ -1,6 +1,6 @@
11 {{>layout/header-temp}}
22 <div class="content">
3
- <form action="{{contextRoot}}/modules/update" method="post">
3
+ <form method="post">
44 <h1>Modify Module</h1>
55 {{#result}}
66 <h4>{{result}}</h4>
....@@ -24,8 +24,8 @@
2424 </div>
2525
2626 <div>
27
- <label for="parentId">Parent Module :</label>
28
- <select id="parentId" name="parentId">
27
+ <label for="parent_id">Parent Module :</label>
28
+ <select id="parent_id" name="parent_id">
2929 <option value="">------</option>
3030 {{#modules}}
3131 <option value="{{id}}">{{name}}</option>
....@@ -44,9 +44,9 @@
4444 </div>
4545 <script>
4646 $(function () {
47
- {{#hasParentId}}
48
- $('#parentId').val('{{hasParentId}}');
49
- {{/hasParentId}}
47
+ {{#module.parent_id}}
48
+ $('#parent_id').val('{{module.parent_id}}');
49
+ {{/module.parent_id}}
5050 });
5151 </script>
5252 {{>layout/footer}}
views/roles/create.html
....@@ -2,16 +2,17 @@
22 <div class="content">
33 <form action="create" method="post">
44 <h1>Add Role</h1>
5
- {{#result}}
6
- <h4>{{result}}</h4>
7
- {{/result}}
5
+ {{#error}}
6
+ <h4 class="error">{{error}}</h4>
7
+ {{/error}}
88 <div>
99 <label for="name">Name :</label>
10
- <input type="text" name="name" id="name" placeholder="Name" required/>
10
+ <input type="text" name="name" id="name" placeholder="Name" required
11
+ value="{{data.name}}" />
1112 </div>
1213 <div>
13
- <label for="welcomePageModile">Welcome Page :</label>
14
- <input type="text" name="welcomePageModile" id="welcomePageModile" placeholder="Welcome Page Mobile"/>
14
+ <label for="welcome_page_modile">Welcome Page :</label>
15
+ <input type="text" name="welcome_page_modile" id="welcome_page_modile" placeholder="Welcome Page Mobile" value="{{data.welcome_page_modile}}" />
1516 </div>
1617 <div>
1718 <label for="accessibles">Accessibles :</label>
....@@ -19,13 +20,13 @@
1920 <div id="accessibles">
2021 {{#modules}}
2122 <div>
22
- {{#parentId}}
23
+ {{#parent_id}}
2324 <span class="empty-space">&nbsp;&nbsp;&nbsp;</span>
24
- {{/parentId}}
25
+ {{/parent_id}}
2526 <input type="checkbox" id="{{id}}" name="accessibles" value="{{id}}"
26
- {{#parentId}}
27
- data-parent-id="{{parentId}}"
28
- {{/parentId}}
27
+ {{#parent_id}}
28
+ data-parent-id="{{parent_id}}"
29
+ {{/parent_id}}
2930 />
3031 <label for="{{id}}">{{name}}</label>
3132 </div>
views/roles/list.html
....@@ -1,12 +1,34 @@
11 {{>layout/header-temp}}
22 <div class="content">
3
- <h2>Role List&nbsp;<a href="{{contextRoot}}/roles/create">ADD</a></h2>
3
+ <h2>Role List&nbsp;<a href="/roles/create">ADD</a></h2>
44 {{#roles}}
55 <h3>
66 {{name}}
7
- <a href="{{contextRoot}}/roles/update?id={{id}}"><span>edit</span></a>
8
- <a href="{{contextRoot}}/roles/delete?id={{id}}"><span>delete</span></a>
7
+ <a href="/roles/{{id}}"><span>edit</span></a>
8
+ <a id="{{id}}" class="btn_delete" href="#"><span>delete</span></a>
99 </h3>
1010 {{/roles}}
1111 </div>
12
+<script>
13
+ $(document).ready(function () {
14
+ $('.btn_delete').on('click', function(){
15
+ if(confirm("Are you sure you want to delete this?")){
16
+ const id = $(this).attr('id');
17
+ $.ajax({
18
+ url: "/api/roles/" + id,
19
+ method:"delete"
20
+ }).done(function(data) {
21
+ if(data.deleted == 1) {
22
+ $('#'+id).parent().remove();
23
+ }
24
+ }).fail(function(error) {
25
+ alert(error);
26
+ });
27
+ }
28
+ else{
29
+ return false;
30
+ }
31
+ });
32
+ });
33
+</script>
1234 {{>layout/footer}}
views/roles/update.html
....@@ -1,6 +1,6 @@
11 {{>layout/header-temp}}
22 <div class="content">
3
- <form action="{{contextRoot}}/roles/update" method="post">
3
+ <form method="post">
44 <h1>Modify Role</h1>
55 {{#result}}
66 <h4>{{result}}</h4>
....@@ -12,9 +12,9 @@
1212 <input type="hidden" name="id" id="id" required value="{{id}}"/>
1313 </div>
1414 <div>
15
- <label for="welcomePageModile">Welcome Page :</label>
16
- <input type="text" name="welcomePageModile" id="welcomePageModile" placeholder="Welcome Page Mobile"
17
- value="{{welcomePageModile}}"/>
15
+ <label for="welcome_page_modile">Welcome Page :</label>
16
+ <input type="text" name="welcome_page_modile" id="welcome_page_modile" placeholder="Welcome Page Mobile"
17
+ value="{{welcome_page_modile}}"/>
1818 </div>
1919 <div>
2020 <label for="accessibles">Accessibles :</label>
....@@ -22,13 +22,13 @@
2222 <div id="accessibles">
2323 {{#modules}}
2424 <div>
25
- {{#parentId}}
25
+ {{#parent_id}}
2626 <span class="empty-space">&nbsp;&nbsp;&nbsp;</span>
27
- {{/parentId}}
27
+ {{/parent_id}}
2828 <input type="checkbox" id="{{id}}" name="accessibles" value="{{id}}"
29
- {{#parentId}}
30
- data-parent-id="{{parentId}}"
31
- {{/parentId}}
29
+ {{#parent_id}}
30
+ data-parent-id="{{parent_id}}"
31
+ {{/parent_id}}
3232 />
3333 <label for="{{id}}">{{name}}</label>
3434 </div>
....@@ -49,8 +49,8 @@
4949 <script>
5050 $(function () {
5151 {{#role.accessibles}}
52
- $('#{{id}}').prop('checked', true);
53
- {{/role.accessibles}}
52
+ $('#{{.}}').prop('checked', true);
53
+ {{/role.accessibles}}
5454 $('input[type=checkbox]').on('change', function () {
5555 const checked = $(this).is(':checked');
5656 const id = $(this).val();