Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

account store #22

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ab18041
Transaction History Store
patche-v Jan 21, 2020
d2152e5
Transaction History Store Insert
patche-v Jan 21, 2020
a89ce21
Transaction History Store GetTransactionsById
patche-v Jan 21, 2020
2bf4a20
Formatted code and updated variables
patche-v Jan 21, 2020
b2b953c
Updated queries
patche-v Jan 21, 2020
d321ce5
Renaming function arguments to GO convention
patche-v Jan 22, 2020
9cbeffa
Resolved variable types
patche-v Jan 22, 2020
3ad66c0
Added time.Now() in a variable
patche-v Jan 22, 2020
65fd542
account store
Jan 22, 2020
845e943
account-store
Jan 23, 2020
7e462dd
removed createdAt, updatedAt in function
Jan 23, 2020
41f2ff9
Changes to functions return types
patche-v Jan 29, 2020
a14b5f8
Merge branch 'account-store' of github.com:inplayer-org/backend-inter…
patche-v Jan 29, 2020
01f507b
Changed functions return types
patche-v Jan 29, 2020
a88709d
Delete transactionhistory.go
patche-v Jan 29, 2020
3c8fe17
Changed function return types
patche-v Jan 29, 2020
d0c0163
Merge branch 'account-store' of github.com:inplayer-org/backend-inter…
patche-v Jan 29, 2020
6d0af18
Added lastInsertId in InsertAccount func
patche-v Jan 29, 2020
c3beb6a
Changed return values
patche-v Jan 29, 2020
bc98f2d
Account Store Tests
patche-v Jan 30, 2020
278a4d6
account store test (testing merge)
Jan 30, 2020
ac80a38
Merge branch 'account-store-test' of github.com:inplayer-org/backend-…
Jan 30, 2020
221bdb5
account store test (merge files)
Jan 30, 2020
3102569
account store test (marge files 2)
Jan 30, 2020
4ee7b09
Merge branch 'account-store-test' into account-store
patche-v Jan 30, 2020
5da08f9
Merge branch 'account-store-test' of github.com:inplayer-org/backend-…
patche-v Jan 30, 2020
7fbe28a
Merge on account-store-test
patche-v Jan 30, 2020
96315da
Changed return values in accountstore and modified tests
patche-v Feb 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions BankAccount/pkg/store/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package store

import (
"database/sql"
"time"

"bankacc/pkg/entities"
)

type AccountStore interface {
InsertAccount(userId int, balance float64, currency string) (*entities.Account, error)
GetAccountsByUserId(userId int) ([]*entities.Account, error)
UpdateAccount(id int, userId int, balance float64, currency string, updatedAt time.Time) (*entities.Account, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@F1c1cA you can remove updatedAt as an argument here, since it's not used.

CloseAccount(id int, userId int, updatedAt time.Time) (*entities.Account, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@F1c1cA here as well.

}

type AccountModel struct {
Db *sql.DB
}

func NewAccountStoreModel(db *sql.DB) *AccountModel {
return &AccountModel{
Db: db,
}
}

func (store *AccountModel) InsertAccount(userId int, balance float64, currency string) (*entities.Account, error) {
createdAt := time.Now()
updatedAt := time.Now()

result, err := store.Db.Exec("INSERT INTO Account (user_id, balance, currency, status, created_at, updated_at) VALUES(?, ?, ?, 1, ?, ?)", userId, balance, currency, createdAt, updatedAt)
if err != nil {
return nil, err
}
res, err := result.LastInsertId()
if err != nil {
return nil, err
}
account := entities.Account{
Id: int(res),
UserId: userId,
Balance: balance,
Currency: currency,
Status: true,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
return &account, nil
}

func (store *AccountModel) GetAccountsByUserId(userId int) ([]*entities.Account, error) {
var accounts []*entities.Account
result, err := store.Db.Query("SELECT * FROM BankAccount.Account WHERE user_id=?", userId)
if err != nil {
return nil, err
}
var account entities.Account
for result.Next() {
err := result.Scan(&account.Id, &account.UserId, &account.Balance, &account.Currency, &account.Status, &account.CreatedAt, &account.UpdatedAt)
if err != nil {
return nil, err
}
accounts = append(accounts, &account)
}
return accounts, nil
}

func (store *AccountModel) UpdateAccount(id int, userId int, balance float64, currency string) (*entities.Account, error) {
updatedAt := time.Now()
_, err := store.Db.Exec("UPDATE BankAccount.Account SET balance = ?, currency =?, updated_at = ? WHERE user_id = ? AND id =?", balance, currency, updatedAt, userId, id)
if err != nil {
return nil, err
}
account := entities.Account{
Id: id,
UserId: userId,
Balance: balance,
Currency: currency,
Status: true,
}
return &account, nil
}

func (store *AccountModel) CloseAccount(id int, userId int) (*entities.Account, error) {
updatedAt := time.Now()
_, err := store.Db.Exec("UPDATE BankAccount.Account SET status = 0, updated_at = ? WHERE user_id = ? AND id = ?", updatedAt, userId, id)
if err != nil {
return nil, err
}
account := entities.Account{
Id: id,
UserId: userId,
Status: false,
UpdatedAt: updatedAt,
}
return &account, err
}
1 change: 0 additions & 1 deletion BankAccount/pkg/store/transactionhistory.go

This file was deleted.