-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
93 lines (79 loc) · 2.54 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const _ = require('lodash');
//const repl = require('repl');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.use(express.static('public'));
//const local = repl.start('$ ');
// local.on('exit', () => {
// console.log('exiting repl')
// process.exit();
// })
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
const bookSchema = {
bookImage: String,
bookTitle: String,
bookURL: String,
bookField: String
}
const Books = mongoose.model('Book', bookSchema);
const librarySchema = {
fieldTitle: String,
fieldColor: String,
fieldBooks: [bookSchema]
}
const Fields = mongoose.model('Field', librarySchema);
app.get('/', (req, res) => {
Fields.find({}, (err, foundFields) => {
if (foundFields.length === 0) {
res.render('home', { foundFields: foundFields, selectCreate: 'CREATED', _: _, bookField: 'FIELD' })
} else {
res.render('home', { foundFields: foundFields, selectCreate: 'SELECTED', _: _, bookField: 'FIELD' })
}
})
})
app.post('/', (req, res) => {
const fieldTitle = _.startCase(req.body.fieldTitle);
const field = new Fields({
fieldTitle: fieldTitle,
fieldColor: req.body.fieldColor
})
Fields.findOne({ fieldTitle: fieldTitle }, (err, foundFieldTitle) => {
if (foundFieldTitle) {
res.redirect('/');
} else {
field.save();
res.redirect('/');
}
})
})
app.get('/:fieldTitle', (req, res) => {
const fieldTitle = _.startCase(req.params.fieldTitle);
Fields.find({}, (err, foundFields) => {
res.render('book', { foundFields: foundFields, _: _, fieldTitle: fieldTitle })
})
})
app.post('/addBook', (req, res) => {
let fieldTitle = req.body.fieldTitle
const newBook = new Books({
bookImage: req.body.bookImage,
bookTitle: req.body.bookTitle,
bookURL: req.body.bookURL,
bookField: fieldTitle
})
newBook.save();
Fields.updateOne({ fieldTitle: fieldTitle }, { $push: { fieldBooks: [newBook] } }, { upsert: true, new: true }, (err) => {
if (err) {
res.redirect('/')
} else {
fieldTitle = _.lowerCase(fieldTitle);
res.redirect('/' + fieldTitle);
}
})
})
app.listen(process.env.PORT || 3000, () => {
console.log('Server is live on port 3000');
})