Report ready

This commit is contained in:
Senad Uka
2019-06-22 06:17:48 +02:00
parent 0357df32a7
commit 857324cdc9
3 changed files with 24 additions and 2 deletions

View File

@@ -11,7 +11,12 @@ class HomiesController < ApplicationController
error_response(:bad_request) error_response(:bad_request)
end end
end end
def cash
json_response(Homie.cash(params[:importance]))
end
private
def homie_params def homie_params
params.require(:homie).permit( params.require(:homie).permit(
:name, :name,
@@ -19,4 +24,5 @@ class HomiesController < ApplicationController
:about :about
) )
end end
end end

View File

@@ -1,4 +1,15 @@
class Homie < ApplicationRecord class Homie < ApplicationRecord
has_many :money_moves_from, class_name: 'MoneyMove', foreign_key: :from_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 has_many :money_moves_to, class_name: 'MoneyMove', foreign_key: :to_homie_id
def self.cash(importance)
got = Homie.all.joins(:money_moves_to).group(:id).order(:id).sum(:amount)
owe = Homie.all.joins(:money_moves_from).group(:id).order(:id).sum(:amount)
result = []
Homie.where(["importance > ?", importance]).map do |homie|
total = got.fetch(homie.id, 0) - owe.fetch(homie.id,0)
{ homie: homie, amount: total }
end
end
end end

View File

@@ -1,7 +1,12 @@
Rails.application.routes.draw do Rails.application.routes.draw do
constraints format: :json do constraints format: :json do
resources :money_moves resources :money_moves
resources :homies resources :homies do
collection do
get 'cash'
end
end
end 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