-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharticles.js
More file actions
35 lines (34 loc) · 1.06 KB
/
articles.js
File metadata and controls
35 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const axios = require('axios');
const { GET_ARTICLES, NYT_URL, CREATE_ARTICLE, GET_ALL_ARTICLES, DELETE_ARTICLE } = require('./constants');
const { Article } = require('./models');
module.exports = {
[GET_ARTICLES](req, res){
axios.get(`${NYT_URL}`)
.then(response => {
console.log('axios response, ', response.data);
res.status(200).json(response.data);
})
.catch((error) => {
res.status(400).json(error);
});
},
[CREATE_ARTICLE](req, res){
Article.create(req.body, function(err, savedArticle){
if (err) return res.status(400).json(err);
res.status(200).json(savedArticle);
})
},
[GET_ALL_ARTICLES](req, res){
Article.find({}, function(err, articles){
if (err) return res.status(400).json(err);
res.status(200).json(articles);
})
},
[DELETE_ARTICLE]: function(req, res){
const { _id } = req.body;
Article.findByIdAndDelete(_id, function(err, deletedArticle){
if (err) return res.status(400).json(err);
res.status(200).json(deletedArticle);
});
}
}