updated create and destroy routes

This commit is contained in:
notnull 2019-06-23 06:31:17 -04:00 committed by data
parent 80ad0c3261
commit 5f415bb23e
2 changed files with 9 additions and 5 deletions

View File

@ -1,5 +1,5 @@
const router = require('express').Router() const router = require('express').Router()
const { Project } = require('../db/models') const { Project, Task } = require('../db/models')
module.exports = router module.exports = router
/* CREATE */ /* CREATE */
@ -35,7 +35,7 @@ router.get('/:id', async (req, res, next) => {
router.put('/:id', async (req, res, next) => { router.put('/:id', async (req, res, next) => {
try { try {
const project = await Project.findByPk(req.params.id) const project = await Project.findByPk(req.params.id)
await project.update({ name: req.body.name }) await project.update(req.body)
res.json(project) res.json(project)
} catch (err) { } catch (err) {
next(err) next(err)
@ -46,7 +46,12 @@ router.put('/:id', async (req, res, next) => {
router.post('/:id/delete', async (req, res, next) => { router.post('/:id/delete', async (req, res, next) => {
try { try {
const project = await Project.findByPk(req.params.id) const project = await Project.findByPk(req.params.id)
project.destroy() project.destroy({ force: true })
await Task.destroy({
where: {
projectId: req.params.id,
},
})
res.json(project) res.json(project)
} catch (err) { } catch (err) {
next(err) next(err)

View File

@ -4,7 +4,6 @@ module.exports = router
/* CREATE */ /* CREATE */
router.post('/', async (req, res, next) => { router.post('/', async (req, res, next) => {
console.log(req.body)
const project = await Project.findOne({ const project = await Project.findOne({
where: { name: req.body.projectName }, where: { name: req.body.projectName },
}) })
@ -42,7 +41,7 @@ router.get('/:id', async (req, res, next) => {
router.put('/:id', async (req, res, next) => { router.put('/:id', async (req, res, next) => {
try { try {
const task = await Task.findByPk(req.params.id) const task = await Task.findByPk(req.params.id)
await task.update({ name: req.body.name }) await task.update(req.body)
res.json(task) res.json(task)
} catch (err) { } catch (err) {
next(err) next(err)