programa.service.js
3.7 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(function() {
'use strict';
angular
.module('dialoga')
.factory('ProgramaService', ProgramaService);
/** @ngInject */
function ProgramaService($http, $q, private_token, ErrorService, $log) {
var apiHost = 'http://login.dialoga.gov.br/api/v1';
var endpoint = {
home: apiHost + '/articles/103358?private_token=null&fields=id,children,categories,abstract,title,image,url,setting,position',
articles: apiHost + '/articles?private_token=' + private_token + '&fields=id,children,created_by,categories,tag_list,abstract,setting,profile&content_type=ProposalsDiscussionPlugin::Proposal',
tasks: apiHost + '/task?private_token=' + private_token + '&fields=id,children,created_by,categories,tag_list,abstract,setting,profile&content_type=ProposalsDiscussionPlugin::Proposal'
};
var service = {
// mock
mockPrograma: mockPrograma,
// api
getArticles: getArticles,
getProposal: getProposal
};
return service;
// ---
// PUBLIC METHODS
// ---
/**
* Mock Data
* @return {Object} a new instance with dumb data;
*/
function mockPrograma () {
return {
id: -1,
title: "Valorização dos Professores",
abstract: "<p>Caminho para uma educação de qualidade.</p>",
image: {url: "/image_uploads/dialoga/0000/0140/valorizacao_professor.jpg"},
categories: [{name: "[category]", id: -1, slug: "[category-slug]", image: null}],
author: '[author]',
position: -1, // ?
profile: { // ?
id: -1,
identifier: '[profile.identifier]',
name: '[profile.name]',
},
setting: { // ?
author_name: '[setting.author_name]',
comment_paragraph_plugin_activate: false
},
children: [], // ?
tag_list: []
};
}
/**
* Get a list of articles
* @param {Number} limit per_page (default is 30)
* @return {Array} a list of articles
*/
function getArticles (limit) {
if (!limit) {
limit = 30;
}
return $http.get(endpoint.articles)
.then(handleSuccess)
.catch(handleError);
}
/**
* Get a task by id
* @param {Number} id of task
* @return {Object} the wanted task or a new one.
*/
function getProposal (id) {
if (!id) {
throw new Error(ErrorService.paramRequired('id'));
}
return $http.get(endpoint.tasks)
.then(handleSuccess)
.catch(handleError);
}
// ---
// PRIVATE METHODS
// ---
/**
* Transform the successful response, unwrapping the application data
* from the API response payload.
*
* @param {Object} response from the server.
* @return {Object} the data unwrapped.
*/
function handleSuccess (response){
return response.data;
}
/**
* Transform the error response, unwrapping the application data from
* the API response payload.
*
* @param {Object} error from the server.
* @return {Promise} promise rejection called.
*/
function handleError (error){
$log.error('XHR Failed on ProgramaService.\n' + angular.toJson(error.data, true));
// The API response from the server should be returned in a
// nomralized format. However, if the request was not handled by the
// server (or what not handles properly - ex. server error), then we
// may have to normalize it on our end, as best we can.
if ( !angular.isObject( error.data ) || !error.data.message) {
return( $q.reject( 'An unknown error occurred.' ) );
}
// Otherwise, use expected error message.
return $q.reject(error.data.message);
}
}
})();