|
|
@ -1,9 +1,20 @@ |
|
|
|
module.exports = { |
|
|
|
module.exports = { |
|
|
|
getPosts: function() { |
|
|
|
getPosts: function(id) { |
|
|
|
|
|
|
|
const BASE_URL = 'http://localhost:5000/api/posts' |
|
|
|
|
|
|
|
const URL = BASE_URL + (id ? `/${id}` : '?sort=-1') |
|
|
|
|
|
|
|
|
|
|
|
this.$http |
|
|
|
this.$http |
|
|
|
.get('http://localhost:5000/api/posts?sort=-1') |
|
|
|
.get(URL) |
|
|
|
.then(res => { |
|
|
|
.then(res => { |
|
|
|
|
|
|
|
if (id) { |
|
|
|
|
|
|
|
if (res.body.length === 0) { |
|
|
|
|
|
|
|
throw Error("Response body empty") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.post = res.body[0] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
this.posts = res.body |
|
|
|
this.posts = res.body |
|
|
|
|
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
.catch(err => { |
|
|
|
console.log(`Error: ${err}`) |
|
|
|
console.log(`Error: ${err}`) |
|
|
|