-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin-subcategory-app.js
117 lines (104 loc) · 3.08 KB
/
admin-subcategory-app.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
const express = require("express");
const router = express.Router();
const Subcategory = require("./models/subcategory");
const Category = require("./models/category");
router.route("/")
.get((req, res) => {
Subcategory.findAll((error, subcategories) => {
let params = {};
if(req.session.alert){
params.alert = req.session.alert;
req.session.alert = null;
}
if(req.session.success){
params.success = req.session.success;
req.session.success = null;
}
if(error){
console.log("error: "+error);
res.send("Ha ocurrido un error. Contacte al programador.");
}else if(subcategories){
params.subcategories = subcategories
res.render("admin-subcategory-index", params);
}else{
res.render("admin-subcategory-index", params);
}
})
});
router.route("/new")
.get((req, res) => {
Category.findAll((error, categories) => {
if(categories && categories.length){
let params = {
categories: categories
};
if(req.session.alert){
params.alert = req.session.alert;
req.session.alert = null;
}
res.render("admin-subcategory-new", params);
}else{
req.session.alert = "Primero debe crear las categorías.";
res.redirect("./");
}
})
})
.post((req, res) => {
Subcategory.add(req.session.admin, req.body, req.files.pic, (success, status, body) => {
if(success){
req.session.success = body.message;
res.redirect("./");
}else{
req.session.alert = body.message;
res.redirect("./new");
}
})
})
router.route("/:id/edit")
.get((req, res) => {
Subcategory.findById(req.params.id).exec((error, subcategory) => {
Category.findAll((error, categories) => {
if(categories && categories.length){
if(subcategory){
let params = {
subcategory: subcategory,
categories: categories
};
if(req.session.alert){
params.alert = req.session.alert;
req.session.alert = null;
}
res.render("admin-subcategory-edit", params);
}else{
req.session.alert = "Subactegoría no encontrada.";
res.redirect("../");
}
}else{
req.session.alert = "Primero debe crear las categorías.";
res.redirect("./");
}
})
})
})
.post((req, res) => {
Subcategory.modify(req.session.admin, req.params.id, req.body, req.files.pic, (success, status, body) => {
if(success){
req.session.success = body.message;
res.redirect("../");
}else{
req.session.alert = body.message;
res.redirect("./edit");
}
})
})
router.get("/:id/delete", (req, res) => {
Subcategory.del(req.params.id, (success, status, body) => {
if(success){
req.session.success = body.message;
}else{
req.session.alert = body.message;
}
res.redirect("../");
})
})
module.exports = router;