<commit_before>package models

import (
	. "app/common"
	"app/services"
	"sync"
)

type UserBalance struct {
	Id      int        `json:"id"`
	Name    string     `json:"name"`
	Balance int        `json:"balance"`
	mu      sync.Mutex `json:"-"`
}

func (m *UserBalance) GetById(id int) *UserBalance {
	error := services.DB.Where("id = ?", id).First(m).Error
	if error != nil {
		Log.Printf("Unable to get user balance!  %s", error, id)
	}

	return m
}

func (m *UserBalance) AddAmount(summ int) (*UserBalance, error) {
	m.mu.Lock()
	defer m.mu.Unlock()
	amount := m.Balance + summ
	error := services.DB.Model(m).Update("balance", amount).Error
	if error != nil {
		Log.Printf("Unable to update user balance!  %s", error, summ, m, amount)
	}
	updated := m.GetById(m.Id)

	return updated, error
}

func (m *UserBalance) WithdrawAmount(summ int) (*UserBalance, error) {
	var amount int
	var error error
	m.mu.Lock()
	defer m.mu.Unlock()
	if m.Balance < summ {
		amount = m.Balance
	} else {
		amount = m.Balance - summ
	}
	error = services.DB.Model(m).Update("balance", amount).Error
	if error != nil {
		Log.Printf("Unable to withdraw user balance!  %s", error, summ, m)
	}
	updated := m.GetById(m.Id)<commit_msg>Update user_balance.go<commit_after>package models

import (
	. "app/common"
	"app/services"
)

type UserBalance struct {
	Id      int        `json:"id"`
	Name    string     `json:"name"`
	Balance int        `json:"balance"`
}

func (m *UserBalance) GetById(id int) *UserBalance {
	error := services.DB.Where("id = ?", id).First(m).Error
	if error != nil {
		Log.Printf("Unable to get user balance!  %s", error, id)
	}

	return m
}

func (m *UserBalance) AddAmount(summ int) (*UserBalance, error) {
	amount := m.Balance + summ
	error := services.DB.Model(m).Update("balance", amount).Error
	if error != nil {
		Log.Printf("Unable to update user balance!  %s", error, summ, m, amount)
	}
	updated := m.GetById(m.Id)

	return updated, error
}

func (m *UserBalance) WithdrawAmount(summ int) (*UserBalance, error) {
	var amount int
	var error error
	if m.Balance < summ {
		amount = m.Balance
	} else {
		amount = m.Balance - summ
	}
	error = services.DB.Model(m).Update("balance", amount).Error
	if error != nil {
		Log.Printf("Unable to withdraw user balance!  %s", error, summ, m)
	}
	updated := m.GetById(m.Id)
{'fraction_non_alphanumeric': 0.1560344827586207, 'fraction_numerical': 0.0, 'mean_word_length': 3.498062015503876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6237002', 'n_tokens_mistral': 851, 'n_tokens_neox': 856, 'n_words': 228}