From e65f352108c4794dd7af11770625528f81750078 Mon Sep 17 00:00:00 2001 From: Bilal Date: Thu, 8 Oct 2020 01:13:57 +0300 Subject: [PATCH] move homies under gang --- app/controllers/gangs_controller.rb | 13 +++++++++---- app/controllers/homies_controller.rb | 7 +++++-- app/models/homie.rb | 6 ++++-- config/routes.rb | 19 ++++++++++--------- .../20201007182916_add_name_to_gangs.rb | 1 + db/schema.rb | 1 + 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/app/controllers/gangs_controller.rb b/app/controllers/gangs_controller.rb index 82204a4..dd70326 100644 --- a/app/controllers/gangs_controller.rb +++ b/app/controllers/gangs_controller.rb @@ -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, diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb index 66ad232..ff64f42 100644 --- a/app/controllers/homies_controller.rb +++ b/app/controllers/homies_controller.rb @@ -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 diff --git a/app/models/homie.rb b/app/models/homie.rb index c4e8e1b..9ad4538 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -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 } diff --git a/config/routes.rb b/config/routes.rb index ea639b5..06d8614 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,19 +1,20 @@ Rails.application.routes.draw do constraints format: :json 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 :work resources :chips, only: %i[index create 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 diff --git a/db/migrate/20201007182916_add_name_to_gangs.rb b/db/migrate/20201007182916_add_name_to_gangs.rb index 26c258e..379adb5 100644 --- a/db/migrate/20201007182916_add_name_to_gangs.rb +++ b/db/migrate/20201007182916_add_name_to_gangs.rb @@ -1,5 +1,6 @@ class AddNameToGangs < ActiveRecord::Migration[5.2] def change add_column :gangs, :name, :text + add_column :gangs, :about, :text end end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index dc50711..80897d6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -44,6 +44,7 @@ ActiveRecord::Schema.define(version: 2020_10_07_213411) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "name" + t.text "about" end create_table "homies", force: :cascade do |t|