Skip to content

Commit

Permalink
Merge 97b99f0 into a8ca0fb
Browse files Browse the repository at this point in the history
  • Loading branch information
sojida committed Apr 5, 2019
2 parents a8ca0fb + 97b99f0 commit 77fad5f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
30 changes: 30 additions & 0 deletions server/controllers/article.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ const controller = {
});
}
},
async deleteArticle(req, res) {
try {
if (!validations.verifyUUID(req.params.id)) {
return res.status(400).json({
error: 'id not valid',
});
}

const article = await Article.destroy({
where: {
id: req.params.id,
},
});

if (!article) {
return res.status(404).json({
error: 'Article not found',
});
}

return res.status(200).json({
message: 'Article Deleted Successfully',
});
} catch (error) {
return res.status(500).json({
error,
message: 'Oops! There seem to be a database error',
});
}
},
};

export default controller;
1 change: 1 addition & 0 deletions server/routes/article.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const { articleController } = controllers;
const router = express.Router();

router.get('/article/:id', articleController.getOneArticle);
router.delete('/article/:id', articleController.deleteArticle);

export default router;
15 changes: 15 additions & 0 deletions server/seeders/20190331211246-articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,20 @@ module.exports = {
image_url: 'https://picsum.photos/200/300',
is_reported: false,
},
{
id: 'fb3def47-153c-40bd-8161-a1c787e083d6',
user_id: '57c515a1-890d-412f-8ca1-0a5395123dca',
title: 'Article That I will soon delete',
slug: 'soon-to-delete',
abstract:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis metus sed turpis pulvinar tristique. Ut pulvinar maximus nulla in fermentum. Vivamus eu arcu suscipit, tempor nisi sed, auctor arcu.',
body:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis metus sed turpis pulvinar tristique. Ut pulvinar maximus nulla in fermentum. Vivamus eu arcu suscipit, tempor nisi sed, auctor arcu. Nulla gravida lacinia risus a vehicula. Nulla est arcu, auctor sit amet condimentum non, porta tristique nisl. In scelerisque sem nec feugiat mollis. Morbi id bibendum est. Duis in fermentum mi, sed lobortis dolor. Nam imperdiet orci posuere arcu semper lobortis. Nulla pellentesque sem sed pharetra blandit. Maecenas suscipit lacinia ornare.',
category: 'Computer Science',
bookmark_count: 23,
likes_count: 3244,
image_url: 'https://picsum.photos/200/300',
is_reported: false,
},
]),
};
22 changes: 22 additions & 0 deletions tests/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,26 @@ describe('ARTICLE', () => {
done();
});
});

it('should respond with article not found', done => {
chai
.request(app)
.delete(`/api/v1/article/4139d3af-b8b4-44f6-a49f-9305791710f7`)
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body.error).to.equal('Article not found');
done();
});
});

it('should respond with success: article deleted', done => {
chai
.request(app)
.delete(`/api/v1/article/fb3def47-153c-40bd-8161-a1c787e083d6`)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.message).to.equal('Article Deleted Successfully');
done();
});
});
});

0 comments on commit 77fad5f

Please sign in to comment.