-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (131 loc) · 5.53 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
const Express = require("express");
const BodyParser = require("body-parser");
const MongoClient = require("mongodb").MongoClient;
const path = require('path');
const autoIncrement = require("mongodb-autoincrement");
const ObjectId = require("mongodb").ObjectID;
const CONNECTION_URL = `mongodb://fidisys-api:!%403asflkj4%23sfslkms%[email protected]:29023/heroku_s9zfc9ft`;
const COLLECTION_NAME = "items";
var app = Express();
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));
var database, collection;
const { response, request } = require("express");
app.listen(process.env.PORT || 5000, () => {
MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => {
if(error) {
throw error;
}
database = client.db("");
collection = database.collection(COLLECTION_NAME);
console.log("Connected!");
});
});
app.get("/api/", (request, response) => {
response.send(`API ENDPOINTS:<br />
- - - - "/items" -- to fetch all items<br />
- - - - "/add" -- to add new item<br />
- - - - "/update" -- to edit price of item with id<br />
- - - - "/delete" -- to delete item<br />
`);
});
app.get("/api/items", (request, response) => {
collection.find({},{projection:{_id:0}}).toArray((error, result) => {
if(error) {
return response.status(500).send(error);
}
response.send(result);
});
});
app.post("/api/add",(request,response)=>{
if(("name" in request.body) && ("price" in request.body) && !isNaN(request.body.price) && request.body.name !== "" && request.body.price !== ""
&& typeof request.body.name === 'string' && typeof request.body.price !== 'string'){
autoIncrement.getNextSequence(database, COLLECTION_NAME, function (err, autoIndex) {
var collection_local = database.collection(COLLECTION_NAME);
if(!err){
collection_local.insertOne({id: autoIndex, ...request.body},(error,result)=>{
if(error){
return response.status(500).send(error);
}
if(result.result.ok === 1){
response.send({status: "success", error: null});
}else{
response.send(result.result);
}
});
}else if(!("name" in req.body) || !("price" in req.body) || req.body.id === "" || req.body.price === ""){
response.send({error: "fields are missing"});
}
else{
response.send({error: "Something is not right!"});
}
});
}else if(!("name" in request.body) || !("price" in request.body) || request.body.name === "" || request.body.price === ""){
response.send({error: "fields are missing"});
}else{
response.send({error: "name and price must be string and number respectively."});
}
});
app.post("/api/update",(req,response)=>{
if(("id" in req.body) && ("price" in req.body) && req.body.id !== "" && req.body.price !== "" && !isNaN(req.body.id) && !isNaN(req.body.price)
&& typeof req.body.id !== 'string' && typeof req.body.price !== 'string'){
let id = parseInt(req.body.id);
collection.findOne({id},(error,result)=>{
console.log(result);
if(error){
response.send({error: "something wrong"});
}
if(result !== null && result.id === id){
collection.updateOne({id},{$set:{price: req.body.price}},(error,result)=>{
if(error){
response.send({error: "Something is not right!"});
}
else if(result){
response.send({status: "success",error: null});
}
});
}else{
response.send({error: "id doesn't exits"});
}
});
}else if(!("id" in req.body) || !("price" in req.body) || req.body.id === "" || req.body.price === ""){
response.send({error: "fields are missing"});
}
else{
response.send({error: "id and price must be number"});
}
});
app.post("/api/delete",(req, response)=>{
if(("id" in req.body) && !isNaN(req.body.id) && req.body.id !== "" && typeof req.body.id !== 'string' ){
let id = parseInt(req.body.id);
collection.findOne({id},(error,result)=>{
console.log(result);
if(error){
response.send({error: "something wrong"});
}
if(result !== null && result.id === id){
collection.deleteOne({id},(error,result)=>{
if(error){
response.send({error: "Something is not right!"});
}else if(result.deletedCount === 1){
response.send({status: "deleted",error: null});
}
});
}else{
response.send({error: "id doesn't exits"});
}
});
}else if(!("id" in req.body) || req.body.id === ""){
response.send({error: "fields are missing"});
}else{
response.send({error: "id must be number"});
}
});
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(Express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}