cirandas.net

ref: master

app/api/v1/categories.rb


 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
module Api
  module V1
    class Categories < Grape::API

      resource :categories do

        get do
          type = params[:category_type]
          include_parent = params[:include_parent] == 'true'
          include_children = params[:include_children] == 'true'

          categories = type.nil? ?  environment.categories : environment.categories.where(:type => type)
          present_partial categories, :with => Entities::Category, parent: include_parent, children: include_children
        end

        desc "Return the category by id"
        get ':id' do
          present_partial environment.categories.find(params[:id]), :with => Entities::Category, parent: true, children: true
        end

      end

    end
  end
end