tasks-backend/api/comments.js
2019-07-09 20:33:42 +02:00

35 lines
698 B
JavaScript

const router = require('express').Router()
const { Comment, User, Vote } = require('../db/models')
module.exports = router
router.get('/', async (req, res, next) => {
try {
const comments = await Comment.findAll({
attributes: ['id', 'text']})
res.send(comments)
} catch (err) {
next(err)
}
})
router.get('/:id', async (req, res, next) => {
try {
const comment = await Comment.findByPk(req.params.id, {
attributes: ['id', 'text']})
res.json(comment)
} catch (err) {
next(err)
}
})
router.post('/', async (req, res, next) => {
try {
const comment = await Comment.create(req.body)
res.json(comment)
} catch (err) {
next(err)
}
})