-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes.js
129 lines (123 loc) · 3.4 KB
/
indexes.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const {
authAdmin,
authUser,
authRole,
authAgency,
} = require("../middleware/auth");
const routes = require("express").Router();
const database = require("../services/database");
routes.get(
"/",
authUser,
authRole,
authAgency,
authAdmin,
async (req, res, next) => {
try {
const indexesResult = await database.indexes.findAll();
const indexTablesResult = await database.index_tables.findAll();
const indexes = {};
const indexTables = [];
for (i in indexTablesResult) {
indexTables.push(indexTablesResult[i].dataValues.name);
}
for (let i in indexTables) {
indexes[`${indexTables[i]}`] = [];
for (let j in indexesResult) {
if (
indexesResult[i].dataValues.id ===
indexesResult[j].dataValues.indexTableId
) {
indexes[`${indexTables[i]}`].push(indexesResult[j].name);
}
}
indexes[`${indexTables[i]}`].sort();
}
indexTables.sort();
res.render("user_views/indexes", {
indexes: indexes,
indexTables: indexTables,
user: req.session.user,
});
} catch (err) {
console.error(err);
} //TODO: better handling
}
);
// TODO: implement index table change
routes.post(
"/",
authUser,
authRole,
authAgency,
authAdmin,
async (req, res, next) => {
try {
let indextable = await database.index_tables.findOne({
where: { name: req.body.indexTable },
});
let result = await database.indexes
.create({ name: req.body.name, indexTableId: indextable.id })
.catch((err) => console.error(err));
if (result) {
res.status(200).send({ msg: "Ο δείκτης δημιουργήθηκε επιτυχώς." });
} else {
res.status(500).send({
msg: "Προέκυψε πρόβλημα κατά τη δημιουργία του δείκτη. Παρακαλώ προσπαθήστε ξανά.",
});
}
} catch (err) {
console.error(err);
}
}
);
routes.put(
"/",
authUser,
authRole,
authAgency,
authAdmin,
async (req, res, next) => {
try {
let result = await database.indexes
.update(
{ name: req.body.edit_name },
{ where: { name: req.body.indexSelect } }
)
.catch((err) => console.error(err));
if (result) {
res.status(200).send({ msg: "Ο δείκτης ενημερώθηκε επιτυχώς." });
} else {
res.status(500).send({
msg: "Προέκυψε πρόβλημα κατά την ενημέρωση του δείκτη. Παρακαλώ προσπαθήστε ξανά.",
});
}
} catch (err) {
console.error(err);
}
}
);
routes.delete(
"/",
authUser,
authRole,
authAgency,
authAdmin,
async (req, res, next) => {
try {
let result = await database.indexes
.destroy({ where: { name: req.body.indexSelect } })
.catch((err) => console.error(err));
if (result) {
res.status(200).send({ msg: "Ο δείκτης διαγράφηκε επιτυχώς." });
} else {
res.status(500).send({
msg: "Προέκυψε πρόβλημα κατά τη διαγραφή του δείκτη. Παρακαλώ προσπαθήστε ξανά.",
});
}
} catch (err) {
console.error(err);
}
}
);
module.exports = routes;