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 { Project } = require('../db/models')
const { Project, Task } = require('../db/models')
module.exports = router
/* CREATE */
@ -35,7 +35,7 @@ router.get('/:id', async (req, res, next) => {
router.put('/:id', async (req, res, next) => {
try {
const project = await Project.findByPk(req.params.id)
await project.update({ name: req.body.name })
await project.update(req.body)
res.json(project)
} catch (err) {
next(err)
@ -46,7 +46,12 @@ router.put('/:id', async (req, res, next) => {
router.post('/:id/delete', async (req, res, next) => {
try {
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)
} catch (err) {
next(err)

View File

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