handle add/remove homies in backend

This commit is contained in:
Bilal
2020-09-12 01:26:56 +03:00
parent 2a5ffac5b9
commit 013e4d144d
6 changed files with 42 additions and 20 deletions

View File

@@ -1,6 +1,6 @@
class ChipsController < ApplicationController class ChipsController < ApplicationController
def index 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 end
def create def create

View File

@@ -1,29 +1,39 @@
class HomiesController < ApplicationController class HomiesController < ApplicationController
def index def index
json_response(Homie.all.order(:created_at)) json_response(Homie.all.order(importance: :desc, name: :asc))
end end
def create def create
homie = Homie.new(homie_params) homie = Homie.new(homie_params)
if homie.save if homie.save
json_response(homie) index
else else
error_response(:bad_request) error_response(:bad_request)
end end
end end
def destroy
id = params[:id].to_i
if Homie.destroy(id)
index
else
error_response(:bad_request)
end
end
def cash def cash
importance = params[:importance].to_i importance = params[:importance].to_i
json_response(Homie.cash(importance)) json_response(Homie.cash(importance))
end end
private private
def homie_params
def homie_params
params.require(:homie).permit( params.require(:homie).permit(
:name, :name,
:importance, :importance,
:about :about,
:chip_id
) )
end end
end end

View File

@@ -1,13 +1,13 @@
class Homie < ApplicationRecord class Homie < ApplicationRecord
has_many :money_moves has_many :money_moves
belongs_to :chip
def self.cash(importance) 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| Homie.where(["importance > ?", importance]).map do |homie|
total = totals.fetch(homie.id, 0) total = totals.fetch(homie.id, 0)
{ homie: homie, amount: total } { homie: homie, amount: total }
end end
end end
end end

View File

@@ -0,0 +1,5 @@
class AddDefaultChipToHomies < ActiveRecord::Migration[5.2]
def change
add_reference :homies, :chip
end
end

View File

@@ -0,0 +1,5 @@
class ChangeDefaultValueForImportanceInHomies < ActiveRecord::Migration[5.2]
def change
change_column_default :homies, :importance, 100
end
end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -37,10 +37,12 @@ ActiveRecord::Schema.define(version: 2020_08_27_142428) do
create_table "homies", force: :cascade do |t| create_table "homies", force: :cascade do |t|
t.text "name", null: false t.text "name", null: false
t.integer "importance", default: 5, null: false t.integer "importance", default: 100, null: false
t.text "about" t.text "about"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.bigint "chip_id"
t.index ["chip_id"], name: "index_homies_on_chip_id"
end end
create_table "money_moves", force: :cascade do |t| create_table "money_moves", force: :cascade do |t|