-
Notifications
You must be signed in to change notification settings - Fork 0
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
Closed
account store #22
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 d2152e5
Transaction History Store Insert
patche-v a89ce21
Transaction History Store GetTransactionsById
patche-v 2bf4a20
Formatted code and updated variables
patche-v b2b953c
Updated queries
patche-v d321ce5
Renaming function arguments to GO convention
patche-v 9cbeffa
Resolved variable types
patche-v 3ad66c0
Added time.Now() in a variable
patche-v 65fd542
account store
845e943
account-store
7e462dd
removed createdAt, updatedAt in function
41f2ff9
Changes to functions return types
patche-v a14b5f8
Merge branch 'account-store' of github.com:inplayer-org/backend-inter…
patche-v 01f507b
Changed functions return types
patche-v a88709d
Delete transactionhistory.go
patche-v 3c8fe17
Changed function return types
patche-v d0c0163
Merge branch 'account-store' of github.com:inplayer-org/backend-inter…
patche-v 6d0af18
Added lastInsertId in InsertAccount func
patche-v c3beb6a
Changed return values
patche-v bc98f2d
Account Store Tests
patche-v 278a4d6
account store test (testing merge)
ac80a38
Merge branch 'account-store-test' of github.com:inplayer-org/backend-…
221bdb5
account store test (merge files)
3102569
account store test (marge files 2)
4ee7b09
Merge branch 'account-store-test' into account-store
patche-v 5da08f9
Merge branch 'account-store-test' of github.com:inplayer-org/backend-…
patche-v 7fbe28a
Merge on account-store-test
patche-v 96315da
Changed return values in accountstore and modified tests
patche-v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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) | ||
CloseAccount(id int, userId int, updatedAt time.Time) (*entities.Account, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.