CRUD API ready
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
class ApplicationController < ActionController::API
|
class ApplicationController < ActionController::API
|
||||||
|
include Response
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
module Response
|
module Response
|
||||||
|
def error_response(status)
|
||||||
|
render nothing: true, status: status
|
||||||
|
end
|
||||||
|
|
||||||
def json_response(object, status = :ok)
|
def json_response(object, status = :ok)
|
||||||
render json: object, status: status
|
render json: object, status: status
|
||||||
end
|
end
|
||||||
@@ -1,2 +1,22 @@
|
|||||||
class HomiesController < ApplicationController::API
|
class HomiesController < ApplicationController
|
||||||
|
def index
|
||||||
|
json_response(Homie.all.order(:created_at))
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
homie = Homie.new(homie_params)
|
||||||
|
if homie.save
|
||||||
|
json_response(homie)
|
||||||
|
else
|
||||||
|
error_response(:bad_request)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def homie_params
|
||||||
|
params.require(:homie).permit(
|
||||||
|
:name,
|
||||||
|
:importance,
|
||||||
|
:about
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class MoneyMovesController < ApplicationController::API
|
class MoneyMovesController < ApplicationController
|
||||||
def list
|
def index
|
||||||
json_response(MoneyMove.find(:all).order(:created_at))
|
json_response(MoneyMove.find(:all).order(:created_at))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ class MoneyMovesController < ApplicationController::API
|
|||||||
end
|
end
|
||||||
|
|
||||||
def money_move_params
|
def money_move_params
|
||||||
params.require(:money_moves).permit(
|
params.require(:money_move).permit(
|
||||||
:description,
|
:description,
|
||||||
:amount,
|
:amount,
|
||||||
:from_homie_id,
|
:from_homie_id,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
class ApplicationRecord < ActiveRecord::Base
|
class ApplicationRecord < ActiveRecord::Base
|
||||||
self.abstract_class = true
|
self.abstract_class = true
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
class Homie < ApplicationRecord
|
class Homie < ApplicationRecord
|
||||||
has_many :money_moves_from, class_name: 'MoneyMove', column: :from_homie_id
|
has_many :money_moves_from, class_name: 'MoneyMove', foreign_key: :from_homie_id
|
||||||
has_many :money_moves_to, class_name: 'MoneyMove', column: :to_homie_id
|
has_many :money_moves_to, class_name: 'MoneyMove', foreign_key: :to_homie_id
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
resources :money_moves
|
constraints format: :json do
|
||||||
resources :homies
|
resources :money_moves
|
||||||
|
resources :homies
|
||||||
|
end
|
||||||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user