Skip to content

Instantly share code, notes, and snippets.

@himangSharatun
Created February 9, 2019 10:10
Show Gist options
  • Save himangSharatun/a12f28cecb816a91e889dd5ac7ddf3b5 to your computer and use it in GitHub Desktop.
Save himangSharatun/a12f28cecb816a91e889dd5ac7ddf3b5 to your computer and use it in GitHub Desktop.
class UserController < ApplicationController
def index
@user = User.all
render json: @user, each_serializer: UserSerializer
end
def mass_update
update_using_csv(params[:csv])
message = Api::Message.new(:succesfully_updated)
render json: message, serializer: MessageSerializer
end
private
def update_using_csv(file)
decoded_file = Base64.decode64(file)
file = decoded_file.encode('utf-8', invalid: :replace, undef: :replace, replace: '')
CSV.parse(file, headers: true).each do |row|
update_balance(row["username"], row["amount"])
end
end
def update_balance(username, amount)
user = User.find_by(username: username.to_s)
user.balance += amount.to_i
user.save!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment