handle chip and chip values operations
This commit is contained in:
40
app/controllers/chip_values_controller.rb
Normal file
40
app/controllers/chip_values_controller.rb
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
class ChipValuesController < ApplicationController
|
||||||
|
|
||||||
|
def create
|
||||||
|
if ChipValue.create_full_chip_value(chip_value_params)
|
||||||
|
json_response all_chips_response
|
||||||
|
else
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
rescue StandardError
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
if ChipValue.update_full_chip_value(chip_value_params)
|
||||||
|
json_response all_chips_response
|
||||||
|
end
|
||||||
|
rescue StandardError
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
if ChipValue.destroy_full_chip_value(params[:id])
|
||||||
|
json_response all_chips_response
|
||||||
|
else
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
rescue StandardError
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def chip_value_params
|
||||||
|
params.require(:chip_value).permit(:id, :base_chip_id, :secondary_chip_id, :value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def all_chips_response
|
||||||
|
Chip.all.order(:name).to_json(include: :base_chip_values)
|
||||||
|
end
|
||||||
|
end
|
||||||
29
app/controllers/chips_controller.rb
Normal file
29
app/controllers/chips_controller.rb
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
class ChipsController < ApplicationController
|
||||||
|
def index
|
||||||
|
json_response Chip.all.order(:name).to_json(include: :base_chip_values)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
chip = Chip.new(chip_params)
|
||||||
|
if chip.save
|
||||||
|
json_response chip
|
||||||
|
else
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
chip_id = params[:id]
|
||||||
|
if chip_id.present? && Chip.destroy(chip_id)
|
||||||
|
index
|
||||||
|
else
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def chip_params
|
||||||
|
params.require(:chip).permit(:id, :name, :symbol, :enabled)
|
||||||
|
end
|
||||||
|
end
|
||||||
23
app/models/chip.rb
Normal file
23
app/models/chip.rb
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
class Chip < ApplicationRecord
|
||||||
|
has_many :base_chip_values, class_name: "ChipValue", foreign_key: "base_chip_id", dependent: :delete_all
|
||||||
|
has_many :secondary_chip_values, class_name: "ChipValue", foreign_key: "secondary_chip_id", dependent: :delete_all
|
||||||
|
|
||||||
|
validates :name, uniqueness: true
|
||||||
|
validates :name, :symbol, presence: true
|
||||||
|
|
||||||
|
after_create :add_chip_values
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# When new chip(currency) is added, add new record to the chip_values table for every existing chip(currency)
|
||||||
|
def add_chip_values
|
||||||
|
Chip.all.each do |chip|
|
||||||
|
next if chip.name == name
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
ChipValue.new(base_chip: self, secondary_chip: chip, value: 0).save
|
||||||
|
ChipValue.new(base_chip: chip, secondary_chip: self, value: 0).save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
53
app/models/chip_value.rb
Normal file
53
app/models/chip_value.rb
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
class ChipValue < ApplicationRecord
|
||||||
|
belongs_to :base_chip, class_name: 'Chip'
|
||||||
|
belongs_to :secondary_chip, class_name: 'Chip'
|
||||||
|
|
||||||
|
validates :base_chip_id, uniqueness: { scope: :secondary_chip_id }
|
||||||
|
|
||||||
|
def self.create_full_chip_value(params)
|
||||||
|
base_chip_id = params[:base_chip_id]
|
||||||
|
secondary_chip_id = params[:secondary_chip_id]
|
||||||
|
value = params[:value].to_f
|
||||||
|
|
||||||
|
chips = Chip.where(id: [base_chip_id, secondary_chip_id])
|
||||||
|
base_chip = chips.first
|
||||||
|
secondary_chip = chips.second
|
||||||
|
|
||||||
|
mirrored_value = value.zero? ? 0 : (1 / value)
|
||||||
|
|
||||||
|
base_chip_value = ChipValue.new(base_chip: base_chip, secondary_chip: secondary_chip, value: value)
|
||||||
|
mirrored_chip_value = ChipValue.new(base_chip: secondary_chip, secondary_chip: base_chip, value: mirrored_value)
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
base_chip_value.save
|
||||||
|
mirrored_chip_value.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.update_full_chip_value(params)
|
||||||
|
id = params[:id]
|
||||||
|
new_value = params[:value].to_f
|
||||||
|
mirrored_value = new_value.zero? ? 0 : (1 / new_value)
|
||||||
|
|
||||||
|
chip_value = ChipValue.find(id)
|
||||||
|
mirrored_chip_value = ChipValue.where(base_chip_id: chip_value.secondary_chip_id, secondary_chip_id: chip_value.base_chip_id).first
|
||||||
|
|
||||||
|
chip_value.value = new_value
|
||||||
|
mirrored_chip_value.value = mirrored_value
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
chip_value.save
|
||||||
|
mirrored_chip_value.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.destroy_full_chip_value(id)
|
||||||
|
chip_value = ChipValue.find(id)
|
||||||
|
mirrored_chip_value = ChipValue.where(base_chip_id: chip_value.secondary_chip_id, secondary_chip_id: chip_value.base_chip_id).first
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
chip_value.destroy
|
||||||
|
mirrored_chip_value.destroy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
constraints format: :json do
|
constraints format: :json do
|
||||||
resources :money_moves
|
resources :money_moves
|
||||||
resources :homies do
|
resources :chips, only: %i[index create destroy]
|
||||||
collection do
|
resources :chip_values, only: %i[create update destroy]
|
||||||
|
resources :homies do
|
||||||
|
collection do
|
||||||
get 'cash'
|
get 'cash'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
12
db/migrate/20200827113649_create_chips.rb
Normal file
12
db/migrate/20200827113649_create_chips.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
class CreateChips < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :chips do |t|
|
||||||
|
t.text :name, null: false
|
||||||
|
t.text :symbol, null: false
|
||||||
|
t.boolean :enabled, default: true
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :chips, :name, unique: true
|
||||||
|
end
|
||||||
|
end
|
||||||
12
db/migrate/20200827142428_create_chip_values.rb
Normal file
12
db/migrate/20200827142428_create_chip_values.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
class CreateChipValues < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :chip_values do |t|
|
||||||
|
t.references :base_chip, foreign_key: { to_table: :chips }
|
||||||
|
t.references :secondary_chip, foreign_key: { to_table: :chips }
|
||||||
|
t.decimal :value, precision: 12, scale: 3, null: false
|
||||||
|
t.timestamps
|
||||||
|
|
||||||
|
t.index [:base_chip_id, :secondary_chip_id], unique: true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
24
db/schema.rb
24
db/schema.rb
@@ -10,11 +10,31 @@
|
|||||||
#
|
#
|
||||||
# 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: 2019_06_20_200006) do
|
ActiveRecord::Schema.define(version: 2020_08_27_142428) 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"
|
||||||
|
|
||||||
|
create_table "chip_values", force: :cascade do |t|
|
||||||
|
t.bigint "base_chip_id"
|
||||||
|
t.bigint "secondary_chip_id"
|
||||||
|
t.decimal "value", precision: 12, scale: 3, null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["base_chip_id", "secondary_chip_id"], name: "index_chip_values_on_base_chip_id_and_secondary_chip_id", unique: true
|
||||||
|
t.index ["base_chip_id"], name: "index_chip_values_on_base_chip_id"
|
||||||
|
t.index ["secondary_chip_id"], name: "index_chip_values_on_secondary_chip_id"
|
||||||
|
end
|
||||||
|
|
||||||
|
create_table "chips", force: :cascade do |t|
|
||||||
|
t.text "name", null: false
|
||||||
|
t.text "symbol", null: false
|
||||||
|
t.boolean "enabled", default: true
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["name"], name: "index_chips_on_name", unique: true
|
||||||
|
end
|
||||||
|
|
||||||
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: 5, null: false
|
||||||
@@ -31,4 +51,6 @@ ActiveRecord::Schema.define(version: 2019_06_20_200006) do
|
|||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_foreign_key "chip_values", "chips", column: "base_chip_id"
|
||||||
|
add_foreign_key "chip_values", "chips", column: "secondary_chip_id"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user