-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
117 lines (96 loc) · 2.87 KB
/
app.js
File metadata and controls
117 lines (96 loc) · 2.87 KB
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
const express = require('express')
const upload = require('./utils/multer')
const app = express()
require("dotenv").config();
const productModel = require('./utils/products')
const orderModel = require('./utils/order')
const adminModel = require('./utils/addmin')
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static("public"));
app.get('/', async (req, res) => {
try {
const products = await productModel.find()
// console.log(products);
res.render('index', { products })
} catch (error) {
console.log(error);
}
})
app.get('/api/admin', (req, res) => {
res.render('admin/dashboard')
})
app.get('/api/admin/create-admin', async(req, res) => {
const email = process.env.EMAIL
const password = process.env.PASSWORD
exitUser = await adminModel.findOne({email})
if(exitUser){
res.send("This Email is alrady Admin")
}else{
await adminModel.create({
email,
password
})
res.send('Admin Create')
}
})
app.get('/api/admin/orders', (req, res) => {
res.render('admin/orders')
})
app.get('/api/admin/products', (req, res) => {
res.render('admin/products')
})
app.get('/api/admin/products/add', (req, res) => {
res.render('admin/addproduct')
})
app.post('/add-product', upload.single("productImage"), async (req, res) => {
const { productName, description, price } = req.body
const { filename } = req.file
// console.log(req.file);
try {
const createProduct = await productModel.create({
productName: productName,
description: description,
productIamge: `images\\${filename}`,
price: price
})
res.redirect('/api/admin/product')
} catch (error) {
console.log(error);
}
})
app.get('/product/:productName', async (req, res) => {
try {
const productName = req.params.productName
console.log(productName);
const product = await productModel.findOne({ productName })
// console.log(product);
res.render('product',{product})
} catch (error) {
console.log(error);
}
})
app.get('/checkout', (req, res) => {
res.render('checkout')
})
app.post('/order', async (req, res) => {
const {bill_full_name,bill_address,bill_phone,bill_area,total_price,products,total_quantity} = req.body
try {
const order = await orderModel.create({
productNames:products,
quantity:total_quantity,
totalPrice:total_price,
fullName:bill_full_name,
address:bill_address,
phone:bill_phone,
area:bill_area,
})
res.redirect('/')
} catch (error) {
console.log(error);
}
})
app.listen(4000, () => {
console.log('server is working');
})