-
Notifications
You must be signed in to change notification settings - Fork 0
/
promoRouter.js
85 lines (81 loc) · 3.39 KB
/
promoRouter.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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const authenticate = require('../authenticate');
const Promotions = require('../models/promotions');
const cors = require('./cors');
const promoRouter = express.Router();
promoRouter.use(bodyParser.json());
promoRouter.route('/')
.options(cors.corsWithOptions, (req, res) => { res.sendStatus(200); })
.get(cors.cors, (req, res, next) => {
Promotions.find({})
.then((promotions) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(promotions);
}, (err) => next(err))
.catch((err) => next(err));
})
.post(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => {
Promotions.create(req.body)
.then((promotion) => {
console.log('promotion Created ', promotion);
res.statusCode = 200;
res.setHeader('Content_Type', 'application/json');
res.json(promotion);
}, (err) =>
next(err))
.catch((err) => next(err));
})
.put(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => {
res.statusCode = 403;
res.setHeader('Content-Type', 'text/plain');
res.end('PUT operation not supported on /promotions');
})
.delete(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => {
Promotions.deleteOne({})
.then((resp) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(resp);
}, (err) => next(err))
.catch((err) => next(err));
});
promoRouter.route('/:promoIds')
.options(cors.corsWithOptions, (req, res) => { res.sendStatus(200); })
.get(cors.cors, (req, res, next) => {
Promotions.findById(req.params.promoIds)
.then((promotion) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(promotion);
}, (err) => next(err))
.catch((err) => next(err));
})
.post(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => {
res.statusCode = 403;
res.setHeader('Content-Type', 'text/plain');
res.end('POST operation not supported on /promotions/' + req.params.promoIds);
})
.put(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => {
Promotions.findByIdAndUpdate(req.params.promoIds, {
$set: req.body
}, { new: true })
.then((promotion) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(promotion);
}, (err) => next(err))
.catch((err) => next(err));
})
.delete(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => {
Promotions.findByIdAndRemove(req.params.promoIds)
.then((resp) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(resp);
}, (err) => next(err))
.catch((err) => next(err));
});
module.exports = promoRouter;