Created
February 9, 2019 10:10
-
-
Save himangSharatun/a12f28cecb816a91e889dd5ac7ddf3b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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