-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
114 lines (89 loc) · 2.38 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
import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
import fs from 'fs';
import path from 'path';
import connectDB from "./db.js";
import { Users, Wishlists } from './models.js';
const app = express();
// connect database
connectDB();
// setup express middlewares
app.use(express.json());
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
function getProducts() {
const dummyProductDataPath = path.resolve(process.cwd(), 'products.json');
const products = fs.readFileSync(dummyProductDataPath, {
encoding: "utf-8"
});
return JSON.parse(products);
}
// get users
app.get('/', async (_, res) => {
const users = await Users.find().populate('wishlist');
res.json({
users
})
});
// regis new user
app.post('/', async (req, res) => {
const { name, email, password, username } = req.body;
const newUser = new Users({
name,
email,
password,
username
});
const isSaved = await newUser.save();
if (isSaved) {
res.send("Success!");
return;
}
res.send("Error!");
});
// get product dummy
app.get('/products', (_, res) => {
const products = getProducts();
res.json(products);
});
// add product to wishlist
app.post('/products', async (req, res) => {
const { product_id, user_id } = req.body;
if (!product_id || !user_id) {
res.send("Missing required fields");
return;
}
const product = getProducts()?.products.find(({ id }) => id === product_id) ?? null;
if (product) {
let wishProduct = null;
const isProductAlreadyInDB = !!await Wishlists.findOne({ product_id });
if (isProductAlreadyInDB) {
wishProduct = await Wishlists.findOne({ product_id });
} else {
wishProduct = new Wishlists({
product_id: product.id,
product_name: product.name,
categories: product.categories,
unit_price: product.price
});
// save product to wishlist collection
await wishProduct.save();
}
const user = await Users.findById(user_id);
if (!user) {
res.send("User not found!");
return;
}
user.wishlist.push(wishProduct);
user.save();
res.send("Success adding the product to your wishlist!");
return;
}
res.send("Error!");
})
app.listen(8080, () => {
console.log("Server running on port 8080");
})