-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
283 lines (254 loc) · 5.67 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//req é requesiçao, res é resposta
//TODO: Criação de novos produtos
const { PrismaClient } = require('@prisma/client')
const dotenv = require('dotenv')
const jwt = require('jsonwebtoken')
const express = require("express")
const bcrypt = require('bcrypt');
const bodyParser = require('body-parser')
const ejs = require('ejs')
const fs = require('fs')
const path = require('path')
const cookieParser = require('cookie-parser')
const saltRounds = 10
const prisma = new PrismaClient()
const app = express()
app.set('view engine', 'ejs')
dotenv.config()
const port = process.env.PORT
app.use(express.json())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser())
app.use(express.static('public'))
let ejsOptions = {delimiter: '?'}
const getUser = async function(req, res, next) {
try {
req.user = jwt.verify(req.cookies.auth, process.env.JWT_SECRET_KEY)
}catch(err) {
console.log(err)
res.send("Not signed in")
return
}
next()
}
app.use('/favorites', getUser)
app.use('/buyProduct', getUser)
app.get("/", async (req,res) => {
const products = await prisma.product.findMany({
include: {
categories: true
}
})
console.log(req.user)
res.render('index', {products: products})
})
app.get("/cart", async (req, res) => {
res.render('cart')
})
app.get("/login", async (req, res) => {
res.render('login')
})
app.get("/signup", async (req, res) => {
res.render('signup')
})
app.get(`/products`, async (req, res) => {
const products = await prisma.product.findMany({
include: {
categories: true
},
})
res.render('products', {products: products})
return
})
app.get(`/products/:name`,async (req, res) => {
const name = req.params.name
const product = await prisma.product.findFirst({
where: {
name: name
},
include: {
favorited_by: true,
categories: true,
}
})
console.log(product.favorited_by.length)
res.render('product', {product: product, favorites: product.favorited_by.length})
})
app.post('/login', async(req, res) => {
console.log("it got here")
const user = await prisma.user.findFirst({
where: {
email: req.body.email
}
})
if(user == null) {
res.json("Usuário não existe")
return
}
const verified = await bcrypt.compare(req.body.password, user.password)
console.log(verified)
if (!verified) {
console.log("You baddie")
res.json("false")
return
}
const jwtSecretKey = process.env.JWT_SECRET_KEY
const data = {
time: Date(),
userEmail: req.body.email,
}
const token = jwt.sign(data, jwtSecretKey)
console.log("You are not baddie")
res.json(token)
return
})
app.get('/categories', async (req, res) => {
const categories = await prisma.category.findMany({})
res.render('categories', {categories: categories})
})
app.get('/categories/:name', async (req, res) => {
const products = await prisma.product.findMany({
where: {
categories: {
some: {
name: req.params.name
}
}
},
include: {
categories: true
}
})
res.render('category', {products: products, category: req.params.name})
})
app.get('/favorites', async (req, res) => {
console.log(req.user.userEmail)
const products = await prisma.product.findMany({
where: {
favorited_by: {
some: {
email: req.user.userEmail
}
}
},
include: {
categories: true
}
})
res.render('favorite', {products: products})
})
app.get('/history', async (req, res) => {
console.log("History")
})
//Post requests
app.post('/user', async (req, res) => {
console.log("Why")
var hash_password;
const already_found_user = await prisma.user.findFirst({
where: {
email: req.body.email,
}
})
if (already_found_user) {
res.json("User already found")
console.log("Hi")
return
}
bcrypt.hash(req.body.password, saltRounds, async function(err, hash) {
console.log(req.body.email, req.body.hash, req.body.password)
const user = await prisma.user.create({
data: {
email: req.body.email,
password: hash,
name: req.body.name
}
})
})
let jwtSecretKey = process.env.JWT_SECRET_KEY
let data = {
time: Date(),
userEmail: req.body.email,
}
const token = jwt.sign(data, jwtSecretKey)
res.json(token)
})
app.post('/product', async (req, res) => { const new_product = await prisma.product.create({ data: {
img_path: req.body.img_path,
name: req.body.name,
price: parseInt(req.body.price),
desc: req.body.desc,
rating: req.body.rating,
}
})
res.send(new_product)
})
app.put('/categoryToProduct', async (req, res) => {
const result = await prisma.product.update({
where: {
name: req.body.name,
},
data: {
categories: {
connect: {
id: parseInt(req.body.category_id)
}
}
},
include: {
categories: true,
}
})
res.send(result)
return
})
app.post('/buyProduct', async (req, res) => {
const user = await prisma.user.findFirst({
where: {
email: req.user.userEmail
}
})
const product = await prisma.product.findFirst({
where: {
name: req.body.product
}
})
const bought_product = await prisma.bought_product.create({
data: {
user_email: req.user.userEmail,
product_name: product.name,
}
})
res.json(bought_product)
return
})
app.put('/favoriteProduct', async (req, res) => {
const auth = req.cookies.auth
const user = jwt.verify(auth, process.env.JWT_SECRET_KEY)
const favorite = await prisma.user.update({
where: {
email: user.userEmail,
},
data: {
favorites: {
connect: {
name: req.body.name
}
}
},
include: {
favorites: true
}
})
res.json(favorite)
})
app.delete('/product', async(req, res) => {
const deleted_product = await prisma.product.delete({
where: {
name: req.body.name
}
})
res.send(deleted_product)
})
const server = app.listen(port, () =>
console.log(`Server ready at http://localhost:${port}`)
)