forked from lucaslevin1/wikistack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.js
79 lines (69 loc) · 1.88 KB
/
wiki.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
const express = require('express');
const router = express.Router();
const addPageHTML = require('../views/addPage.js');
const main = require('../views/main.js');
const { db, Page, User } = require('../models/index.js');
const wikiPage = require('../views/wikipage.js')
//sending default layout
router.get('/', async (req, res, next) => {
const pages = await Page.findAll()
// console.log(pages[0].slug)
res.send(main(pages));
})
router.post('/', async (req, res, next) => {
try {
const name = req.body.name;
const email = req.body.email;
const title = req.body.title;
const content = req.body.content;
const status = req.body.status;
//.findOrCreate returns an array with [instance (an obj), true or false, based on there was or not the value in the database]
let [user, wasCreated] = await User.findOrCreate(
{where: {
name: `${name}`,
email: `${email}`
}
})
await user.save();
const page = new Page({
title: title,
content: content,
status: status,
})
await page.save();
page.setAuthor(user);
//to see in terminal the new added post
console.log(`new Input:
postId: ${page.id},
userId: ${user.id},
title: ${page.title},
user: ${user.name},
e-mail: ${user.email},
content: ${page.content},
status: ${page.status},
created: ${page.createdAt}`);
res.redirect(`/wiki/${page.slug}`);
}
catch (error) {
next(error)
}
// res.json(req.body); // to see how the body is returned
})
router.get('/add', (req, res, next) => {
res.send(addPageHTML());
})
router.get('/:slug', async(req, res, next) => {
try {
const page = await Page.findOne({
where: {
slug: req.params.slug
}
});
const user = await page.getAuthor();
const wikiPageHTML = wikiPage(page, user)
res.send(wikiPageHTML)
} catch (error){
next(error)
}
})
module.exports = router;