mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
47 lines
997 B
Ruby
47 lines
997 B
Ruby
|
|
# frozen_string_literal: true
|
||
|
|
|
||
|
|
class Api::V1::AreasController < ApplicationController
|
||
|
|
before_action :authenticate_user!
|
||
|
|
before_action :set_area, only: %i[update destroy]
|
||
|
|
|
||
|
|
def index
|
||
|
|
@areas = current_user.areas
|
||
|
|
|
||
|
|
render json: @areas, status: :ok
|
||
|
|
end
|
||
|
|
|
||
|
|
def create
|
||
|
|
@area = current_user.areas.build(area_params)
|
||
|
|
|
||
|
|
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_user.areas.find(params[:id])
|
||
|
|
end
|
||
|
|
|
||
|
|
def area_params
|
||
|
|
params.require(:area).permit(:name, :latitude, :longitude, :radius)
|
||
|
|
end
|
||
|
|
end
|