2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Category
|
|
|
|
# Categories are used to classify Events
|
2015-05-05 03:10:25 +02:00
|
|
|
class API::CategoriesController < API::ApiController
|
2016-06-29 16:09:27 +02:00
|
|
|
before_action :authenticate_user!, except: [:index]
|
2019-01-16 16:28:25 +01:00
|
|
|
before_action :set_category, only: %i[show update destroy]
|
2016-06-28 17:06:33 +02:00
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
def index
|
|
|
|
@categories = Category.all
|
|
|
|
end
|
2016-06-28 17:06:33 +02:00
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
def show; end
|
2016-06-28 17:06:33 +02:00
|
|
|
|
|
|
|
def create
|
|
|
|
authorize Category
|
|
|
|
@category = Category.new(category_params)
|
|
|
|
if @category.save
|
|
|
|
render :show, status: :created, location: @category
|
|
|
|
else
|
|
|
|
render json: @category.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Category
|
|
|
|
if @category.update(category_params)
|
|
|
|
render :show, status: :ok, location: @category
|
|
|
|
else
|
|
|
|
render json: @category.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize Category
|
2016-06-30 09:57:40 +02:00
|
|
|
if @category.safe_destroy
|
|
|
|
head :no_content
|
|
|
|
else
|
|
|
|
render json: @category.errors, status: :unprocessable_entity
|
|
|
|
end
|
2016-06-28 17:06:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
def set_category
|
|
|
|
@category = Category.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_params
|
|
|
|
params.require(:category).permit(:name)
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|