2020-10-07 18:26:59 +03:00
|
|
|
class GangsController < ApplicationController
|
2020-10-21 15:11:20 +02:00
|
|
|
include ActionController::MimeResponds
|
|
|
|
|
before_action :set_gang, only: [:update, :backup]
|
|
|
|
|
|
2020-10-07 22:47:19 +03:00
|
|
|
def index
|
|
|
|
|
json_response Gang.all
|
|
|
|
|
rescue StandardError
|
|
|
|
|
error_response :bad_request
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-08 01:13:57 +03:00
|
|
|
def create
|
|
|
|
|
gang = Gang.create(gang_params)
|
|
|
|
|
if gang.save
|
|
|
|
|
json_response Gang.all
|
|
|
|
|
else
|
|
|
|
|
error_response :bad_request
|
2020-10-07 18:26:59 +03:00
|
|
|
end
|
|
|
|
|
|
2020-10-08 01:13:57 +03:00
|
|
|
rescue StandardError
|
|
|
|
|
error_response :bad_request
|
2020-10-07 18:26:59 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2020-10-21 15:11:20 +02:00
|
|
|
@gang.update!(gang_params)
|
2020-10-07 18:26:59 +03:00
|
|
|
json_response onboarded: true
|
2020-10-21 15:11:20 +02:00
|
|
|
rescue StandardError => err
|
|
|
|
|
Rails.logger.error(err)
|
2020-10-10 01:18:57 +03:00
|
|
|
error_response :bad_request
|
2020-10-07 18:26:59 +03:00
|
|
|
end
|
|
|
|
|
|
2020-10-21 15:11:20 +02:00
|
|
|
def backup
|
|
|
|
|
csv_string = CSV.generate do |csv|
|
|
|
|
|
csv << [:homie_name, :money_amount, :work_amount, :description, :date, :deleted]
|
|
|
|
|
@gang.money_moves.each do |money_move|
|
|
|
|
|
csv << money_move.to_csv_row
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@gang.works.each do |work|
|
|
|
|
|
csv << work.to_csv_row
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
send_data csv_string
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-07 18:26:59 +03:00
|
|
|
private
|
|
|
|
|
|
2020-10-21 15:11:20 +02:00
|
|
|
def set_gang
|
|
|
|
|
if params[:gang_id] || params[:id]
|
|
|
|
|
@gang = Gang.find(params[:id] || params[:gang_id])
|
|
|
|
|
else
|
|
|
|
|
error_response :bad_request
|
|
|
|
|
end
|
|
|
|
|
rescue StandardError => err
|
|
|
|
|
Rails.logger.error(err)
|
|
|
|
|
error_response :bad_request
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-07 18:26:59 +03:00
|
|
|
def gang_params
|
2020-10-07 22:47:19 +03:00
|
|
|
params.require(:gang).permit :name,
|
2020-10-08 01:13:57 +03:00
|
|
|
:about,
|
2020-10-07 22:47:19 +03:00
|
|
|
:chip_name,
|
2020-10-07 18:26:59 +03:00
|
|
|
:chip_code,
|
|
|
|
|
:chip_symbol,
|
|
|
|
|
:chip_scale,
|
|
|
|
|
:chip_prefixed
|
|
|
|
|
end
|
|
|
|
|
end
|