-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
195 lines (181 loc) · 7.46 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
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
console.log('Server is here')
if (process.env.NODE_DEV !== 'production'){
const dotenv = require('dotenv')
dotenv.config({path: 'proc.env'}) //add env path
}
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient
MongoClient.connect(process.env.DATABASE_URL, {
useUnifiedTopology: true
})
.then(client => {
console.log('connected to database')
const db = client.db('bunch-of-things') //database name
const thingGroup = db.collection('things') //collection name
// express to find and serve public directory contents
app.use(express.static('public'))
// set engine ahead of everything else
app.set('view engine', 'ejs')
// 'middleware'
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.get ('/', (req,res)=>{
// res.send('HELLO!')
thingGroup.find().toArray()
.then(results => {
res.render('index.ejs', { stuff: results })
})
console.log('we sent a thing')
})
// filter by tag
app.get ('/tags/:keyword', (req,res)=>{
const tag = req.params.keyword
thingGroup.find({ tags: tag }).toArray()
.then(results => {
res.render('index.ejs', { stuff: results })
})
})
// search, not filter
app.get ('/search', (req,res)=>{
const term = req.query.term
thingGroup.find({ title: { $regex:term, $options: 'i'}}).toArray()
.then(results => {
res.render('index.ejs', { stuff: results })
})
})
app.post('/addPic', (req,res)=>{
if (req.body.passcode === process.env.PASSCODE){
const newTags = req.body.tags.split(', ')
thingGroup.insertOne({
title: req.body.title,
imgURL: req.body.imgURL,
vidURL: req.body.vidURL,
caption: req.body.caption,
tags: [...newTags]
})
.then(result => {
// console.log(result)
res.redirect('/') //you don't want to reload persistently at /thing either - it resubs
})
}else {
res.json('Passcode incorrect.')
}
})
app.post('/find', (req,res)=>{
if (req.body.passcode === '2554'){
const newTags = req.body.tags.split(', ')
thingGroup.insertOne({
title: req.body.title,
imgURL: req.body.imgURL,
vidURL: req.body.vidURL,
caption: req.body.caption,
tags: [...newTags]
})
.then(result => {
// console.log(result)
res.redirect('/') //you don't want to reload persistently at /thing either - it resubs
})
}else {
res.json('Passcode incorrect.')
}
})
app.put('/update', (req,res)=>{
if (req.body.passcode === process.env.PASSCODE){
if (req.body.field === 'Title'){
thingGroup.updateOne({title: req.body.title}, {
$set: {
'title': req.body.edit
}
},{
sort: {_id: -1}, //not sure this does anything for me - is it listing from most recent entry to oldest?
upsert: false //don't add if doesn't exist
})
.then(result => {
console.log('Updated a title')
res.json('Updated a title')
})
.catch(error => console.error(error))
}else if (req.body.field === 'Tags'){
const newTags = req.body.edit.split(', ')
thingGroup.updateOne({title: req.body.title}, {
$addToSet: { 'tags': [...newTags] }
},{
sort: {_id: -1}, //not sure this does anything for me - is it listing from most recent entry to oldest?
upsert: false //don't add if doesn't exist based on title spec
})
.then(result => {
console.log('Updated a title')
res.json('Updated a title')
})
.catch(error => console.error(error))
}else if (req.body.field === 'Asset URL'){
if (req.body.urlType === 'video'){
thingGroup.updateOne({title: req.body.title}, {
$set: {
'vidURL': req.body.edit,
}
},{
sort: {_id: -1},
upsert: false
})
.then(result => {
console.log('Updated vid url')
res.json('Updated video url')
})
.catch(error => console.error(error))
} else {
thingGroup.updateOne({title: req.body.title}, {
$set: {
'imgURL': req.body.edit,
}
},{
sort: {_id: -1},
upsert: false
})
.then(result => {
console.log('Updated img url')
res.json('Updated img url')
})
.catch(error => console.error(error))
}
}else if (req.body.field === 'Caption'){
thingGroup.updateOne({title: req.body.title}, {
$set: {
'caption': req.body.edit
}
},{
sort: {_id: -1},
upsert: false
})
.then(result => {
console.log('Updated a caption')
// response.json('Updated a caption')
})
.catch(error => console.error(error))
}else {
res.end() //can't do anything
}
res.redirect('/') //refresh to show change
}else {
res.json('Passcode incorrect.')
// res.end() //don't do anything if not valid
}
})
app.delete('/deletePic', (req,res)=>{
if (req.body.passcode === process.env.PASSCODE){
thingGroup.deleteOne({title:req.body.title})
.then(result => {
res.json('Bye forever')
})
.catch(err=> console.log(err))
}else {
res.json('Passcode incorrect.')
// res.end() //don't do anything if not valid
}
})
app.listen(process.env.PORT || 3300, ()=> {
console.log('Listening on 3300 or her port')
})
})
.catch(err => console.log(err))