Commit 76221bc16a8ee966133862565495ce6c18704bba
Committed by
Rodrigo Souto
1 parent
d460f910
Exists in
master
and in
29 other branches
api: suggest article
Showing
3 changed files
with
43 additions
and
2 deletions
Show diff stats
lib/noosfero/api/entities.rb
| @@ -101,7 +101,12 @@ module Noosfero | @@ -101,7 +101,12 @@ module Noosfero | ||
| 101 | class UserLogin < User | 101 | class UserLogin < User |
| 102 | expose :private_token | 102 | expose :private_token |
| 103 | end | 103 | end |
| 104 | - | 104 | + |
| 105 | + class Task < Entity | ||
| 106 | + expose :id | ||
| 107 | + expose :type | ||
| 108 | + end | ||
| 109 | + | ||
| 105 | end | 110 | end |
| 106 | end | 111 | end |
| 107 | end | 112 | end |
lib/noosfero/api/v1/articles.rb
| @@ -48,6 +48,21 @@ module Noosfero | @@ -48,6 +48,21 @@ module Noosfero | ||
| 48 | present find_article(article.children, params[:child_id]), :with => Entities::Article, :fields => params[:fields] | 48 | present find_article(article.children, params[:child_id]), :with => Entities::Article, :fields => params[:fields] |
| 49 | end | 49 | end |
| 50 | 50 | ||
| 51 | + post ':id/children/suggest' do | ||
| 52 | + parent_article = environment.articles.find(params[:id]) | ||
| 53 | + profile = environment.profiles.find(params[:target_id]) | ||
| 54 | + | ||
| 55 | + suggest_article = SuggestArticle.new | ||
| 56 | + suggest_article.article = params[:article] | ||
| 57 | + suggest_article.target = profile | ||
| 58 | + suggest_article.requestor = current_person | ||
| 59 | + | ||
| 60 | + unless suggest_article.save | ||
| 61 | + render_api_errors!(suggest_article.article_object.errors.full_messages) | ||
| 62 | + end | ||
| 63 | + present suggest_article, :with => Entities::Task, :fields => params[:fields] | ||
| 64 | + end | ||
| 65 | + | ||
| 51 | # Example Request: | 66 | # Example Request: |
| 52 | # POST api/v1/articles/:id/children?private_token=234298743290432&article[name]=title&article[body]=body | 67 | # POST api/v1/articles/:id/children?private_token=234298743290432&article[name]=title&article[body]=body |
| 53 | post ':id/children' do | 68 | post ':id/children' do |
| @@ -72,7 +87,6 @@ module Noosfero | @@ -72,7 +87,6 @@ module Noosfero | ||
| 72 | present article, :with => Entities::Article, :fields => params[:fields] | 87 | present article, :with => Entities::Article, :fields => params[:fields] |
| 73 | end | 88 | end |
| 74 | 89 | ||
| 75 | - | ||
| 76 | end | 90 | end |
| 77 | 91 | ||
| 78 | resource :communities do | 92 | resource :communities do |
test/unit/api/articles_test.rb
| @@ -451,4 +451,26 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -451,4 +451,26 @@ class ArticlesTest < ActiveSupport::TestCase | ||
| 451 | assert_equal ['title'], json['articles'].first.keys | 451 | assert_equal ['title'], json['articles'].first.keys |
| 452 | end | 452 | end |
| 453 | 453 | ||
| 454 | + should 'suggest article children' do | ||
| 455 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
| 456 | + params[:target_id] = user.person.id | ||
| 457 | + params[:article] = {:name => "Article name", :body => "Article body"} | ||
| 458 | + assert_difference "SuggestArticle.count" do | ||
| 459 | + post "/api/v1/articles/#{article.id}/children/suggest?#{params.to_query}" | ||
| 460 | + end | ||
| 461 | + json = JSON.parse(last_response.body) | ||
| 462 | + assert_equal 'SuggestArticle', json['type'] | ||
| 463 | + end | ||
| 464 | + | ||
| 465 | + should 'suggest event children' do | ||
| 466 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
| 467 | + params[:target_id] = user.person.id | ||
| 468 | + params[:article] = {:name => "Article name", :body => "Article body", :type => "Event"} | ||
| 469 | + assert_difference "SuggestArticle.count" do | ||
| 470 | + post "/api/v1/articles/#{article.id}/children/suggest?#{params.to_query}" | ||
| 471 | + end | ||
| 472 | + json = JSON.parse(last_response.body) | ||
| 473 | + assert_equal 'SuggestArticle', json['type'] | ||
| 474 | + end | ||
| 475 | + | ||
| 454 | end | 476 | end |