articles.rb 3.29 KB
module API
  module V1
    class Articles < Grape::API
      before { authenticate! }

      resource :articles do

        # Collect comments from articles
        #
        # Parameters:
        #   from             - date where the search will begin. If nothing is passed the default date will be the date of the first article created
        #   oldest           - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
        #   limit            - amount of comments returned. The default value is 20
        #
        # Example Request:
        #  GET /api/v1/articles?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10&content_type=Hub
#    desc 'Articles.', {
#      :params => API::Entities::Article.documentation
#    }
        get do
          articles = select_filtered_collection_of(environment, 'articles', params)
          articles = articles.where(Article.display_filter(current_user.person, nil)[:conditions])
          present articles, :with => Entities::Article 
        end

        desc "Return the article id"
        get ':id' do
          article = find_article(environment.articles, params[:id])
          present article, :with => Entities::Article
        end

        get ':id/children' do
          article = find_article(environment.articles, params[:id])
          articles = select_filtered_collection_of(article, 'children', params)
          articles = articles.where(Article.display_filter(current_user.person, nil)[:conditions])
          present articles, :with => Entities::Article
        end

        get ':id/children/:child_id' do
          article = find_article(environment.articles, params[:id])
          present find_article(article.children, params[:child_id]), :with => Entities::Article
        end

      end

      resource :communities do 
        segment '/:community_id' do 
          resource :articles do
            get do
              community = environment.communities.find(params[:community_id])
              articles = select_filtered_collection_of(community, 'articles', params)
              articles = articles.where(Article.display_filter(current_user.person, community)[:conditions])
              present articles, :with => Entities::Article 
            end

            get '/:id' do
              community = environment.communities.find(params[:community_id])
              article = find_article(community.articles, params[:id])
              present article, :with => Entities::Article
            end

            # Example Request:
            #  POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body
            post do
              community = environment.communities.find(params[:community_id])
              klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]
              article = klass_type.constantize.new(params[:article])
              article.last_changed_by = current_person
              article.created_by= current_person
              article.profile = community

              if !article.save
                render_api_errors!(article.errors.full_messages)
              end
              present article, :with => Entities::Article
            end

          end
        end

      end
   
    end
  end
end