From 0357df32a78499c41694a464e37adbcd3fc7778a Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Fri, 21 Jun 2019 20:01:43 +0200 Subject: [PATCH] CRUD API ready --- app/controllers/application_controller.rb | 1 + .../concerns/response.rb} | 4 ++++ app/controllers/homies_controller.rb | 22 ++++++++++++++++++- app/controllers/money_moves_controller.rb | 6 ++--- app/models/application_record.rb | 1 + app/models/concerns/error_response.rb | 0 app/models/homie.rb | 4 ++-- config/routes.rb | 6 +++-- 8 files changed, 36 insertions(+), 8 deletions(-) rename app/{models/concerns/json_response.rb => controllers/concerns/response.rb} (58%) delete mode 100644 app/models/concerns/error_response.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4ac8823..905bc14 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,3 @@ class ApplicationController < ActionController::API + include Response end diff --git a/app/models/concerns/json_response.rb b/app/controllers/concerns/response.rb similarity index 58% rename from app/models/concerns/json_response.rb rename to app/controllers/concerns/response.rb index cfb0caa..d92f2f2 100644 --- a/app/models/concerns/json_response.rb +++ b/app/controllers/concerns/response.rb @@ -1,4 +1,8 @@ module Response + def error_response(status) + render nothing: true, status: status + end + def json_response(object, status = :ok) render json: object, status: status end diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb index 33e6db4..18e71f4 100644 --- a/app/controllers/homies_controller.rb +++ b/app/controllers/homies_controller.rb @@ -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 diff --git a/app/controllers/money_moves_controller.rb b/app/controllers/money_moves_controller.rb index 2218cf2..fe8e3a8 100644 --- a/app/controllers/money_moves_controller.rb +++ b/app/controllers/money_moves_controller.rb @@ -1,5 +1,5 @@ -class MoneyMovesController < ApplicationController::API - def list +class MoneyMovesController < ApplicationController + def index json_response(MoneyMove.find(:all).order(:created_at)) end @@ -13,7 +13,7 @@ class MoneyMovesController < ApplicationController::API end def money_move_params - params.require(:money_moves).permit( + params.require(:money_move).permit( :description, :amount, :from_homie_id, diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 10a4cba..0e0f662 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,3 +1,4 @@ class ApplicationRecord < ActiveRecord::Base self.abstract_class = true + end diff --git a/app/models/concerns/error_response.rb b/app/models/concerns/error_response.rb deleted file mode 100644 index e69de29..0000000 diff --git a/app/models/homie.rb b/app/models/homie.rb index d4af353..867a8b2 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -1,4 +1,4 @@ class Homie < ApplicationRecord - has_many :money_moves_from, class_name: 'MoneyMove', column: :from_homie_id - has_many :money_moves_to, class_name: 'MoneyMove', column: :to_homie_id + has_many :money_moves_from, class_name: 'MoneyMove', foreign_key: :from_homie_id + has_many :money_moves_to, class_name: 'MoneyMove', foreign_key: :to_homie_id end diff --git a/config/routes.rb b/config/routes.rb index cfbefac..f4c6135 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ Rails.application.routes.draw do - resources :money_moves - resources :homies + constraints format: :json do + resources :money_moves + resources :homies + end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end