diaspora/app/controllers/api/v1/aspects_controller.rb
2020-01-21 23:35:01 +01:00

79 lines
2.5 KiB
Ruby

# frozen_string_literal: true
module Api
module V1
class AspectsController < Api::V1::BaseController
before_action except: %i[create update destroy] do
require_access_token %w[contacts:read]
end
before_action only: %i[create update destroy] do
require_access_token %w[contacts:modify]
end
def index
aspects_query = current_user.aspects
aspects_page = index_pager(aspects_query).response
aspects_page[:data] = aspects_page[:data].map {|a| aspect_as_json(a, false) }
render_paged_api_response aspects_page
end
def show
aspect = current_user.aspects.where(id: params[:id]).first
if aspect
render json: aspect_as_json(aspect, true)
else
render json: I18n.t("api.endpoint_errors.aspects.not_found"), status: :not_found
end
end
def create
params.require(%i[name])
aspect = current_user.aspects.build(name: params[:name])
if aspect&.save
render json: aspect_as_json(aspect, true)
else
render json: I18n.t("api.endpoint_errors.aspects.cant_create"), status: :unprocessable_entity
end
rescue ActionController::ParameterMissing
render json: I18n.t("api.endpoint_errors.aspects.cant_create"), status: :unprocessable_entity
end
def update
aspect = current_user.aspects.where(id: params[:id]).first
if !aspect
render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :not_found
elsif aspect.update!(aspect_params(true))
render json: aspect_as_json(aspect, true)
else
render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :unprocessable_entity
end
rescue ActionController::ParameterMissing, ActiveRecord::RecordInvalid
render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :unprocessable_entity
end
def destroy
aspect = current_user.aspects.where(id: params[:id]).first
if aspect&.destroy
head :no_content
else
render json: I18n.t("api.endpoint_errors.aspects.cant_delete"), status: :unprocessable_entity
end
end
private
def aspect_params(allow_order=false)
parameters = params.permit(:name)
parameters[:order_id] = params[:order] if params.has_key?(:order) && allow_order
parameters
end
def aspect_as_json(aspect, as_full)
AspectPresenter.new(aspect).as_api_json(as_full)
end
end
end
end