dawarich/app/controllers/api/v1/areas_controller.rb

48 lines
1 KiB
Ruby
Raw Normal View History

2024-07-21 14:09:42 -04:00
# frozen_string_literal: true
class Api::V1::AreasController < ApplicationController
2024-07-27 08:30:46 -04:00
skip_forgery_protection
before_action :authenticate_api_key
2024-07-21 14:09:42 -04:00
before_action :set_area, only: %i[update destroy]
def index
@areas = current_api_user.areas
2024-07-21 14:09:42 -04:00
render json: @areas, status: :ok
end
def create
@area = current_api_user.areas.build(area_params)
2024-07-21 14:09:42 -04:00
if @area.save
render json: @area, status: :created
else
render json: { errors: @area.errors.full_messages }, status: :unprocessable_entity
end
end
def update
if @area.update(area_params)
render json: @area, status: :ok
else
render json: { errors: @area.errors.full_messages }, status: :unprocessable_entity
end
end
def destroy
@area.destroy!
render json: { message: 'Area was successfully deleted' }, status: :ok
end
private
def set_area
@area = current_api_user.areas.find(params[:id])
2024-07-21 14:09:42 -04:00
end
def area_params
params.require(:area).permit(:name, :latitude, :longitude, :radius)
end
end