1const { Board } = require('../models');
2
3const getSettingsPage = async (req, res, next) => {
4 try {
5
6 const boards = await Board.findAll({
7 attributes: ['id', 'name', 'slug']
8 });
9
10 res.render('settings', { title: 'Settings', boards });
11 return;
12 } catch (e) {
13 console.log(e)
14 next(e)
15 return;
16 }
17}
18
19const getNewBoardPage = (req, res) => {
20 res.render('new_board', { title: 'New Board' });
21}
22
23const createNewBoard = async (req, res, next) => {
24 try {
25 const slug = req.xop.name.trim().toLowerCase().replace(/ /g, '-');
26
27 // check if name is already present
28 const existingBoard = await Board.findAll({
29 where: {
30 slug
31 }
32 })
33
34 if (existingBoard.length) {
35 req.flash('error', ['Board name already in use']);
36 res.render('new_board', { title: 'New Board', body: req.body, flashes: req.flash() });
37 return;
38 }
39
40 const board = await Board.create({
41 name: req.xop.name,
42 description: req.xop.description,
43 slug
44 });
45 res.redirect('/settings');
46 return;
47 } catch (e) {
48 console.log(e)
49 next(e);
50 return;
51 }
52}
53
54module.exports = {
55 getSettingsPage,
56 getNewBoardPage,
57 createNewBoard
58}