move homies under gang
This commit is contained in:
@@ -5,12 +5,16 @@ class GangsController < ApplicationController
|
|||||||
error_response :bad_request
|
error_response :bad_request
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def create
|
||||||
if Gang.count.zero?
|
gang = Gang.create(gang_params)
|
||||||
Gang.create.save
|
if gang.save
|
||||||
|
json_response Gang.all
|
||||||
|
else
|
||||||
|
error_response :bad_request
|
||||||
end
|
end
|
||||||
|
|
||||||
json_response Gang.first
|
rescue StandardError
|
||||||
|
error_response :bad_request
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@@ -26,6 +30,7 @@ class GangsController < ApplicationController
|
|||||||
|
|
||||||
def gang_params
|
def gang_params
|
||||||
params.require(:gang).permit :name,
|
params.require(:gang).permit :name,
|
||||||
|
:about,
|
||||||
:chip_name,
|
:chip_name,
|
||||||
:chip_code,
|
:chip_code,
|
||||||
:chip_symbol,
|
:chip_symbol,
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
class HomiesController < ApplicationController
|
class HomiesController < ApplicationController
|
||||||
def index
|
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
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@@ -23,7 +25,8 @@ class HomiesController < ApplicationController
|
|||||||
|
|
||||||
def info
|
def info
|
||||||
importance = params[:importance].present? ? params[:importance].to_i : -1
|
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
|
end
|
||||||
|
|
||||||
def settle
|
def settle
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ class Homie < ApplicationRecord
|
|||||||
|
|
||||||
belongs_to :gang
|
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)
|
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)
|
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)
|
cash_total = cash_totals.fetch(homie.id, 0)
|
||||||
work_total = work_totals.fetch(homie.id, 0)
|
work_total = work_totals.fetch(homie.id, 0)
|
||||||
{ homie: homie, amount: cash_total, work: work_total }
|
{ homie: homie, amount: cash_total, work: work_total }
|
||||||
|
|||||||
@@ -1,19 +1,20 @@
|
|||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
constraints format: :json do
|
constraints format: :json do
|
||||||
scope :api do
|
scope :api do
|
||||||
resources :gangs, only: [:index, :show, :update]
|
resources :gangs, only: [:index, :create, :update] do
|
||||||
|
resources :homies, param: :homie_id do
|
||||||
|
collection do
|
||||||
|
get 'info'
|
||||||
|
end
|
||||||
|
member do
|
||||||
|
post 'settle'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
resources :money_moves
|
resources :money_moves
|
||||||
resources :work
|
resources :work
|
||||||
resources :chips, only: %i[index create destroy]
|
resources :chips, only: %i[index create destroy]
|
||||||
resources :chip_values, only: %i[create update destroy]
|
resources :chip_values, only: %i[create update destroy]
|
||||||
resources :homies, param: :homie_id do
|
|
||||||
collection do
|
|
||||||
get 'info'
|
|
||||||
end
|
|
||||||
member do
|
|
||||||
post 'settle'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
class AddNameToGangs < ActiveRecord::Migration[5.2]
|
class AddNameToGangs < ActiveRecord::Migration[5.2]
|
||||||
def change
|
def change
|
||||||
add_column :gangs, :name, :text
|
add_column :gangs, :name, :text
|
||||||
|
add_column :gangs, :about, :text
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -44,6 +44,7 @@ ActiveRecord::Schema.define(version: 2020_10_07_213411) do
|
|||||||
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.text "name"
|
t.text "name"
|
||||||
|
t.text "about"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "homies", force: :cascade do |t|
|
create_table "homies", force: :cascade do |t|
|
||||||
|
|||||||
Reference in New Issue
Block a user