-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (61 loc) · 1.92 KB
/
app.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
var ExpSvr = require('express')
var ExpSvrFunc = ExpSvr()
var BdyParser = require('body-parser')
ExpSvrFunc.use(BdyParser.json())
var MdbCncVap = require('mongodb').MongoClient
var MdbUrlVar = "mongodb://localhost:27017"
var NamClnVar
MdbCncVap.connect(MdbUrlVar, (ErrMdbVar, SvrMdbVar) => {
if (ErrMdbVar) throw ErrMdbVar
var NamMdbVar = SvrMdbVar.db("ShoppingCart")
NamClnVar = NamMdbVar.collection("Products")
console.log("Connected Successfully")
})
ExpSvrFunc.listen(8080, function () {
console.log("Server Started and Running")
})
// Read Operation
ExpSvrFunc.get("/cart", function (Req, Res) {
NamClnVar.find({}).toArray((ErrMdbVar, ResMdbVar) => {
if (ErrMdbVar) throw ErrMdbVar
Res.json(ResMdbVar)
})
})
//Create Operation
ExpSvrFunc.post("/cart", function (Req, Res) {
const BdyReqVar = Req.body
var AddItmVar = {
"Uid": BdyReqVar.Uid,
"Name": BdyReqVar.Name,
"Cost": BdyReqVar.Cost
}
NamClnVar.insertOne(AddItmVar, (ErrMdbVar, ResMdbVar) => {
if (ErrMdbVar) throw ErrMdbVar
Res.status(201)
Res.send("Record Added Successfully")
})
})
//Update Operation
ExpSvrFunc.put("/cart/:Uid", function (Req, Res) {
var ReqUidVar = Number(Req.params.Uid)
const BdyReqVar = Req.body
var UpdItmVar = {
"Uid": ReqUidVar,
"Name": BdyReqVar.Name,
"Cost": BdyReqVar.Cost
}
NamClnVar.updateOne({ "Uid": ReqUidVar }, { $set: UpdItmVar }, function (ErrMdbVar, ResMdbVar) {
if (ErrMdbVar) throw ErrMdbVar
Res.status(201)
Res.send("Data Updated Successfully")
})
})
// Delete Operation
ExpSvrFunc.delete("/cart/:Uid", function (Req, Res) {
var ReqUidVar = Number(Req.params.Uid)
NamClnVar.deleteOne({"Uid":ReqUidVar},function(ErrMdbVar,ResMdbVar){
if(ErrMdbVar) throw ErrMdbVar
Res.status(201)
Res.send("One Data Successfully Deleted")
})
})