-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
158 lines (138 loc) · 4.94 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
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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
const sqlite3 = require('sqlite3').verbose();
const app = express();
const port = 3000;
const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function(req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
const upload = multer({
storage: storage,
fileFilter: function (req, file, cb) {
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
return cb(new Error('Only image files are allowed!'), false);
}
cb(null, true);
}
});
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
// Connect to SQLite database
const db = new sqlite3.Database('digidrobe.db', (err) => {
if (err) {
console.error('Error connecting to database:', err.message);
} else {
console.log('Connected to the digidrobe database.');
}
});
// Create table for items if it doesn't exist
db.serialize(() => {
db.run("CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, filename TEXT, displayname TEXT, colourway TEXT, brand TEXT, type TEXT)");
});
// Sample data (replace with actual data from database)
let items = [];
// Load items from database
db.all("SELECT * FROM items", (err, rows) => {
if (err) {
console.error('Error fetching items from database:', err.message);
} else {
items = rows;
}
});
app.get('/', (req, res) => {
res.render('index', { items });
});
app.get('/fitter', (req, res) => {
res.render('fitter');
});
app.get('/random-image/:type', (req, res) => {
const type = req.params.type;
// Filter items by type and get a random item
const filteredItems = items.filter(item => item.type === type);
const randomItem = filteredItems[Math.floor(Math.random() * filteredItems.length)];
if (randomItem) {
res.json({ imageUrl: `/uploads/${randomItem.filename}` });
} else {
res.json({ imageUrl: null });
}
});
app.post('/upload', upload.single('image'), (req, res) => {
const filename = req.file.filename;
const originalname = req.file.originalname;
const displayname = originalname.substring(0, originalname.lastIndexOf('.')); // Remove file extension
db.run("INSERT INTO items (filename, displayname) VALUES (?, ?)", [filename, displayname], (err) => {
if (err) {
console.error('Error inserting item into database:', err.message);
return res.status(500).send('Error uploading item');
}
// Reload items from the database
db.all("SELECT * FROM items", (err, rows) => {
if (err) {
console.error('Error fetching items from database:', err.message);
return res.status(500).send('Error fetching items');
}
items = rows;
res.redirect('/');
});
});
});
app.get('/delete/:id', (req, res) => {
const id = parseInt(req.params.id);
const index = items.findIndex(item => item.id === id);
if (index !== -1) {
const filename = items[index].filename;
fs.unlinkSync(`./public/uploads/${filename}`);
items.splice(index, 1);
// Delete item from database
db.run("DELETE FROM items WHERE id = ?", [id], (err) => {
if (err) {
console.error('Error deleting item from database:', err.message);
}
res.redirect('/');
});
} else {
res.redirect('/');
}
});
app.get('/item/:id', (req, res) => {
const id = parseInt(req.params.id);
const item = items.find(item => item.id === id);
if (!item) {
return res.status(404).send('Item not found');
}
res.render('item', { itemId: id, item, imageUrl: `/uploads/${item.filename}` }); // Pass the image URL to the template
});
app.get('/', (req, res) => {
console.log(items); // Log the items array
res.render('index', { items });
});
app.post('/set-info/:id', (req, res) => {
const id = parseInt(req.params.id);
const { brand, colourway, displayname, type } = req.body;
const item = items.find(item => item.id === id);
if (!item) {
return res.status(404).send('Item not found');
}
// Update item in database
db.run("UPDATE items SET brand = ?, type = ?, colourway = ?, displayname = ? WHERE id = ?", [brand, type, colourway, displayname, id], (err) => {
if (err) {
console.error('Error updating item in database:', err.message);
return res.status(500).send('Error updating item');
}
item.colourway = colourway;
item.displayname = displayname;
item.brand = brand;
item.type = type;
res.redirect('/');
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});