const {Router} = require('express')
const Good = require('../models/good')
const router = Router()
router.get('/', async (req, res) => {
const goods = await Good.find()
res.render(goods', {
title: Товары',
goods
})
})
router.get('/:id/edit',async(req,res)=>{
if(!req.query.allow){
return res.redirect('/')
}
const goods=await Good.findById(req.params.id)
res.render('good-edit',{
title: "Редактировать" ,
goods
})
})
router.post('/edit',async(req,res)=>{
const {id}=req.body.id
delete req.body.id
await Good.findByIdAndUpdate(id,req.body)
res.redirect('/goods')
})
router.get('/:id', async (req, res) => {
const good = await Good.findById(req.params.id)
res.render('good', {
layout: 'main',
title: `Товар ${course.title}`,
good
})
})
router.post('/remove',async(req,res)=>{
try{
await Good.deleteOne({_id:req.body.id})
res.redirect('/goods')}
catch (e) {
console.log(e)
}
})
module.exports = router