-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.go
80 lines (67 loc) · 2.41 KB
/
accounts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package accounts
import (
"time"
"github.com/masudur-rahman/expense-tracker-bot/infra/logr"
"github.com/masudur-rahman/expense-tracker-bot/models"
isql "github.com/masudur-rahman/database/sql"
)
type SQLAccountsRepository struct {
db isql.Database
logger logr.Logger
}
func NewSQLAccountsRepository(db isql.Database, logger logr.Logger) *SQLAccountsRepository {
return &SQLAccountsRepository{
db: db.Table("account"),
logger: logger,
}
}
func (a *SQLAccountsRepository) GetAccountByShortName(userID int64, shortName string) (*models.Account, error) {
a.logger.Infow("get account by account id", "account id", shortName)
var acc models.Account
found, err := a.db.FindOne(&acc, models.Account{ShortName: shortName, UserID: userID})
if err != nil {
return nil, err
}
if !found {
return nil, models.ErrAccountNotFound{AccID: shortName}
}
return &acc, nil
}
func (a *SQLAccountsRepository) ListAccounts(userID int64) ([]models.Account, error) {
a.logger.Infow("list accounts")
accs := make([]models.Account, 0)
err := a.db.FindMany(&accs, models.Account{UserID: userID})
return accs, err
}
func (a *SQLAccountsRepository) ListAccountsByType(userID int64, typ models.AccountType) ([]models.Account, error) {
a.logger.Infow("list accounts", "type", typ)
accs := make([]models.Account, 0)
err := a.db.FindMany(&accs, models.Account{UserID: userID, Type: typ})
return accs, err
}
func (a *SQLAccountsRepository) AddNewAccount(account *models.Account) error {
a.logger.Infow("add new account", "name", account.Name)
_, err := a.GetAccountByShortName(account.UserID, account.ShortName)
if err == nil {
return models.ErrAccountAlreadyExist{ShortName: account.ShortName}
} else if !models.IsErrNotFound(err) {
return err
}
_, err = a.db.InsertOne(account)
return err
}
func (a *SQLAccountsRepository) UpdateAccountBalance(userID int64, shortName string, txnAmount float64) error {
a.logger.Infow("updating account balance", "account", shortName)
acc, err := a.GetAccountByShortName(userID, shortName)
if err != nil {
return err
}
acc.Balance += txnAmount
acc.LastTxnAmount = txnAmount
acc.LastTxnTimestamp = time.Now().Unix()
return a.db.ID(acc.ID).MustCols("balance").UpdateOne(acc)
}
func (a *SQLAccountsRepository) DeleteAccount(userID int64, shortName string) error {
a.logger.Infow("deleting account", "account", shortName)
return a.db.DeleteOne(models.Account{ShortName: shortName, UserID: userID})
}