2024-07-21 14:09:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-08-25 14:19:02 -04:00
|
|
|
class Api::V1::AreasController < ApiController
|
2025-12-06 14:54:49 -05:00
|
|
|
before_action :set_area, only: %i[show update destroy]
|
2024-07-21 14:09:42 -04:00
|
|
|
|
|
|
|
|
def index
|
2024-07-27 06:22:56 -04:00
|
|
|
@areas = current_api_user.areas
|
2024-07-21 14:09:42 -04:00
|
|
|
|
|
|
|
|
render json: @areas, status: :ok
|
|
|
|
|
end
|
|
|
|
|
|
2025-12-06 14:54:49 -05:00
|
|
|
def show
|
|
|
|
|
render json: @area, status: :ok
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-21 14:09:42 -04:00
|
|
|
def create
|
2024-07-27 06:22:56 -04:00
|
|
|
@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
|
2025-09-12 15:08:45 -04:00
|
|
|
render json: { errors: @area.errors.full_messages }, status: :unprocessable_content
|
2024-07-21 14:09:42 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
|
|
|
|
if @area.update(area_params)
|
|
|
|
|
render json: @area, status: :ok
|
|
|
|
|
else
|
2025-09-12 15:08:45 -04:00
|
|
|
render json: { errors: @area.errors.full_messages }, status: :unprocessable_content
|
2024-07-21 14:09:42 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
|
@area.destroy!
|
|
|
|
|
|
|
|
|
|
render json: { message: 'Area was successfully deleted' }, status: :ok
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def set_area
|
2024-07-27 06:22:56 -04:00
|
|
|
@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
|