-
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
Transaction History Store #16
Open
patche-v
wants to merge
23
commits into
master
Choose a base branch
from
transaction-history-store
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
23 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 2a6c92e
TransactionHistoryStore Tests
patche-v e38c58d
Tests fixed
patche-v 5cf245a
Tests fixed
patche-v 41f2ff9
Changes to functions return types
patche-v f9d9581
Changed functions return types
patche-v 796933d
Removed unnecessary variables
patche-v b7dbb7a
Removed unnecessary variables
patche-v 3246f2c
Added lastInsertIndex in Insert func
patche-v 8f09fb8
Merge branch 'transactionhistory-tests' into transaction-history-store
patche-v bab15d4
Fixed for iteration
patche-v 2a979a0
Merge branch 'master' of github.com:inplayer-org/backend-interns into…
patche-v 525c2a2
Merged with master
patche-v 324f24e
Added go mod
patche-v 6049447
Changed return values in transactionhistorystore and modified tests
patche-v b9a7e7e
Changed return values in transactionhistorystore 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 |
---|---|---|
@@ -1 +1,79 @@ | ||
package store | ||
|
||
import ( | ||
"bankacc/pkg/entities" | ||
"database/sql" | ||
"time" | ||
) | ||
|
||
type TransactionHistoryStore interface { | ||
Insert(userId int, accountId int, amount float64, action string) (*entities.TransactionHistory, error) | ||
GetTransactionsById(id int) ([]*entities.TransactionHistory, error) | ||
GetTransactionsByIdFromToDate(id int, fromDate time.Time, toDate time.Time) ([]*entities.TransactionHistory, error) | ||
} | ||
|
||
type TransactionHistoryModel struct { | ||
Db *sql.DB | ||
} | ||
|
||
func NewTransactionHistoryStoreModel(db *sql.DB) *TransactionHistoryModel { | ||
return &TransactionHistoryModel{ | ||
Db: db, | ||
} | ||
} | ||
|
||
func (store *TransactionHistoryModel) Insert(userId int, accountId int, amount float64, action string) (*entities.TransactionHistory, error) { | ||
now := time.Now() | ||
|
||
result, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", userId, accountId, amount, action, now) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res, err := result.LastInsertId() | ||
if err != nil { | ||
return nil, err | ||
} | ||
transaction := entities.TransactionHistory{ | ||
Id: int(res), | ||
UserId: userId, | ||
AccountId: accountId, | ||
Amount: amount, | ||
Action: action, | ||
CreatedAt: now, | ||
} | ||
return &transaction, nil | ||
} | ||
|
||
func (store *TransactionHistoryModel) GetTransactionsById(id int) ([]*entities.TransactionHistory, error) { | ||
var transactions []*entities.TransactionHistory | ||
result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ?", id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var transaction entities.TransactionHistory | ||
for result.Next() { | ||
err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
transactions = append(transactions, &transaction) | ||
} | ||
return transactions, nil | ||
} | ||
|
||
func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(id int, fromDate time.Time, toDate time.Time) ([]*entities.TransactionHistory, error) { | ||
var transactions []*entities.TransactionHistory | ||
result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ? and created_at BETWEEN ? and ?", id, fromDate, toDate) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var transaction entities.TransactionHistory | ||
for result.Next() { | ||
err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
transactions = append(transactions, &transaction) | ||
} | ||
return transactions, nil | ||
} |
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,150 @@ | ||
package tests | ||
|
||
import ( | ||
"bankacc/pkg/entities" | ||
"bankacc/pkg/store" | ||
"database/sql" | ||
"log" | ||
"testing" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
func MySQLInit() *sql.DB { | ||
dbDriver := "mysql" | ||
dbUser := "root" | ||
dbPass := "Password1!" | ||
dbName := "BankAccount?parseTime=true" | ||
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
return db | ||
} | ||
|
||
type TearDownTestSuite interface { | ||
TearDownTest() | ||
} | ||
|
||
type TransactionHistoryTestSuite struct { | ||
suite.Suite | ||
Transaction *entities.TransactionHistory | ||
Transactions []entities.TransactionHistory | ||
TransactionsP []*entities.TransactionHistory | ||
TransactionStore store.TransactionHistoryModel | ||
Db *sql.DB | ||
} | ||
|
||
func (suite *TransactionHistoryTestSuite) SetupTest() { | ||
var err error | ||
suite.Db = MySQLInit() | ||
transaction := store.NewTransactionHistoryStoreModel(suite.Db) | ||
suite.Transactions = []entities.TransactionHistory{ | ||
{ | ||
UserId: 1, | ||
AccountId: 1, | ||
Amount: 100, | ||
Action: "Deposit", | ||
}, | ||
{ | ||
UserId: 1, | ||
AccountId: 1, | ||
Amount: 100, | ||
Action: "Withdraw", | ||
}, | ||
{ | ||
UserId: 1, | ||
AccountId: 2, | ||
Amount: 44, | ||
Action: "Deposit", | ||
}, | ||
{ | ||
UserId: 2, | ||
AccountId: 1, | ||
Amount: 200, | ||
Action: "Withdraw", | ||
}, | ||
{ | ||
UserId: 3, | ||
AccountId: 3, | ||
Amount: 300, | ||
Action: "Deposit", | ||
}, | ||
} | ||
|
||
for _, value := range suite.Transactions { | ||
suite.Transaction, err = transaction.Insert(value.UserId, value.AccountId, value.Amount, value.Action) | ||
if err != nil { | ||
suite.T().Fatal("Unable to run InsertTransactionHistory store func") | ||
} | ||
suite.TransactionsP = append(suite.TransactionsP, suite.Transaction) | ||
} | ||
} | ||
|
||
func (suite *TransactionHistoryTestSuite) TestGetTransactionById() { | ||
store := store.NewTransactionHistoryStoreModel(suite.Db) | ||
var err error | ||
var transact []*entities.TransactionHistory | ||
for _, value := range suite.TransactionsP { | ||
transact, err = store.GetTransactionsById(value.UserId) | ||
if err != nil { | ||
suite.T().Fatal("Unable to run GetTransactionsById store func") | ||
} | ||
} | ||
|
||
for _, value := range suite.TransactionsP { | ||
for i := range transact { | ||
if value.Id == transact[i].Id { | ||
suite.Equal(value.UserId, transact[i].UserId) | ||
suite.Equal(value.AccountId, transact[i].AccountId) | ||
suite.Equal(value.Action, transact[i].Action) | ||
suite.Equal(value.Amount, transact[i].Amount) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (suite *TransactionHistoryTestSuite) TestGetTransactionByIdFromToDate() { | ||
store := store.NewTransactionHistoryStoreModel(suite.Db) | ||
var err error | ||
var transact []*entities.TransactionHistory | ||
for _, value := range suite.TransactionsP { | ||
var k int | ||
for k = value.CreatedAt.Nanosecond(); k >= 10; k = k / 10 { | ||
} | ||
if k >= 5 { | ||
value.CreatedAt = value.CreatedAt.Add(time.Second * 1) | ||
} | ||
value.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", value.CreatedAt.UTC().Format("2006-01-02 15:04:05")) | ||
transact, err = store.GetTransactionsByIdFromToDate(value.UserId, value.CreatedAt.UTC(), value.CreatedAt.UTC()) | ||
if err != nil { | ||
suite.T().Fatal("Unable to run GetTransactionsByIdFromDate store func") | ||
} | ||
} | ||
|
||
for _, value := range suite.TransactionsP { | ||
for i := range transact { | ||
if value.Id == transact[i].Id { | ||
suite.Equal(value.UserId, transact[i].UserId) | ||
suite.Equal(value.AccountId, transact[i].AccountId) | ||
suite.Equal(value.Action, transact[i].Action) | ||
suite.Equal(value.Amount, transact[i].Amount) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestTransactionHistoryTestSuite(t *testing.T) { | ||
suite.Run(t, new(TransactionHistoryTestSuite)) | ||
} | ||
|
||
func (suite *TransactionHistoryTestSuite) TearDownTest() { | ||
for i := 0; i < len(suite.TransactionsP); i++ { | ||
_, err := suite.Db.Exec("DELETE FROM TransactionHistory WHERE id=?", suite.TransactionsP[i].Id) | ||
if err != nil { | ||
suite.T().Fatal("Unable to run delete query") | ||
} | ||
} | ||
} |
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.
@vpatcev18 why do we need to iterate the transactions here?
This will fetch transactions multiple times for the same user ID.