-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (147 loc) · 5.25 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
console.log('hii');
// imports
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
const dotenv = require('dotenv').config();
const port = 3000;
const { MongoClient, ObjectID } = require('mongodb');
const ejs = require('ejs');
// verbinden met de mongo database
let db = null;
// function connectDB
async function connectDB() {
// get URI from .env file
const uri = process.env.DATABASECONNECT;
// make connection to database
const options = { useUnifiedTopology: true };
const client = new MongoClient(uri, options);
await client.connect();
db = await client.db(process.env.DATABASENAME);
}
// een 'test' om te achterhalen of de database is verbonden
connectDB()
.then(() => {
// if succesfull connections is made, show a message
console.log('We have a connection to Mongo!');
})
.catch((error) => {
// if connnection is unsuccesful, show errors
console.log(error);
});
// dit is waar de applicatie zich bevindt, hij 'luistert op port 3000, daar wordt de app weergegeven
// Listen on port
app.listen(3000, () => {
console.log('Express web app on localhost:3000');
});
// Static files
app.use(express.static('public'));
app.use(express.static('public/images'));
app.use(express.static('public/js'));
app.use('/css', express.static('/public/css')); // link naar je css folder
app.use('/js', express.static('/public/js')); // link naar je js folder
app.use('/images', express.static('public/images')); // link naar je images folder
app.use(bodyParser.urlencoded({ extended: false }));
// (EJS)Display je html op je localhost (omdat de index in de root van ons document zit,
// hoef je het niet te specificeren in de app.get('',))
app.get('', (req, res) => {
res.render('index', { text: 'Find Friends' });
});
app.get('', (req, res) => {
res.render('index', { text: 'Help finding outfits for occassions' });
});
app.get('/menu', (req, res) => {
res.render('menu', { text: ' ' });
});
//laad kledingitems uit database wanneer we via get route maketheoutfit aanvragen.
app.get('/maketheoutfit', async (req, res) => {
let maketheoutfit = {}; // data vanuit de database
maketheoutfit = await db.collection('clothes').find({}, { sort: {} }).toArray();
res.render('maketheoutfit', { text: 'What To Wear', maketheoutfit });
});
//filteren op een bepaald kledingitem
app.post('/maketheoutfit', async (req, res) => {
const clothes = await db.collection('clothes').find({}).toArray();
const clothingitems = clothes.filter((clothes) => clothes.merk.toLowerCase() === req.body.merken.toLowerCase() && clothes.categories.includes(req.body.categories.toLowerCase()));
console.log(clothingitems);
console.log(clothes);
console.log(req.body.merken);
console.log(req.body.categories);
res.render('maketheoutfitresults', { title: 'Results' , maketheoutfit: clothingitems });
});
//pagina om gekozen producten op te slaan
app.get('/maketheoutfit/chosenclothing', async (req, res) => {
const clothes = await db.collection('clothes');
const savedItems = await db.collection('savedItems');
const objectID = new ObjectID('604a14c5aabbc00b883fdc28');
savedItems.findOne({ _id: objectID }, (err, savedItemsObject) => {
if (err) {
console.log(err);
} else {
clothes
.find({ _id: { $in: savedItemsObject.saves } })
.toArray((err, savedClothing) => {
if (err) {
console.log(err);
} else {
res.render('chosenclothing', {
title: 'Saved Items',
savedClothing,
});
}
});
}
});
});
//aangeklikte kledingitems opslaan op de database om dan weer te geven op de chosenclothing pagina
app.post('/maketheoutfit/chosenclothing', async (req, res) => {
const clothes = await db.collection('clothes');
const savedItems = await db.collection('savedItems');
const objectID = new ObjectID('604a14c5aabbc00b883fdc28');
const savedItem = new ObjectID(req.body.saveit);
await savedItems.update(
{ _id: objectID },
{ $push: { saves: savedItem } },
);
//controleren
savedItems.findOne({ _id: objectID }, (err, savedItemsObject) => { // object id die nu in saveditems staat controleren
if (err) {
console.log(err);
} else {
clothes
.find({ _id: { $in: savedItemsObject.saves } })
.toArray((err, savedClothing) => {
if (err) {
console.log(err);
} else {
res.render('chosenclothing', {
title: 'Saved Items',
savedClothing,
});
}
});
}
});
});
// detailpagina kleding
app.get('/maketheoutfit/:clothesId', async (req, res) => {
const clothes = await db.collection('clothes').findOne({ id: req.params.clothesId });
res.render('clothingdetails', { title: 'Clothing Details', clothes });
});
//----------------------------------------------
// formuliertje
app.get('/outfithelprequest', (req, res) => {
res.render('outfithelprequest', { title: 'Hi' });
});
app.post('/outfithelprequest', (req, res) => {
res.render('sendOutfitrequest', { title: 'Request has been sent!' });
});
// Set Views
app.set('views', './views');
app.set('view engine', 'ejs');
// routing
app.use((req, res, next) => {
res.status(404).send('404 Not Found');
});
//
app.set('view engine', 'ejs');