-
Notifications
You must be signed in to change notification settings - Fork 0
/
renttech.js
160 lines (138 loc) · 5.34 KB
/
renttech.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
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
const uuidv1 = require('uuid/v1');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
next();
});
var port = process.env.PORT || 8080; // set our port
var mongoose = require('mongoose');
mongoose.connect('mongodb://rentech:[email protected]:13956/rentech'); // connect to our database
const mongooseCon = mongoose.connection;
mongooseCon.on('error', console.error);
mongooseCon.once('open', function(){
console.log("Connected to mongod server");
});
//mongoDb Definitions
const userSchema = mongoose.Schema({
uuid: String,
username: String,
fName: String,
lName: String,
email: String,
password: String,
numSales: Number,
buyerRating: Number,
city: String
});
const user = mongoose.model('user', userSchema);
const itemSchema = mongoose.Schema({
uuid: String,
sellerUuid: String,
buyerUuid: String,
status: String,
category: String,
itemName: String,
numItem: Number,
price: Number,
description: String,
location: String
});
const item = mongoose.model('item', itemSchema);
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.post('/login', function(req, res) {
const body = req.body;
if(body.hasOwnProperty('username') && body.hasOwnProperty('password')){
user.findOne({ username: body.username, password: body.password }, function (err, person) {
if(err) return res.json({ message: 'false' });
if(!person) return res.status(401).json({ message: 'false' });
return res.json({ uuid: person.uuid });
})
}else{
res.json({ message: 'false' });
}
});
router.post('/register', function(req, res) {
const body = req.body;
const newUser = new user({
uuid: uuidv1(),
username: body.username,
fName: body.fname,
lName: body.lname,
email: body.email,
password: body.password,
numSales: 0,
buyerRating: 5,
city: body.city
});
newUser.save(function (err, user) {
if(err) return res.json({ message: 'failure' });
return res.status(200).json({ message: 'success' });
})
})
router.post('/createItem', function(req, res) {
const body = req.body;
const newItem = new item({
uuid: uuidv1(),
sellerUuid: body.sellerUuid,
buyerUuid: '',
status: 'available',
category: body.category,
itemName: body.itemName,
numItem: body.numItem,
price: body.price,
description: body.description,
location: body.location
});
newItem.save(function (err, item) {
if(err) return res.status(402).json({ message: 'failure'});
return res.status(200).json({ item });
})
})
router.get('/get', function(req, res) {
item.find({}, function(err, item) {
if(err) return res.json({ message: 'failure' });
return res.status(200).json( item )
})
})
router.get('/get/:sellerUuid', function(req, res) {
item.find({ sellerUuid: req.params.sellerUuid }, function(err, item) {
if(err) return res.json({ message: 'failure' });
if(!item) return res.json({ message: 'failure' });
return res.status(200).json( item )
})
})
router.get('/getInfo/:uuid', function(req, res) {
user.findOne({ uuid: req.params.uuid }, function(err, user) {
if(err) return res.json({ message: 'failure' });
if(!user) return res.json({ message: 'failure' });
return res.status(200).json({ 'username': user.username, 'email': user.email, 'fname': user.fName });
})
})
router.get('/removeListing/:uuid', function(req, res) {
item.findOneAndRemove( { uuid: req.params.uuid }, function(err, item) {
if(err) return res.json({ message: 'failure' });
if(!item) return res.json({ message: 'failure' });
return res.status(200).json({ item });
})
})
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);