-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Api for create, update, delete, list and show Addresses
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
module Api | ||
module V1 | ||
class AddressesController < ApplicationController | ||
skip_before_action :verify_authenticity_token | ||
before_action :authenticate_api_user | ||
before_action :authorize_api_user | ||
before_action :set_address, only: [:show, :update, :destroy] | ||
|
||
# GET /api/v1/addresses | ||
def index | ||
addresses = Address.includes(:country, :geo_state).all | ||
render json: addresses.as_json(include: { | ||
country: { only: [:id, :name] }, | ||
geo_state: { only: [:id, :name] } | ||
}), status: :ok | ||
end | ||
|
||
# GET /api/v1/addresses/:id | ||
def show | ||
render json: @address.as_json(include: { | ||
country: { only: [:id, :name] }, | ||
geo_state: { only: [:id, :name] } | ||
}), status: :ok | ||
end | ||
|
||
# POST /api/v1/addresses | ||
def create | ||
address = Address.new(address_params) | ||
if address.save | ||
render json: address.as_json(include: { | ||
country: { only: [:id, :name] }, | ||
geo_state: { only: [:id, :name] } | ||
}), status: :created | ||
else | ||
render json: { errors: address.errors.full_messages }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PUT /api/v1/addresses/:id | ||
def update | ||
if @address.update(address_params) | ||
render json: @address.as_json(include: { | ||
country: { only: [:id, :name] }, | ||
geo_state: { only: [:id, :name] } | ||
}), status: :ok | ||
else | ||
render json: { errors: @address.errors.full_messages }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /api/v1/addresses/:id | ||
def destroy | ||
@address.destroy | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def authenticate_api_user | ||
authenticate_or_request_with_http_basic do |username, password| | ||
@current_api_user = ApiUser.find_by(username: username)&.authenticate(password) | ||
end | ||
end | ||
|
||
def authorize_api_user | ||
unless @current_api_user&.api_permissions&.exists?(endpoint_name: 'addresses') | ||
render json: { error: 'Forbidden' }, status: :forbidden | ||
end | ||
end | ||
|
||
def set_address | ||
@address = Address.find(params[:id]) | ||
rescue ActiveRecord::RecordNotFound | ||
render json: { error: 'Address not found' }, status: :not_found | ||
end | ||
|
||
def address_params | ||
params.require(:address).permit( | ||
:street_address, :city, :state_id, :postal_code, | ||
:country_id, :geo_lat, :geo_long, :geo_labelx, :geo_labely | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters