Class: Railspress::Api::V1::CategoriesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/railspress/api/v1/categories_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_key

Instance Method Summary collapse

Instance Method Details

#createObject



29
30
31
32
33
34
35
36
37
# File 'app/controllers/railspress/api/v1/categories_controller.rb', line 29

def create
  category = Railspress::Category.new(category_params)

  if category.save
    render json: { data: serialize_category(category) }, status: :created
  else
    render_validation_errors(category)
  end
end

#destroyObject



47
48
49
50
51
52
53
# File 'app/controllers/railspress/api/v1/categories_controller.rb', line 47

def destroy
  if @category.destroy
    head :no_content
  else
    render_validation_errors(@category)
  end
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/railspress/api/v1/categories_controller.rb', line 9

def index
  categories = Railspress::Category.ordered
  total_count = categories.count
  categories = categories.offset((page - 1) * per_page).limit(per_page)

  render json: {
    data: categories.map { |category| serialize_category(category) },
    meta: {
      page: page,
      per: per_page,
      total_count: total_count,
      total_pages: (total_count.to_f / per_page).ceil
    }
  }
end

#showObject



25
26
27
# File 'app/controllers/railspress/api/v1/categories_controller.rb', line 25

def show
  render json: { data: serialize_category(@category) }
end

#updateObject



39
40
41
42
43
44
45
# File 'app/controllers/railspress/api/v1/categories_controller.rb', line 39

def update
  if @category.update(category_params)
    render json: { data: serialize_category(@category) }
  else
    render_validation_errors(@category)
  end
end