class HomiesController < ApplicationController def index 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 homie = Homie.new(homie_params) if homie.save index else error_response(:bad_request) end end def destroy id = params[:homie_id].to_i if Homie.destroy(id) index else error_response(:bad_request) end end def info importance = params[:importance].present? ? params[:importance].to_i : -1 gang = params[:gang_id] json_response(Homie.info(importance, gang)) end def settle homie_id = params[:homie_id].to_i amount = params[:amount].present? ? params[:amount].to_d : nil homie = Homie.find(homie_id) if homie.settle(amount) money_moves = MoneyMove.where(homie_id: params[:homie_id].to_i).all.order(created_at: :desc) json_response money_moves else error_response :bad_request end rescue StandardError error_response :bad_request end private def homie_params params.require(:homie).permit( :name, :importance, :about, :gang_id ) end end