-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (29 loc) · 896 Bytes
/
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
const router = require('express').Router();
const Item = require('../models/item');
router.get('/', async (req, res, next) => {
const items = await Item.find({});
res.render('index', {items});
});
router.get('/items/create', async (req, res, next) => {
res.render('create');
});
router.post('/items/create', async (req, res, next) => {
const {title, description, imageUrl} = req.body;
const newItem = new Item({title, description, imageUrl});
newItem.validateSync();
if (newItem.errors) {
res.status(400).render('create', {newItem: newItem});
} else {
await newItem.save();
res.redirect('/');
}
});
router.get('/items/:itemId', async (req, res, next) => {
const item = await Item.findById(req.params.itemId)
if (item == undefined) {
res.redirect('/');
} else {
res.status(200).render('single-item', {item: item})
}
});
module.exports = router;