-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (117 loc) · 4.3 KB
/
index.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
var express = require('express');
var router = express.Router();
var JWT= require('jsonwebtoken');
var mongoose= require('mongoose');
const UserModel = require('../models/Usermodel');
const ItemModel = require('../models/Itemmodel');
const CartModel = require('../models/Cartmodel');
const fileManager = require('../config/fileManager');
const authentication= require('../config/authentication');
const connect = mongoose.connect("mongodb://localhost:27017/anskushapp",{ useNewUrlParser: true }, {autoIndex: false});
// connecting to the database
connect.then((db) => {
console.log("Connected to the MongoDB server\n\n");
}, (err) => { console.log(err); });
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
//UserModel Endpoints
//Signup
router.post('/user/signup', async function(req, res, next) {
var item = {
Firstname: req.body.Firstname,
Lastname: req.body.Lastname,
Username: req.body.Username,
EmailAddress: req.body.EmailAddress,
Password: req.body.Password
}
var data= new UserModel(item);
await data.save();
res.status(200).json({success: true, message:'Successfully Signup !' });
});
//Login
router.post('/user/login', async function(req, res, next) {
if(!req.body.EmailAddress || !req.body.Password){
return res.status(400).json({success: false,message:"Provides all parameters i.e. email and password"});
}
let result = await UserModel.findOne({EmailAddress : req.body.EmailAddress});
if(!result){
return res.status(409).json({success: false,message:"Invalid username or password"});
}
console.log(result);
const token=JWT.sign(req.body.EmailAddress,'my_secret_key');
res.status(200).json({success: true, message:'Successfully Login !',token:token });
});
//Logout
//ItemModel Endpoints
//Get All items
router.get('/item/get', authentication.ensuretoken, function(req, res, next) {
ItemModel.find()
.then(function(doc){
res.status(200).json({success: true, data:doc });
});
});
//post endpoint
router.post('/item/post',authentication.ensuretoken,fileManager.ItemImage.single('Image'), async function(req, res, next) {
var item = {
Image: req.file.path,
Title: req.body.Title,
Specifications: req.body.Specifications,
Description: req.body.Description,
Price: req.body.Price
}
var data= new ItemModel(item);
await data.save();
res.status(200).json({success: true, message :data });
});
//update endpoint
router.put('/item/update',authentication.ensuretoken,fileManager.ItemImage.single('Image'),async function(req, res, next) {
var id = req.body.id;
var result = await ItemModel.findById(id);
await fileManager.DeleteFile( result.Image );
result.Image=req.file.path;
result.Title=req.body.Title;
result.Specifications=req.body.Specifications;
result.Description=req.body.Description;
result.Price=req.body.Price;
await result.save();
res.status(200).json({success: true, message :result });
});
//delete endpoint
router.delete('/item/delete', authentication.ensuretoken,async function(req, res, next) {
var id = req.body.id;
var result = await ItemModel.findById(id);
await fileManager.DeleteFile( result.Image );
var result = await ItemModel.findByIdAndDelete(id);
res.status(200).json({success: true, message :result });
});
//ItemModel Endpoints
//Get All Carts of specific customer
router.post('/cart/get', authentication.ensuretoken,function(req, res, next) {
CartModel.find({EmailAddress : req.body.EmailAddress})
.then(function(doc){
res.status(200).json({success: true, data:doc });
});
});
//post endpoint
router.post('/cart/post',authentication.ensuretoken, fileManager.CartImage.single('Image'),async function(req, res, next) {
var item = {
Image: req.file.path,
Title: req.body.Title,
Price: req.body.Price,
EmailAddress: req.body.EmailAddress
}
var data= new CartModel(item);
await data.save();
res.status(200).json({success: true, message :data });
});
//delete endpoint
router.delete('/cart/delete',authentication.ensuretoken, async function(req, res, next) {
var id = req.body.id;
var result = await CartModel.findById(id);
await fileManager.DeleteFile( result.Image );
var result = await CartModel.findByIdAndDelete(id);
res.status(200).json({success: true, message :result });
});
module.exports = router;