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
|
||||
|
||||
5
db/migrate/20200911162613_add_default_chip_to_homies.rb
Normal file
5
db/migrate/20200911162613_add_default_chip_to_homies.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class AddDefaultChipToHomies < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_reference :homies, :chip
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class ChangeDefaultValueForImportanceInHomies < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
change_column_default :homies, :importance, 100
|
||||
end
|
||||
end
|
||||
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2020_08_27_142428) do
|
||||
ActiveRecord::Schema.define(version: 2020_09_11_171657) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -37,10 +37,12 @@ ActiveRecord::Schema.define(version: 2020_08_27_142428) do
|
||||
|
||||
create_table "homies", force: :cascade do |t|
|
||||
t.text "name", null: false
|
||||
t.integer "importance", default: 5, null: false
|
||||
t.integer "importance", default: 100, null: false
|
||||
t.text "about"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.bigint "chip_id"
|
||||
t.index ["chip_id"], name: "index_homies_on_chip_id"
|
||||
end
|
||||
|
||||
create_table "money_moves", force: :cascade do |t|
|
||||
|
||||
Reference in New Issue
Block a user