-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
124 lines (99 loc) · 2.63 KB
/
server.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
const express = require('express')
const productRoute = require('./routes/productRoute')
//const userRoutes = require('./routes/userRoute')
//const cartRoute = require('./routes/cartRoute')
const vendorRoutes = require('./routes/vendorRoute')
const {
db,
Cart,
Products,
Users,
Vendors
} = require('./db')
var session = null;
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use('/',
express.static(__dirname + '/public')
)
app.post('/users', async (req, res) => {
console.log("users area intiated!!")
try {
console.log("user zone");
const result1 = await Users.create({
name: req.body.name
})
if (req.body.name != null) {
session = req.body.name
}
res.send({ success: true })
} catch (e) {
res.send({ success: false, err: e.message })
}
})
app.use('/vendors',vendorRoutes)
app.use('/products',productRoute)
// app.use('/users',userRoutes)
app.post('/cart', async (req, res) => {
try {
if (session == null) {
throw new Error("User not Logged In!")
}
else {
console.log("cart entered")
// find product already exist in cart
const productCnt = await Cart.count({where : {productId : req.body.productId, userName:session}})
console.log("product count is: "+productCnt)
if (productCnt == 0) {
console.log("product count inner is: "+productCnt)
await Cart.create({
productId : req.body.productId,
productQuantity : 1,
userName : session
})
} else {
console.log("product count else is: "+productCnt)
await Cart.increment('productQuantity',{where : {productId : req.body.productId, userName:session}})
}
// if exist then increament
// else create with quanity=1
res.send({ success: true })
}
} catch (e) {
res.send({ success: false, err: e.message })
}
})
app.get('/mycart', async (req, res) => {
try {
if(session == null)
{
throw new Error("User not Logged In!")
}
else {
// const prods = await Cart.findAll({ where: { userName: session } })
// res.send(prods)
; (async () => {
console.log('cart entered hellaa')
const products = await Cart.findAll({
include: [
{
model: Products,
include: [Vendors]
}
]
}
)
res.send(products)
})();
}
} catch (e) {
res.send({success:false, error:e.message})
}
})
// app.use('/mycart',cartRoute)
const PORT = process.env.PORT || 8811
db.sync()
.then(() => {
app.listen(PORT)
})