Exemplo n.º 1
0
Arquivo: bux.go Projeto: tslnc04/go-1
/*
Write a time-stamped entry to user's bux log.  This function is
public because sometimes a log entry unrelated to adding or subtracting
SkilBux is needed.
*/
func Log(user string, amount interface{}, comment string) error {
	switch v := amount.(type) {
	case int:
		amount = strconv.Itoa(v)
	}
	if !h.IsUser(user) {
		return fmt.Errorf(NOTUSER)
	}
	return log(user, amount.(string), comment)
}
Exemplo n.º 2
0
Arquivo: bux.go Projeto: tslnc04/go-1
/*
Debit the current user and credit to target user.  Only positive
numbers allowed.  Not designed for high volume usage (no transactional
locking). Transfers to the same person from blocked.
*/
func Transfer(to string, amount interface{}, comment string) error {
	var err error
	if !h.IsUser(to) {
		return fmt.Errorf(NOTUSER)
	}
	switch v := amount.(type) {
	case string:
		if amount, err = strconv.Atoi(v); err != nil {
			return err
		}
	}
	current, err := u.Current()
	if err != nil {
		return err
	}
	from := current.Username
	if to == from {
		return fmt.Errorf(SAME)
	}
	has, err := read(from)
	if err != nil {
		return err
	}
	if amount.(int) < 1 {
		return fmt.Errorf(NEEDSPOS)
	}
	if amount.(int) > has {
		return fmt.Errorf(NOTENOUGH)
	}
	// TODO make Add + Sub atomic transaction (but will need db)
	if err = Adjust(amount.(int), to, "from "+from+": "+comment); err != nil {
		return err
	}
	if err = Adjust(-amount.(int), from, "to "+to+": "+comment); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 3
0
Arquivo: bux.go Projeto: tslnc04/go-1
// Returns Bux for given user or 0 if error. Always check for error.
func Get(u string) (int, error) {
	if !h.IsUser(u) {
		return 0, fmt.Errorf(NOTUSER)
	}
	return read(u)
}