Commit cc96ff23b199a4eeb62ec7e14099d256e2a7926c
1 parent
c6d22d1a
Exists in
master
and in
34 other branches
Refactor profile and article services
Showing
9 changed files
with
62 additions
and
37 deletions
Show diff stats
src/app/cms/cms.component.ts
1 | import {StateConfig, Component, Inject} from 'ng-forward'; | 1 | import {StateConfig, Component, Inject} from 'ng-forward'; |
2 | import {Profile} from "./../models/interfaces"; | 2 | import {Profile} from "./../models/interfaces"; |
3 | +import {ArticleService} from "../../lib/ng-noosfero-api/http/article.service"; | ||
4 | + | ||
3 | @Component({ | 5 | @Component({ |
4 | selector: 'cms', | 6 | selector: 'cms', |
5 | templateUrl: "app/cms/cms.html" | 7 | templateUrl: "app/cms/cms.html" |
6 | }) | 8 | }) |
7 | -@Inject("noosfero", "$stateParams", "$httpParamSerializer", "$state", "SweetAlert") | 9 | +@Inject(ArticleService, "noosfero", "$stateParams", "$httpParamSerializer", "$state", "SweetAlert") |
8 | export class Cms { | 10 | export class Cms { |
9 | 11 | ||
10 | article: any = {}; | 12 | article: any = {}; |
11 | profile: any; | 13 | profile: any; |
12 | 14 | ||
13 | - constructor(private noosfero: any/* TODO convert noosferoService */, private $stateParams: ng.ui.IStateParamsService, private $httpParamSerializer: any, private $state: ng.ui.IStateService, private SweetAlert: any) { | 15 | + constructor(private ArticleService: ArticleService, private noosfero: any/* TODO convert noosferoService */, private $stateParams: ng.ui.IStateParamsService, private $httpParamSerializer: any, private $state: ng.ui.IStateService, private SweetAlert: any) { |
14 | 16 | ||
15 | } | 17 | } |
16 | 18 | ||
17 | save() { | 19 | save() { |
18 | this.noosfero.currentProfile.then((profile: Profile) => { | 20 | this.noosfero.currentProfile.then((profile: Profile) => { |
19 | - return this.noosfero.profiles.one(profile.id).customPOST( | ||
20 | - { article: this.article }, | ||
21 | - 'articles', | ||
22 | - {}, | ||
23 | - { 'Content-Type': 'application/json' } | ||
24 | - ) | 21 | + return this.ArticleService.create(profile.id, this.article); |
25 | }).then((response: restangular.IResponse) => { | 22 | }).then((response: restangular.IResponse) => { |
26 | this.$state.transitionTo('main.profile.page', { page: response.data.article.path, profile: response.data.article.profile.identifier }); | 23 | this.$state.transitionTo('main.profile.page', { page: response.data.article.path, profile: response.data.article.profile.identifier }); |
27 | this.SweetAlert.swal({ | 24 | this.SweetAlert.swal({ |
src/app/components/noosfero-articles/blog/blog.component.ts
1 | import {Component, Input, Inject} from "ng-forward"; | 1 | import {Component, Input, Inject} from "ng-forward"; |
2 | 2 | ||
3 | import {Article, Profile} from "./../../../models/interfaces"; | 3 | import {Article, Profile} from "./../../../models/interfaces"; |
4 | +import {ArticleService} from "../../../../lib/ng-noosfero-api/http/article.service"; | ||
4 | 5 | ||
5 | @Component({ | 6 | @Component({ |
6 | selector: "noosfero-blog", | 7 | selector: "noosfero-blog", |
7 | templateUrl: "app/components/noosfero-articles/blog/blog.html" | 8 | templateUrl: "app/components/noosfero-articles/blog/blog.html" |
8 | }) | 9 | }) |
9 | -@Inject("noosfero", "$scope") | 10 | +@Inject(ArticleService, "$scope") |
10 | export class NoosferoArticleBlog { | 11 | export class NoosferoArticleBlog { |
11 | 12 | ||
12 | @Input() article: Article; | 13 | @Input() article: Article; |
@@ -17,7 +18,7 @@ export class NoosferoArticleBlog { | @@ -17,7 +18,7 @@ export class NoosferoArticleBlog { | ||
17 | private currentPage: number; | 18 | private currentPage: number; |
18 | private totalPosts: number = 0; | 19 | private totalPosts: number = 0; |
19 | 20 | ||
20 | - constructor(private noosfero: any, private $scope: ng.IScope) { | 21 | + constructor(private ArticleService: ArticleService, private $scope: ng.IScope) { |
21 | } | 22 | } |
22 | 23 | ||
23 | ngOnInit() { | 24 | ngOnInit() { |
@@ -25,7 +26,7 @@ export class NoosferoArticleBlog { | @@ -25,7 +26,7 @@ export class NoosferoArticleBlog { | ||
25 | } | 26 | } |
26 | 27 | ||
27 | loadPage() { | 28 | loadPage() { |
28 | - this.noosfero.articles.one(this.article.id).customGET("children", { | 29 | + this.ArticleService.getChildren(this.article.id, { |
29 | content_type: "TinyMceArticle", | 30 | content_type: "TinyMceArticle", |
30 | per_page: this.perPage, | 31 | per_page: this.perPage, |
31 | page: this.currentPage | 32 | page: this.currentPage |
src/app/components/noosfero/noosfero.service.js
@@ -6,18 +6,7 @@ | @@ -6,18 +6,7 @@ | ||
6 | 6 | ||
7 | return { | 7 | return { |
8 | currentProfile: currentProfile.promise, | 8 | currentProfile: currentProfile.promise, |
9 | - setCurrentProfile: function(profile) { currentProfile.resolve(profile) }, | ||
10 | - profiles: Restangular.service('profiles'), | ||
11 | - articles: Restangular.service('articles'), | ||
12 | - profile: function(profileId) { | ||
13 | - return Restangular.one('profiles', profileId); | ||
14 | - }, | ||
15 | - members: function(profile) { | ||
16 | - return Restangular.service('members', profile); | ||
17 | - }, | ||
18 | - boxes: function(profileId) { | ||
19 | - return Restangular.service('boxes', Restangular.one('profiles', profileId)) | ||
20 | - } | 9 | + setCurrentProfile: function(profile) { currentProfile.resolve(profile) } |
21 | } | 10 | } |
22 | }); | 11 | }); |
23 | })(); | 12 | })(); |
src/app/content-viewer/content-viewer.component.ts
@@ -5,13 +5,14 @@ import {ArticleView} from "../components/noosfero-articles/article/article_view" | @@ -5,13 +5,14 @@ import {ArticleView} from "../components/noosfero-articles/article/article_view" | ||
5 | import {Input, Component, StateConfig, Inject} from "ng-forward"; | 5 | import {Input, Component, StateConfig, Inject} from "ng-forward"; |
6 | 6 | ||
7 | import {NoosferoArticleBlog} from "./../components/noosfero-articles/blog/blog.component"; | 7 | import {NoosferoArticleBlog} from "./../components/noosfero-articles/blog/blog.component"; |
8 | +import {ArticleService} from "../../lib/ng-noosfero-api/http/article.service"; | ||
8 | 9 | ||
9 | @Component({ | 10 | @Component({ |
10 | selector: "content-viewer", | 11 | selector: "content-viewer", |
11 | templateUrl: "app/content-viewer/page.html", | 12 | templateUrl: "app/content-viewer/page.html", |
12 | directives: [NoosferoArticleBlog, ArticleView] | 13 | directives: [NoosferoArticleBlog, ArticleView] |
13 | }) | 14 | }) |
14 | -@Inject("noosfero", "$log", "$stateParams") | 15 | +@Inject(ArticleService, "noosfero", "$log", "$stateParams") |
15 | export class ContentViewer { | 16 | export class ContentViewer { |
16 | 17 | ||
17 | @Input() | 18 | @Input() |
@@ -20,14 +21,14 @@ export class ContentViewer { | @@ -20,14 +21,14 @@ export class ContentViewer { | ||
20 | @Input() | 21 | @Input() |
21 | profile: noosfero.Profile = null; | 22 | profile: noosfero.Profile = null; |
22 | 23 | ||
23 | - constructor(private noosfero: any, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { | 24 | + constructor(private ArticleService: ArticleService, private noosfero: any, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { |
24 | this.activate(); | 25 | this.activate(); |
25 | } | 26 | } |
26 | 27 | ||
27 | activate() { | 28 | activate() { |
28 | this.noosfero.currentProfile.then((profile: noosfero.Profile) => { | 29 | this.noosfero.currentProfile.then((profile: noosfero.Profile) => { |
29 | this.profile = profile; | 30 | this.profile = profile; |
30 | - return this.noosfero.profiles.one(this.profile.id).one("articles").get({ path: this.$stateParams["page"] }); | 31 | + return this.ArticleService.getByProfile(this.profile.id, { path: this.$stateParams["page"] }); |
31 | }).then((response: restangular.IResponse) => { | 32 | }).then((response: restangular.IResponse) => { |
32 | this.article = response.data.article; | 33 | this.article = response.data.article; |
33 | }); | 34 | }); |
src/app/profile-info/profile-info.component.ts
1 | import {StateConfig, Component, Inject} from 'ng-forward'; | 1 | import {StateConfig, Component, Inject} from 'ng-forward'; |
2 | 2 | ||
3 | import {Profile} from "./../models/interfaces"; | 3 | import {Profile} from "./../models/interfaces"; |
4 | +import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; | ||
4 | 5 | ||
5 | @Component({ | 6 | @Component({ |
6 | selector: 'profile', | 7 | selector: 'profile', |
7 | templateUrl: "app/profile-info/profile-info.html" | 8 | templateUrl: "app/profile-info/profile-info.html" |
8 | }) | 9 | }) |
9 | -@Inject("noosfero") | 10 | +@Inject(ProfileService, "noosfero") |
10 | export class ProfileInfo { | 11 | export class ProfileInfo { |
11 | 12 | ||
12 | activities: any | 13 | activities: any |
13 | profile: any | 14 | profile: any |
14 | 15 | ||
15 | - constructor(private noosfero: any) { | 16 | + constructor(private ProfileService: ProfileService, private noosfero: any) { |
16 | this.activate(); | 17 | this.activate(); |
17 | } | 18 | } |
18 | 19 | ||
19 | activate() { | 20 | activate() { |
20 | this.noosfero.currentProfile.then((profile: Profile) => { | 21 | this.noosfero.currentProfile.then((profile: Profile) => { |
21 | this.profile = profile; | 22 | this.profile = profile; |
22 | - return this.noosfero.profiles.one(this.profile.id).one('activities').get(); | 23 | + return this.ProfileService.getActivities(this.profile.id); |
23 | }).then((response: restangular.IResponse) => { | 24 | }).then((response: restangular.IResponse) => { |
24 | this.activities = response.data.activities; | 25 | this.activities = response.data.activities; |
25 | }); | 26 | }); |
src/app/profile/profile-home.component.ts
1 | import {StateConfig, Component, Inject} from 'ng-forward'; | 1 | import {StateConfig, Component, Inject} from 'ng-forward'; |
2 | 2 | ||
3 | import {Profile} from "./../models/interfaces"; | 3 | import {Profile} from "./../models/interfaces"; |
4 | +import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; | ||
4 | 5 | ||
5 | @Component({ | 6 | @Component({ |
6 | selector: 'profile-home', | 7 | selector: 'profile-home', |
7 | template: "<div></div>" | 8 | template: "<div></div>" |
8 | }) | 9 | }) |
9 | -@Inject("noosfero", "$log", "$stateParams", "$scope", "$state") | 10 | +@Inject(ProfileService, "noosfero", "$log", "$stateParams", "$scope", "$state") |
10 | export class ProfileHome { | 11 | export class ProfileHome { |
11 | 12 | ||
12 | profile: Profile; | 13 | profile: Profile; |
13 | 14 | ||
14 | - constructor(noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService, $scope: ng.IScope, $state: ng.ui.IStateService) { | 15 | + constructor(ProfileService: ProfileService, noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService, $scope: ng.IScope, $state: ng.ui.IStateService) { |
15 | noosfero.currentProfile.then((profile: Profile) => { | 16 | noosfero.currentProfile.then((profile: Profile) => { |
16 | this.profile = profile; | 17 | this.profile = profile; |
17 | - return noosfero.profile(this.profile.id).customGET('home_page', { fields: 'path' }); | 18 | + return ProfileService.get(this.profile.id).customGET('home_page', { fields: 'path' }); |
18 | }).then((response: restangular.IResponse) => { | 19 | }).then((response: restangular.IResponse) => { |
19 | if (response.data.article) { | 20 | if (response.data.article) { |
20 | $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false }); | 21 | $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false }); |
src/app/profile/profile.component.ts
@@ -4,6 +4,7 @@ import {ProfileHome} from '../profile/profile-home.component'; | @@ -4,6 +4,7 @@ import {ProfileHome} from '../profile/profile-home.component'; | ||
4 | import {Cms} from '../cms/cms.component'; | 4 | import {Cms} from '../cms/cms.component'; |
5 | import {ContentViewer} from "../content-viewer/content-viewer.component"; | 5 | import {ContentViewer} from "../content-viewer/content-viewer.component"; |
6 | import {NoosferoActivities} from "../components/noosfero-activities/activities.component"; | 6 | import {NoosferoActivities} from "../components/noosfero-activities/activities.component"; |
7 | +import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; | ||
7 | 8 | ||
8 | import * as noosferoModels from "./../models/interfaces"; | 9 | import * as noosferoModels from "./../models/interfaces"; |
9 | 10 | ||
@@ -66,17 +67,17 @@ import * as noosferoModels from "./../models/interfaces"; | @@ -66,17 +67,17 @@ import * as noosferoModels from "./../models/interfaces"; | ||
66 | } | 67 | } |
67 | } | 68 | } |
68 | ]) | 69 | ]) |
69 | -@Inject("noosfero", "$log", "$stateParams") | 70 | +@Inject(ProfileService, "noosfero", "$log", "$stateParams") |
70 | export class Profile { | 71 | export class Profile { |
71 | 72 | ||
72 | boxes: noosferoModels.Box[]; | 73 | boxes: noosferoModels.Box[]; |
73 | profile: noosferoModels.Profile; | 74 | profile: noosferoModels.Profile; |
74 | 75 | ||
75 | - constructor(noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService) { | ||
76 | - noosfero.profiles.one().get({ identifier: $stateParams["profile"] }).then((response: restangular.IResponse) => { | 76 | + constructor(ProfileService: ProfileService, noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService) { |
77 | + ProfileService.getByIdentifier($stateParams["profile"]).then((response: restangular.IResponse) => { | ||
77 | this.profile = response.data[0]; | 78 | this.profile = response.data[0]; |
78 | noosfero.setCurrentProfile(this.profile); | 79 | noosfero.setCurrentProfile(this.profile); |
79 | - return noosfero.boxes(this.profile.id).one().get(); | 80 | + return ProfileService.getBoxes(this.profile.id); |
80 | }).then((response: restangular.IResponse) => { | 81 | }).then((response: restangular.IResponse) => { |
81 | this.boxes = response.data.boxes; | 82 | this.boxes = response.data.boxes; |
82 | }); | 83 | }); |
src/lib/ng-noosfero-api/http/article.service.ts
1 | import { Injectable, Inject } from "ng-forward"; | 1 | import { Injectable, Inject } from "ng-forward"; |
2 | +import {Article} from "../../../app/models/interfaces"; | ||
2 | 3 | ||
3 | @Injectable() | 4 | @Injectable() |
4 | @Inject("Restangular") | 5 | @Inject("Restangular") |
@@ -8,8 +9,25 @@ export class ArticleService { | @@ -8,8 +9,25 @@ export class ArticleService { | ||
8 | 9 | ||
9 | } | 10 | } |
10 | 11 | ||
12 | + create(profileId: number, article: Article) { | ||
13 | + return this.Restangular.one('profiles', profileId).customPOST( | ||
14 | + { article: article }, | ||
15 | + 'articles', | ||
16 | + {}, | ||
17 | + { 'Content-Type': 'application/json' } | ||
18 | + ); | ||
19 | + } | ||
20 | + | ||
21 | + get(articleId: number) { | ||
22 | + return this.Restangular.one('articles', articleId); | ||
23 | + } | ||
24 | + | ||
11 | getByProfile(profileId: number, filters: any) { | 25 | getByProfile(profileId: number, filters: any) { |
12 | return this.Restangular.one('profiles', profileId).customGET('articles', filters); | 26 | return this.Restangular.one('profiles', profileId).customGET('articles', filters); |
13 | } | 27 | } |
14 | 28 | ||
29 | + getChildren(articleId: number, options: any = {}) { | ||
30 | + return this.get(articleId).customGET('children', options); | ||
31 | + } | ||
32 | + | ||
15 | } | 33 | } |
src/lib/ng-noosfero-api/http/profile.service.ts
@@ -8,8 +8,24 @@ export class ProfileService { | @@ -8,8 +8,24 @@ export class ProfileService { | ||
8 | 8 | ||
9 | } | 9 | } |
10 | 10 | ||
11 | + getActivities(profileId: number, options: any = {}) { | ||
12 | + return this.get(profileId).customGET("activities", options); | ||
13 | + } | ||
14 | + | ||
15 | + get(profileId: number) { | ||
16 | + return this.Restangular.one('profiles', profileId); | ||
17 | + } | ||
18 | + | ||
11 | getProfileMembers(profileId: number, filters: any) { | 19 | getProfileMembers(profileId: number, filters: any) { |
12 | - return this.Restangular.service('profiles').one(profileId).customGET("members", filters); | 20 | + return this.get(profileId).customGET("members", filters); |
21 | + } | ||
22 | + | ||
23 | + getByIdentifier(identifier: string) { | ||
24 | + return this.Restangular.one('profiles').get({ identifier: identifier }); | ||
25 | + } | ||
26 | + | ||
27 | + getBoxes(profileId: number) { | ||
28 | + return this.get(profileId).customGET('boxes'); | ||
13 | } | 29 | } |
14 | 30 | ||
15 | } | 31 | } |