handle add/remove homies in backend
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
class ChipsController < ApplicationController
|
||||
def index
|
||||
json_response Chip.all.order(:name).to_json(include: :base_chip_values)
|
||||
json_response Chip.where(enabled: true).order(:name).to_json(include: :base_chip_values)
|
||||
end
|
||||
|
||||
def create
|
||||
|
||||
@@ -1,29 +1,39 @@
|
||||
class HomiesController < ApplicationController
|
||||
def index
|
||||
json_response(Homie.all.order(:created_at))
|
||||
json_response(Homie.all.order(importance: :desc, name: :asc))
|
||||
end
|
||||
|
||||
def create
|
||||
homie = Homie.new(homie_params)
|
||||
if homie.save
|
||||
json_response(homie)
|
||||
else
|
||||
error_response(:bad_request)
|
||||
end
|
||||
homie = Homie.new(homie_params)
|
||||
if homie.save
|
||||
index
|
||||
else
|
||||
error_response(:bad_request)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def destroy
|
||||
id = params[:id].to_i
|
||||
if Homie.destroy(id)
|
||||
index
|
||||
else
|
||||
error_response(:bad_request)
|
||||
end
|
||||
end
|
||||
|
||||
def cash
|
||||
importance = params[:importance].to_i
|
||||
json_response(Homie.cash(importance))
|
||||
end
|
||||
|
||||
private
|
||||
def homie_params
|
||||
|
||||
def homie_params
|
||||
params.require(:homie).permit(
|
||||
:name,
|
||||
:importance,
|
||||
:about
|
||||
:name,
|
||||
:importance,
|
||||
:about,
|
||||
:chip_id
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
class Homie < ApplicationRecord
|
||||
has_many :money_moves
|
||||
belongs_to :chip
|
||||
|
||||
def self.cash(importance)
|
||||
totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount)
|
||||
totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount)
|
||||
|
||||
result = []
|
||||
Homie.where(["importance > ?", importance]).map do |homie|
|
||||
total = totals.fetch(homie.id, 0)
|
||||
{ homie: homie, amount: total }
|
||||
total = totals.fetch(homie.id, 0)
|
||||
{ homie: homie, amount: total }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user