-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
193 lines (161 loc) · 4.38 KB
/
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var express = require('express');
var bodyParser = require('body-parser');
var mobiusModule = require('./lib/mobiusClient');
var mongooseModule = require('./lib/mongooseClient');
var appConfig = require('./config/config.json');
var docs = require('express-mongoose-docs');
var router = express.Router();
var mobiusIns = mobiusModule(appConfig.mobius);
var mongooseIns = mongooseModule(appConfig.mongo);
var mongoose = mongooseIns.init();
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
// models
var Carousel = mongoose.model('Carousel', {
key: String,
name: String,
department: String
});
var Department = mongoose.model('Department', {
key: String,
name: String
});
var Offer = mongoose.model('Offer', {
department: String,
carousel: String,
id: String,
name: String,
mainImage: String
});
// cors
router.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
// status
router.get('/status/ping', function(req, res) {
res.send('pong')
})
// carousels
router.get('/carousels/:department', function(req, res) {
Carousel.where({
department: req.params.department
}).find(function (err, elem) {
if (err) return res.send(err);
if (elem) {
var response = {
'collection': req.params.department,
'items': elem
};
res.send(response);
}
});
});
router.post('/carousel', function(req, res) {
var carousel = new Carousel({
key: req.body.key,
name: req.body.name,
department: req.body.department
});
carousel.save(function () {
res.send('created!');
});
});
router.delete('/carousel', function(req, res) {
Carousel.findOneAndRemove({
key: req.body.key,
department: req.body.department
}, function () {
res.send('removed!');
});
});
// departments
router.get('/departments', function(req, res) {
Department.find(function (err, elem) {
if (err) return res.send(err);
if (elem) {
var response = {
'collection': req.params.department,
'items': elem
};
res.send(response);
}
});
});
router.post('/department', function(req, res) {
var department = new Department({
key: req.body.key,
name: req.body.name
});
department.save(function () {
res.send('created!');
});
});
router.delete('/department', function(req, res) {
Department.findOneAndRemove({
key: req.body.key
}, function () {
res.send('removed!');
});
});
// offers
router.get('/offer/:id', function(req, res) {
var offerId = req.params.id;
mobiusIns.getOffer(offerId, function (error, data) {
if (error) {
res.status(404).send();
} else {
res.send(data);
}
});
});
router.get('/offers/:department/:carousel', function(req, res) {
Offer.where({
department: req.params.department,
carousel: req.params.carousel
}).find(function (err, elem) {
if (err) return res.send(err);
if (elem) {
var response = {
'collection': req.params.department,
'items': elem
};
res.send(response);
}
});
});
router.post('/offer/:id', function(req, res) {
var offerId = req.params.id;
mobiusIns.getOffer(offerId, function (error, data) {
if (error) {
res.status(404).send();
} else {
var offer = new Offer({
department: req.body.department,
carousel: req.body.carousel,
id: data.id,
name: data.name,
mainImage: data.mainImage.small
});
offer.save(function () {
res.send('created!');
});
}
});
});
router.delete('/offer/:id', function(req, res) {
Offer.findOneAndRemove({
id: req.params.id,
department: req.body.department,
carousel: req.body.carousel
}, function () {
res.send('removed!');
});
});
app.use('/', router);
docs(app, mongoose);
app.listen(8080, function () {
console.log('server started!');
});