forked from cmda-minor-web/browser-technologies-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
285 lines (230 loc) · 7.43 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
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
284
285
// **** IMPORT PACKAGES **** //
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;
const bodyParser = require('body-parser');
const Images = require('./modules/models/image.js');
const Series = require('./modules/models/serie');
const path = require('path');
// const mongo = require('mongodb');
const mongoose = require('mongoose');
const multer = require('multer');
const storage = multer.diskStorage({
// This adds a name and extension to the uploaded file
// destination: function (req, file, cb) {
// cb(null, 'static/uploads/'); // location where the uploaded file needs to be stored
// },
destination: 'static/uploads/',
filename: function (req, file, cb) {
// changes file name to filename + date + original extension
cb(
null,
file.fieldname + '-' + Date.now() + path.extname(file.originalname)
);
},
});
const upload = multer({
storage: storage,
});
require('dotenv').config();
// database connection
const db = createConnection();
function createConnection() {
const DB_USER = process.env.DB_USER;
const DB_PASS = process.env.DB_PASS;
const DB_NAME = process.env.DB_NAME;
const URI = `mongodb+srv://${DB_USER}:${DB_PASS}@${DB_NAME}.ssfa5.mongodb.net/${DB_NAME}?retryWrites=true&w=majority`;
mongoose.connect(
URI,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
},
(err) => {
err ? console.log(err) : console.log('MongoDB is connected');
}
);
return mongoose.connection;
}
// **** MIDDLEWARE SET-UP **** //
// Using static files from static directory
app.use(express.static('static'));
app.use(bodyParser.urlencoded({ extended: true }));
// Setting views (EJS)
app.set('views', './views');
app.set('view engine', 'ejs');
// ******** ROUTING ********** //
app.get('/', function (req, res) {
res.render('index.ejs');
});
app.get('/upload', function (req, res) {
res.render('pages/upload');
});
// upload photographs to server
app.post('/upload', upload.single('image'), function (req, res, next) {
const imageObject = {
image: req.file ? req.file.filename : null, // zet alles na de ? uit, dan krijg je een data object. Daar kan je meer mee.
title: req.body.title,
description: req.body.description,
photographer: req.body.photographer,
location: req.body.location,
};
createImage(imageObject);
console.log('uploaded image', imageObject.image);
function createImage({
image,
title,
description,
photographer,
location,
series,
}) {
Images.create({
image: image,
title: title,
description: description,
photographer: photographer,
location: location,
series: series,
});
}
res.redirect('/photos');
});
app.get('/photos', async function (req, res) {
// render photos with id and serach id and give it classname
// last upload
// 1. search ID from last object
// 2. give class
const images = await Images.find().catch((err) => console.log(err));
images.reverse();
if (images) {
res.render('pages/photos/overviewPhotos', { images });
} else {
res.redirect('/error');
}
});
app.get('/photos/:id', async (req, res) => {
const image = await Images.findOne({
_id: req.params.id,
}).catch((err) => console.log(err));
console.log('image data', image);
res.render('pages/photos/detailPhotos', { image });
});
app.post('/photos/:id/edit', async (req, res) => {
const updatedValues = {
title: req.body.title,
description: req.body.description,
photographer: req.body.photographer,
location: req.body.location,
};
await Images.updateOne(
{
_id: req.params.id,
},
updatedValues
).catch((err) => console.log(err));
const image = await Images.findOne({
_id: req.params.id,
}).catch((err) => console.log(err));
res.render('pages/photos/detailPhotos', { image });
});
app.post('/photos/:id/remove', async (req, res) => {
await Images.findByIdAndDelete(req.params.id); // Delete image from Images collection
// Delete image from Series collection (id)
// const series = await Series.find().catch((err) => console.log(err));
// const imageObject = series.images;
// console.log(imageObject);
// Series.forEach((serie) => console.log('serie data', serie));
// await Series.findByIdAndDelete({ _id: { $in: req.params.id } });
// 1. Loop over Series objects (Arr)
// 2. Check in each object if there is in the images key thé ID
// 3. delete that ID findbyIdAndDelete (2+3)
// for loop - kijken of ID erinzit, zoja findby and delete
res.redirect('/photos');
});
app.get('/series/overview', async function (req, res) {
const series = await Series.find().catch((err) => console.log(err));
if (series.length >= 1) {
// Catch first image of every object
// lop over series
const images = await Images.find(series.images);
console.log('serie - images: ', images);
console.log('serie data: ', series);
res.render('pages/series/overviewSeries', {
series,
images,
});
}
res.render('pages/series/overviewSeries', { series });
});
app.get('/series/new', async function (req, res) {
const images = await Images.find().catch((err) => console.log(err));
images.reverse();
res.render('pages/series/newSerie', { images });
});
function titleValidator(value) {
if (value == '') {
console.log(value);
const defaultValue = 'New serie';
return defaultValue;
} else {
return value;
}
}
app.post('/series/new', function (req, res) {
const serieObject = {
titleSerie: titleValidator(req.body.titleSerie),
images: req.body.selectedPhotos,
};
createSerie(serieObject);
function createSerie({ titleSerie, images, imagesNames }) {
Series.create({
titleSerie: titleSerie,
images: images,
imagesNames: imagesNames,
});
}
res.redirect('/series/overview');
});
app.get('/series/detail/:id', async function (req, res) {
const series = await Series.findOne({
_id: req.params.id,
}).catch((err) => console.log(err));
const images = await Images.find({ _id: { $in: series.images } });
console.log('serie data', series);
res.render('pages/series/detailSeries', { series, images });
});
app.post('/series/detail/:id/remove', async function (req, res) {
await Series.findByIdAndDelete(req.params.id);
res.redirect('/series/overview');
});
// Delete image from serie
// app.post('/series/detail/:id/deleteImage', async function (req, res) {
// // 1. Search ID (req.params.id) in Series collection ($in)
// const id = req.params.id;
// const series = await Series.find().catch((err) => console.log(err));
// await Series.findByIdAndDelete({ id: { $in: series.images } });
// // Splice from array
// res.redirect('/series/overview');
// });
app.get('/series/detail/:id/slideshow', async function (req, res) {
const series = await Series.findOne({
_id: req.params.id,
}).catch((err) => console.log(err));
const images = await Images.find({ _id: { $in: series.images } });
console.log('Data image', images);
console.log('Data serie', series);
res.render('pages/series/show/slideshow', { series, images });
});
app.get('/series/detail/:id/carousel', function (req, res) {
res.render('pages/series/carousel');
});
app.get('/error', function (req, res) {
res.render('404');
});
app.get('/offline', function (req, res) {
res.render('offline');
});
app.listen(PORT, () => console.log(`App is running on port ${PORT}`));