move homies under gang

This commit is contained in:
Bilal
2020-10-08 01:13:57 +03:00
parent 51d8a369a9
commit e65f352108
6 changed files with 30 additions and 17 deletions

View File

@@ -5,12 +5,16 @@ class GangsController < ApplicationController
error_response :bad_request
end
def show
if Gang.count.zero?
Gang.create.save
def create
gang = Gang.create(gang_params)
if gang.save
json_response Gang.all
else
error_response :bad_request
end
json_response Gang.first
rescue StandardError
error_response :bad_request
end
def update
@@ -26,6 +30,7 @@ class GangsController < ApplicationController
def gang_params
params.require(:gang).permit :name,
:about,
:chip_name,
:chip_code,
:chip_symbol,

View File

@@ -1,6 +1,8 @@
class HomiesController < ApplicationController
def index
json_response(Homie.all.order(importance: :desc, name: :asc))
homies = Homie.where(gang: params[:gang_id]).order(importance: :desc, name: :asc)
# json_response(Homie.all.order(importance: :desc, name: :asc))
json_response homies
end
def create
@@ -23,7 +25,8 @@ class HomiesController < ApplicationController
def info
importance = params[:importance].present? ? params[:importance].to_i : -1
json_response(Homie.info(importance))
gang = params[:gang_id]
json_response(Homie.info(importance, gang))
end
def settle

View File

@@ -4,11 +4,13 @@ class Homie < ApplicationRecord
belongs_to :gang
def self.info(importance)
def self.info(importance, gang)
# TODO: This can be improved
cash_totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount)
work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount)
Homie.where(["importance > ?", importance]).map do |homie|
Homie.where(['importance > ? and gang_id = ?', importance, gang]).map do |homie|
cash_total = cash_totals.fetch(homie.id, 0)
work_total = work_totals.fetch(homie.id, 0)
{ homie: homie, amount: cash_total, work: work_total }