Commit e1fadd02c733fb5af9fac8d66dbf1717a4732add

Authored by Leonardo Merlin
1 parent 5215e1c4

Update service

src/app/components/article-service/article.service.js
@@ -32,7 +32,8 @@ @@ -32,7 +32,8 @@
32 var url = service.apiArticles + articleId; 32 var url = service.apiArticles + articleId;
33 var paramsExtended = angular.extend({}, params); 33 var paramsExtended = angular.extend({}, params);
34 34
35 - UtilService.get(url, {params: paramsExtended}).then(function(data){ 35 + UtilService.get(url, {params: paramsExtended})
  36 + .then(function(data){
36 cbSuccess(data); 37 cbSuccess(data);
37 }).catch(function(error){ 38 }).catch(function(error){
38 cbError(error); 39 cbError(error);
@@ -47,43 +48,38 @@ @@ -47,43 +48,38 @@
47 throw { name: 'NotImplementedYet', message: 'The service "getArticleBySlug" is not implemented yet.'}; 48 throw { name: 'NotImplementedYet', message: 'The service "getArticleBySlug" is not implemented yet.'};
48 } 49 }
49 50
50 - function getCategories (articleId, cbSuccess, cbError) { 51 + function getCategories (articleId, params, cbSuccess, cbError) {
51 // Ex.: /api/v1/articles/103358?fields= 52 // Ex.: /api/v1/articles/103358?fields=
52 53
53 var url = service.apiArticles + articleId; 54 var url = service.apiArticles + articleId;
54 -  
55 - UtilService.get(url, {params: { 55 + var paramsExtended = angular.extend({
56 'fields[]': ['id', 'categories'] 56 'fields[]': ['id', 'categories']
57 - }}).then(function(data){ 57 + }, params);
  58 +
  59 + UtilService.get(url, {params: paramsExtended})
  60 + .then(function(data){
58 cbSuccess(data); 61 cbSuccess(data);
59 }).catch(function(error){ 62 }).catch(function(error){
60 cbError(error); 63 cbError(error);
61 }); 64 });
62 } 65 }
63 66
64 - function getCategoryBySlug () { 67 + function getCategoryBySlug (/*slug, params, cbSuccess, cbError*/) {
65 throw { name: 'NotImplementedYet', message: 'The service "getArticleBySlug" is not implemented yet.'}; 68 throw { name: 'NotImplementedYet', message: 'The service "getArticleBySlug" is not implemented yet.'};
66 } 69 }
67 70
68 - function getTopics (cbSuccess, cbError) { 71 + function getTopics (params, cbSuccess, cbError) {
69 // Ex.: /api/v1/articles/103358/children?fields= 72 // Ex.: /api/v1/articles/103358/children?fields=
70 -  
71 - var url = service.apiArticles + API.articleId.home + '/children';  
72 -  
73 - UtilService.get(url, {params: {  
74 - 'fields[]': ['id', 'title', 'slug', 'abstract', 'categories', 'setting', 'children_count', 'hits']  
75 - }}).then(function(data){  
76 - cbSuccess(data);  
77 - }).catch(function(error){  
78 - cbError(error);  
79 - }); 73 + getTopicById(API.articleId.home);
80 } 74 }
81 75
82 - function getTopicById (topicId, cbSuccess, cbError) {  
83 - // Ex.: /api/v1/articles/103358/children/121521?fields= 76 + function getTopicById (topicId, params, cbSuccess, cbError) {
  77 + // Ex.: /api/v1/articles/103358/children?fields=
84 78
85 - // var url = service.apiArticles + API.articleId.home + '/children/' + topicId; // dont need to chain  
86 - var url = service.apiArticles + topicId; 79 + var url = service.apiArticles + topicId + '/children';
  80 + var paramsExtended = angular.extend({
  81 + 'fields[]': ['id', 'categories']
  82 + }, params);
87 83
88 UtilService.get(url, {params: { 84 UtilService.get(url, {params: {
89 'fields[]': ['id', 'title', 'body', 'slug', 'abstract', 'categories', 'setting', 'children_count', 'hits'] 85 'fields[]': ['id', 'title', 'body', 'slug', 'abstract', 'categories', 'setting', 'children_count', 'hits']
@@ -111,11 +107,23 @@ @@ -111,11 +107,23 @@
111 }); 107 });
112 } 108 }
113 109
  110 + /**
  111 + * Ex.: /api/v1/articles/[article_id]/children?[params]content_type=ProposalsDiscussionPlugin::Proposal
  112 + * Ex.: /api/v1/articles/103644/children?limit=20&fields=id,name,slug,abstract,created_by&content_type=ProposalsDiscussionPlugin::Proposal
  113 + *
  114 + * @param {Integer} topicId topic where has those proposals
  115 + * @param {Object} params params for pagination ant others
  116 + * @param {Function} cbSuccess callback for success
  117 + * @param {Function} cbError callback for error
  118 + * @return {Array} [description]
  119 + */
114 function getProposalsByTopicId (topicId, params, cbSuccess, cbError) { 120 function getProposalsByTopicId (topicId, params, cbSuccess, cbError) {
115 - var url = service.apiArticles + topicId; 121 + var url = service.apiArticles + topicId + '/children';
116 122
117 var paramsExtended = angular.extend({ 123 var paramsExtended = angular.extend({
118 'fields[]': ['id', 'title', 'abstract', 'children', 'children_count'], 124 'fields[]': ['id', 'title', 'abstract', 'children', 'children_count'],
  125 + 'limit':'20',
  126 + 'page':'1',
119 'content_type':'ProposalsDiscussionPlugin::Proposals' 127 'content_type':'ProposalsDiscussionPlugin::Proposals'
120 }, params); 128 }, params);
121 129
@@ -126,11 +134,10 @@ @@ -126,11 +134,10 @@
126 }); 134 });
127 } 135 }
128 136
129 - function getRandomProposal (cbSuccess, cbError) {}  
130 -  
131 function getEvents (community_id, params, cbSuccess, cbError) { 137 function getEvents (community_id, params, cbSuccess, cbError) {
  138 + // Ex.: /api/v1/communities/19195/articles?categories_ids[]=' + cat_id + '&content_type=Event';
132 // Ex.: /api/v1/communities/' + community_id + '/articles?categories_ids[]=' + cat_id + '&content_type=Event'; 139 // Ex.: /api/v1/communities/' + community_id + '/articles?categories_ids[]=' + cat_id + '&content_type=Event';
133 - 140 +
134 var url = service.apiCommunities + community_id + '/articles'; 141 var url = service.apiCommunities + community_id + '/articles';
135 var paramsExtended = angular.extend({ 142 var paramsExtended = angular.extend({
136 'fields[]': ['id', 'slug', 'title', 'abstract', 'body', 'categories', 'created_at', 'start_date', 'end_date', 'hits'], 143 'fields[]': ['id', 'slug', 'title', 'abstract', 'body', 'categories', 'created_at', 'start_date', 'end_date', 'hits'],
src/app/components/dialoga-service/dialoga.service.js
@@ -9,24 +9,23 @@ @@ -9,24 +9,23 @@
9 function DialogaService($rootScope, $sce, API, ArticleService, UtilService, Slug, $log) { 9 function DialogaService($rootScope, $sce, API, ArticleService, UtilService, Slug, $log) {
10 $log.debug('DialogaService'); 10 $log.debug('DialogaService');
11 11
12 - var service = {  
13 - getHome: getHome,  
14 - getAbout: getAbout,  
15 - getThemes: getThemes,  
16 - getPrograms: getPrograms,  
17 - getProgramBySlug: getProgramBySlug,  
18 - getProgramsRandom: getProgramsRandom,  
19 - getProposals: getProposals,  
20 - getProposalsByTopicId: getProposalsByTopicId,  
21 - getEvents: getEvents,  
22 - getQuestions: getQuestions,  
23 - searchProgram: searchProgram,  
24 - searchProposal: searchProposal,  
25 - }; 12 + var extendedService = angular.extend({}, ArticleService);
  13 +
  14 + extendedService.serviceDialoga = $rootScope.basePath + '/api/v1/dialoga_plugin/';
  15 + extendedService.getHome = getHome;
  16 + extendedService.getAbout = getAbout;
  17 + extendedService.getThemes = getThemes;
  18 + extendedService.getPrograms = getPrograms;
  19 + extendedService.getProgramBySlug = getProgramBySlug;
  20 + extendedService.getProgramsRandom = getProgramsRandom;
  21 + extendedService.getEvents = getEvents; // override
  22 + extendedService.getQuestions = getQuestions;
  23 + extendedService.searchPrograms = searchPrograms;
  24 + extendedService.searchProposals = searchProposals;
26 25
27 var CACHE = {}; 26 var CACHE = {};
28 27
29 - return service; 28 + return extendedService;
30 29
31 function getHome (cbSuccess, cbError) { 30 function getHome (cbSuccess, cbError) {
32 if( !!CACHE.home ){ 31 if( !!CACHE.home ){
@@ -91,11 +90,11 @@ @@ -91,11 +90,11 @@
91 } 90 }
92 91
93 function getProgramBySlug (slug, cbSuccess, cbError) { 92 function getProgramBySlug (slug, cbSuccess, cbError) {
94 - 93 +
95 if( !CACHE.programs ){ 94 if( !CACHE.programs ){
96 getPrograms(_getProgramBySlug, cbError); 95 getPrograms(_getProgramBySlug, cbError);
97 } else { 96 } else {
98 - _getProgramBySlug(); 97 + _getProgramBySlug();
99 } 98 }
100 99
101 function _getProgramBySlug(){ 100 function _getProgramBySlug(){
@@ -110,56 +109,46 @@ @@ -110,56 +109,46 @@
110 } 109 }
111 } 110 }
112 111
113 - function getProgramsRandom (cbSuccess, cbError) {  
114 - getPrograms(cbSuccess, cbError);  
115 - // TODO: get endpoint for production  
116 - // if( !!CACHE.programsRandom ){  
117 - // cbSuccess(CACHE.programsRandom);  
118 - // }else{  
119 - // // load article content  
120 - // // UtilService.get(API.random_topics, {params: {  
121 - // ArticleService.getArticleById(API.articleId.home, {params: {  
122 - // 'fields[]': [  
123 - // 'id', 'title', 'slug', 'abstract', 'children_count'],  
124 - // 'content_type': 'ProposalsDiscussionPlugin::Topic'  
125 - // }}).then(function(data){  
126 - // CACHE.programsRandom = data;  
127 -  
128 - // cbSuccess(data);  
129 - // }).catch(function(error){  
130 - // cbError(error);  
131 - // });  
132 - // }  
133 - } 112 + // Ex.: /api/v1/dialoga_plugin/random_topics/103358
  113 + // TODO: get endpoint for production
  114 + // TODO: put at cache?
  115 + function getProgramsRandom(params, cbSuccess, cbError) {
134 116
135 - function getProposals (param, cbSuccess, cbError) {  
136 - ArticleService.getProposals(param, function (data){  
137 - CACHE.proposals = data; 117 + if( !!CACHE.programsRandom ){
  118 + cbSuccess(CACHE.programsRandom);
  119 + }else{
  120 + var mapFromCache = !!CACHE.programs;
138 121
139 - cbSuccess(CACHE.proposals);  
140 - }, cbError);  
141 - } 122 + var url = extendedService.serviceDialoga + 'random_topics/' + API.articleId.home;
  123 + var fields = null;
142 124
143 - function getProposalsByTopicId (topicId, params, cbSuccess, cbError) {  
144 - ArticleService.getProposalsByTopicId(topicId, params, function (data){  
145 - CACHE.proposals = data; 125 + if (mapFromCache){
  126 + // get only references
  127 + fields = ['id', 'title', 'slug'];
  128 + }else{
  129 + // get all content
  130 + fields = [];
  131 + }
  132 +
  133 + var paramsExtended = angular.extend({
  134 + 'fields[]': fields
  135 + }, params);
146 136
147 - cbSuccess(CACHE.proposals);  
148 - }, cbError); 137 + UtilService.get(url, {params: paramsExtended}).then(function(data){
  138 + _pipeHandleProgramsRandomFromCache(mapFromCache, data, cbSuccess);
  139 + }).catch(function(error){
  140 + cbError(error);
  141 + });
  142 + }
149 } 143 }
150 144
151 function getEvents (params, cbSuccess, cbError) { 145 function getEvents (params, cbSuccess, cbError) {
152 - if( !!CACHE.events ){  
153 - cbSuccess(CACHE.events);  
154 - }else{  
155 - ArticleService.getEvents(API.communityId, params, function(data){  
156 - CACHE.events = data; 146 + var paramsExtended = angular.extend({}, params);
157 147
158 - cbSuccess(data);  
159 - }, cbError);  
160 - } 148 + ArticleService.getEvents(API.communityId, paramsExtended, cbSuccess, cbError);
161 } 149 }
162 150
  151 + // TODO: implement
163 function getQuestions (cbSuccess/*, cbError*/) { 152 function getQuestions (cbSuccess/*, cbError*/) {
164 if( !!CACHE.questions ){ 153 if( !!CACHE.questions ){
165 cbSuccess(CACHE.questions); 154 cbSuccess(CACHE.questions);
@@ -172,9 +161,13 @@ @@ -172,9 +161,13 @@
172 } 161 }
173 } 162 }
174 163
175 - function searchProgram (cbSuccess, cbError) {} 164 + function searchPrograms (query, cbSuccess, cbError) {
  165 + ArticleService.searchTopics({query: query}, cbSuccess, cbError);
  166 + }
176 167
177 - function searchProposal (cbSuccess, cbError) {} 168 + function searchProposals (query, cbSuccess, cbError) {
  169 + ArticleService.searchProposals({query: query}, cbSuccess, cbError);
  170 + }
178 171
179 function _pipeHandleYoutube (data) { 172 function _pipeHandleYoutube (data) {
180 var abstract = data.article.abstract; 173 var abstract = data.article.abstract;
@@ -241,6 +234,30 @@ @@ -241,6 +234,30 @@
241 } 234 }
242 } 235 }
243 236
  237 + function _pipeHandleProgramsRandomFromCache (mapFromCache, data, cbSuccess){
  238 +
  239 + if(mapFromCache){
  240 + var result = [];
  241 + var refPrograms = data.articles;
  242 +
  243 + for (var i = CACHE.programs.length - 1; i >= 0; i--) {
  244 + var cachedProgram = CACHE.programs[i];
  245 +
  246 + for (var j = refPrograms.length - 1; j >= 0; j--) {
  247 + var refProgram = refPrograms[j];
  248 +
  249 + if(refProgram.id === cachedProgram.id){
  250 + result.push(cachedProgram);
  251 + }
  252 + }
  253 + }
  254 +
  255 + data.articles = result;
  256 + }
  257 +
  258 + cbSuccess(data);
  259 + }
  260 +
244 function forceIframeParams(abstract) { 261 function forceIframeParams(abstract) {
245 var patternIframe = '<iframe src="'; 262 var patternIframe = '<iframe src="';
246 var indexOfIframe = abstract.indexOf(patternIframe); 263 var indexOfIframe = abstract.indexOf(patternIframe);