-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
69 lines (54 loc) · 1.3 KB
/
test.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
const express = require('express')
const db = require('../db')
const router = express.Router()
router.get('/', (req, res) => {
db.getStoryNames()
.then(displayStoryName)
.catch(displayErrors)
function displayStoryName (info) {
// console.log(info)
res.render('index', {info})
}
function displayErrors (err) {
res.status(500).send(err.message)
}
})
// update exisiting story
router.get('/form/:id', (req, res) => {
console.log(req.params.id)
const id = Number(req.params.id)
db.updateStory(id)
.then(formatData)
.then(loadUpdatePage)
.catch(displayErrors)
function formatData(data) {
const info = {
name: data[0].name,
sentence: data[0].sentence
}
return info
}
function loadUpdatePage (info) {
res.render('form', info)
}
function displayErrors (err) {
res.status(500).send(err.message)
}
})
// post new sentence and load story page
router.post('/form/submit', (req, res) => {
const sentence = req.body.sentence
db.addNewSentence(sentence)
db.getStory()
//.then(console.log)
.then(postStory)
.catch(displayErrors)
function postStory (data) {
console.log(data)
res.render('story', {data, sentence})
}
function displayErrors (err) {
res.status(500).send(err.message)
}
})
module.exports = router