-
Notifications
You must be signed in to change notification settings - Fork 5
/
films.js
59 lines (50 loc) · 1.3 KB
/
films.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
var express = require('express');
var router = express.Router();
const auth = require('./middlewares/auth');
const filmParam = require('./params/film');
const database = require('../lib/db');
// Add auth middleware
router.use(auth);
// GET: List films
router.get('/', (req, res, next) => {
res.render('films/index', {
title: 'Films',
films: database.get('films'),
filmAdded: !!req.query.filmAdded
});
});
// GET: Add new film
router.get('/add', (req, res, next) => {
res.render('films/add', {
title: 'Add film',
film: {}
});
});
// POST: Add new film
router.post('/add', (req, res, next) => {
const film = req.body;
const { name, description, image } = film;
let error = '';
if (!name || !description || !image) {
error = 'Todos los campos son obligatorios';
res.render('films/add', {
title: 'Add film',
film,
error
});
} else {
database.addFilm(film);
res.redirect('/films?filmAdded=true');
}
});
// GET: Show film
router.get('/:film', (req, res, next) => {
const film = req.film;
res.render('films/film', {
title: film.name,
film
});
});
// Parse film ID to append the film object to the request
filmParam(router);
module.exports = router;