This repository was archived by the owner on Aug 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticle.service.js
70 lines (65 loc) · 1.61 KB
/
article.service.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import $ from 'jquery'
/**
* Get all articles from API
* @return {Promise<any>}
*/
export function listArticles () {
return new Promise((resolve, reject) => {
$.getJSON('/api/articles')
.done((data) => resolve(data))
.fail((error) => reject(error))
})
}
/**
* Get an article from API
* @param articleId {int} Article ID
* @return {Promise<any>}
*/
export function getArticle (articleId) {
return new Promise((resolve, reject) => {
$.getJSON(`/api/articles/${articleId}`)
.done((data) => resolve(data))
.fail((error) => reject(error))
})
}
/**
* Update title for an article
* @param articleId {int} Article ID
* @param title {string} New title
* @return {Promise<any>}
*/
export function setArticleTitle (articleId, title) {
return new Promise((resolve, reject) => {
$.ajax(`/api/articles/${articleId}`, {
method: 'PATCH',
contentType: 'application/json',
data: JSON.stringify({
title: title
})
})
.done(data => resolve(data))
.fail(error => reject(error))
})
}
export function createArticle (title = 'New Article') {
return new Promise((resolve, reject) => {
$.ajax(`/api/articles`, {
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({
title: title
})
})
.done(data => resolve(data))
.fail(error => reject(error))
})
}
export function removeArticle (articleId) {
return new Promise((resolve, reject) => {
$.ajax(`/api/articles/${articleId}`, {
method: 'DELETE'
})
.done(data => resolve(data))
.fail(error => reject(error))
})
}