-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (152 loc) · 4.95 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
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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");
require('dotenv').config()
const app = express();
app.use(cors());
mongoose.connect(process.env.mongodbString);
var db = mongoose.connection;
db.on('error', console.log.bind(console, "connection error"));
db.once('open', function (callback) { console.log("connection succeeded"); })
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.json());
//SIGNUP
app.get('/sign_up/:name/:email/:password/:personalContent', function (req, res) {
var name = req.params.name;
var email = req.params.email;
var password = req.params.password;
var personalContent = req.params.personalContent;
var data = {
name, email, password, personalContent
}
db.collection('postify_logon_collection').insertOne(data, function (err, collection) {
if (err) throw err;
console.log("Record inserted Successfully : " + collection);
});
})
//VIEW-SIGNUP-DATA
app.get('/checkAvailibity/:id', async function (req, res) {
var answer = await db.collection('postify_logon_collection').findOne({ email: req.params['id'] }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
if(answer) {res.send(answer);}
else {res.send({email: 'No', description: 'No Such Email Address Present In The Database!'});}
res.end();
})
//SIGNIN
app.get('/sign_in/:email/:password', async function (req, res) {
var answer = await db.collection('postify_logon_collection').findOne({ email: req.params['email'], password: req.params['password'] }, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
if (answer) {
res.json({ auth: 'success' });
}
else {
res.json({auth: 'failure'});
}
res.end();
})
//POSTING-DATA
app.get('/addpost/:newid/:user/:post/:dateandtime/:personalcontent', function (req, res) {
var newid = req.params.newid;
var user = req.params.user;
var post = req.params.post;
var dateandtime = req.params.dateandtime;
var personalcontent = req.params.personalcontent;
var data = {
newid, user, post, dateandtime, personalcontent
}
db.collection('postify_post_collection').insertOne(data, function (err, collection) {
if (err) throw err;
console.log("Record inserted Successfully : " + collection);
});
res.end();
})
//VIEWING-DATA
app.get('/viewpost/:email/:limit', async function (req, res) {
var answer = await db.collection('postify_post_collection').find({ user: req.params['email']}, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
}).sort({_id:-1}).limit(parseInt(req.params['limit'])).toArray();
if (answer) {
res.send(answer);
}
res.end();
})
//VIEWING-ALL-POST
app.get('/viewallpost/:limit', async function (req, res) {
var answer = await db.collection('postify_post_collection').find({}, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
}).sort({_id:-1}).limit(parseInt(req.params['limit'])).toArray();
if (answer) {
res.send(answer);
}
res.end();
})
//HOW-MANY-POSTS
app.get('/howmanyposts/:id', async function (req, res) {
var answer = await db.collection('postify_post_collection').countDocuments({ user: req.params['id']}, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
if (answer) {
res.json({posts: answer})
}
else {
res.json({posts: 0});
}
res.end();
})
//NUMBER-OF-ALL-POSTS
app.get('/numberofallposts/', async function (req, res) {
var answer = await db.collection('postify_post_collection').countDocuments({}, function (err, collection) {
if (err) throw err;
console.log(collection)
return collection;
});
if (answer) {
res.json({posts: answer})
}
else {
res.json({posts: 0});
}
res.end();
})
//DELETING-POST
app.get('/deletepost/:id', async function(req, res) {
var answer = await db.collection('postify_post_collection').deleteOne({newid: req.params['id']},function(err, collection){
if (err) throw err;
console.log(collection)
return collection;
});
res.send(answer);
res.end();
})
//DELETING-ACCOUNT
app.get('/delete/:id', async function(req, res) {
var answer = await db.collection('postify_logon_collection').deleteOne({email: req.params['id']},function(err, collection){
if (err) throw err;
console.log(collection)
return collection;
});
res.send(answer);
var answerOne = await db.collection('postify_post_collection').deleteMany({user: req.params['id']},function(err, collection){
if (err) throw err;
console.log(collection)
return collection;
});
res.end();
})
app.listen(process.env.PORT || 3000);