mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
add repositories and fixed services
This commit is contained in:
@@ -30,14 +30,16 @@ func InitializeRepositories(c *dig.Container) {
|
||||
/*
|
||||
FilteredResponse
|
||||
|
||||
Adds filters to query and executes it.
|
||||
Adds filter to query and executes it.
|
||||
|
||||
Args:
|
||||
*pg.Query: postgres query
|
||||
interface{}: model to be mapped from query execution.
|
||||
*model.FilteredResponse: filter options.
|
||||
*/
|
||||
func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *model.FilteredResponse) error {
|
||||
func FilteredResponse(qry *pg.Query, mdl interface{}, params model.Params) (*model.FilteredResponse, error) {
|
||||
filtered := new(model.FilteredResponse)
|
||||
filtered.Params = params
|
||||
if filtered.Page == 0 {
|
||||
filtered.Page = 1
|
||||
}
|
||||
@@ -55,5 +57,5 @@ func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *model.FilteredRe
|
||||
filtered.TotalRecords = count
|
||||
filtered.Items = mdl
|
||||
|
||||
return err
|
||||
return filtered, err
|
||||
}
|
||||
|
||||
@@ -63,11 +63,14 @@ Gets row from subscription table by id.
|
||||
*model.Subscription: Subscription row object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionRepository) Get(ctx context.Context, am *model.Subscription, flt filter.SubscriptionFilter) (*model.Subscription, error) {
|
||||
func (as *SubscriptionRepository) Get(ctx context.Context, flt filter.SubscriptionFilter) (*model.Subscription, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
am := new(model.Subscription)
|
||||
am.Id = flt.Id
|
||||
|
||||
qry := tx.Model(am)
|
||||
as.OnBeforeGetSubscriptionFilter(qry, &flt)
|
||||
err := common.GenerateEmbed(qry, flt.Embed).WherePK().Select()
|
||||
@@ -93,22 +96,23 @@ Gets filtered rows from subscription table.
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionRepository) GetAll(ctx context.Context, am *[]model.Subscription, filtered *model.FilteredResponse, flt *filter.SubscriptionFilter) error {
|
||||
func (as *SubscriptionRepository) GetAll(ctx context.Context, flt *filter.SubscriptionFilter) (*model.FilteredResponse, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
am := new([]model.Subscription)
|
||||
query := tx.Model(am)
|
||||
as.OnBeforeGetSubscriptionFilter(query, flt)
|
||||
|
||||
err := FilteredResponse(query, am, filtered)
|
||||
filtered, err := FilteredResponse(query, am, flt.Params)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
return nil
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -124,17 +128,18 @@ Gets filtered rows from subscription table.
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionRepository) GetAllTx(tx *pg.Tx, am *[]model.Subscription, flt *filter.SubscriptionFilter) error {
|
||||
func (as *SubscriptionRepository) GetAllTx(tx *pg.Tx, flt *filter.SubscriptionFilter) (*[]model.Subscription, error) {
|
||||
am := new([]model.Subscription)
|
||||
query := tx.Model(am)
|
||||
as.OnBeforeGetSubscriptionFilter(query, flt)
|
||||
|
||||
common.GenerateEmbed(query, flt.Embed)
|
||||
err := query.Select()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
return nil
|
||||
return am, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -218,7 +223,6 @@ func (as *SubscriptionRepository) SubToTrans(subModel *model.Subscription, tx *p
|
||||
transactionStatus := new(model.TransactionStatus)
|
||||
firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation)
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "pending").Select()
|
||||
//tzFirstOfNextMonth := firstOfNextMonth.In(subModel.StartDate.Location())
|
||||
|
||||
startDate := subModel.StartDate
|
||||
stopDate := firstOfNextMonth
|
||||
@@ -271,8 +275,8 @@ func (as *SubscriptionRepository) SubToTrans(subModel *model.Subscription, tx *p
|
||||
}
|
||||
|
||||
func (as *SubscriptionRepository) OnBeforeGetSubscriptionFilter(qry *orm.Query, flt *filter.SubscriptionFilter) {
|
||||
if flt.Id != "" {
|
||||
qry.Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), flt.Id)
|
||||
if flt.UserId != "" {
|
||||
qry.Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), flt.UserId)
|
||||
}
|
||||
if flt.WalletId != "" {
|
||||
qry.Where("? = ?", pg.Ident("wallet_id"), flt.WalletId)
|
||||
|
||||
@@ -54,7 +54,8 @@ Gets all rows from subscription type table.
|
||||
*[]model.SubscriptionType: List of subscription type objects.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionTypeRepository) GetAll(ctx context.Context, flt *filter.SubscriptionTypeFilter, wm *[]model.SubscriptionType) (*[]model.SubscriptionType, error) {
|
||||
func (as *SubscriptionTypeRepository) GetAll(ctx context.Context, flt *filter.SubscriptionTypeFilter) (*[]model.SubscriptionType, error) {
|
||||
wm := new([]model.SubscriptionType)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
query := db.Model(wm)
|
||||
|
||||
@@ -37,17 +37,26 @@ Inserts
|
||||
*model.Transaction: Transaction object
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionRepository) New(ctx context.Context, tm *model.Transaction) (*model.Transaction, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
func (as *TransactionRepository) New(ctx context.Context, tm *model.Transaction, tx *pg.Tx) (*model.Transaction, error) {
|
||||
var commit = false
|
||||
if tx == nil {
|
||||
commit = true
|
||||
db := as.db.WithContext(ctx)
|
||||
tx, _ = db.Begin()
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
if commit {
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
_, err := tx.Model(tm).Insert()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tx.Commit()
|
||||
if commit {
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
return tm, nil
|
||||
}
|
||||
@@ -63,48 +72,76 @@ Gets all rows from subscription type table.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionRepository) GetAll(ctx context.Context, filtered *model.FilteredResponse, flt *filter.TransactionFilter) *model.Exception {
|
||||
func (as *TransactionRepository) GetAll(ctx context.Context, flt *filter.TransactionFilter) (*model.FilteredResponse, *model.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(model.Exception)
|
||||
wm := new([]model.Transaction)
|
||||
transactionStatus := new(model.TransactionStatus)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tsFlt := filter.NewTransactionStatusFilter(model.Params{})
|
||||
tsFlt.Status = "completed"
|
||||
_, err := as.transactionStatusRepository.GetTx(tx, transactionStatus, tsFlt)
|
||||
if flt.NoPending {
|
||||
tsFlt := filter.NewTransactionStatusFilter(model.Params{})
|
||||
tsFlt.Status = "completed"
|
||||
transactionStatus, err := as.transactionStatusRepository.GetTx(tx, tsFlt)
|
||||
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400117"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionStatus\" table: %s", err)
|
||||
return exceptionReturn
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400117"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionStatus\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
flt.TransactionStatusId = transactionStatus.Id
|
||||
}
|
||||
|
||||
flt.TransactionStatusId = transactionStatus.Id
|
||||
|
||||
query := tx.Model(wm)
|
||||
|
||||
as.OnBeforeGetTransactionFilter(query, flt)
|
||||
err = FilteredResponse(query, wm, filtered)
|
||||
filtered, err := FilteredResponse(query, wm, flt.Params)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400118"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row(s) in \"transaction\" table: %s", err)
|
||||
return exceptionReturn
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return nil
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
/*
|
||||
GetAllTx
|
||||
|
||||
Gets filtered rows from transaction table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.Auth: Authentication object
|
||||
string: Wallet id to search
|
||||
*model.FilteredResponse: filter options
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionRepository) GetAllTx(tx *pg.Tx, flt *filter.TransactionFilter) (*[]model.Transaction, error) {
|
||||
am := new([]model.Transaction)
|
||||
query := tx.Model(am)
|
||||
as.OnBeforeGetTransactionFilter(query, flt)
|
||||
|
||||
common.GenerateEmbed(query, flt.Embed)
|
||||
err := query.Select()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return am, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Check
|
||||
|
||||
Checks subscriptions and create transacitons.
|
||||
Checks subscriptions and create transactions.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
@@ -112,32 +149,37 @@ Checks subscriptions and create transacitons.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionRepository) Check(ctx context.Context, filtered *model.FilteredResponse, flt *filter.TransactionFilter) *model.Exception {
|
||||
func (as *TransactionRepository) Check(ctx context.Context, flt *filter.TransactionFilter) (*model.FilteredResponse, *model.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]model.Transaction)
|
||||
sm := new([]model.Subscription)
|
||||
transactionStatus := new(model.TransactionStatus)
|
||||
exceptionReturn := new(model.Exception)
|
||||
filtered := new(model.FilteredResponse)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tsFlt := filter.NewTransactionStatusFilter(model.Params{})
|
||||
tsFlt.Status = "pending"
|
||||
_, err := as.transactionStatusRepository.GetTx(tx, transactionStatus, tsFlt)
|
||||
transactionStatus, err := as.transactionStatusRepository.GetTx(tx, tsFlt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400119"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionStatus\" table: %s", err)
|
||||
return exceptionReturn
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
flt.TransactionStatusId = transactionStatus.Id
|
||||
|
||||
smFlt := filter.NewSubscriptionFilter(model.Params{})
|
||||
smFlt.Id = flt.Id
|
||||
smFlt.WalletId = flt.WalletId
|
||||
as.subscriptionRepository.GetAllTx(tx, sm, smFlt)
|
||||
sm, err := as.subscriptionRepository.GetAllTx(tx, smFlt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400137"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting rows in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
for _, sub := range *sm {
|
||||
if sub.HasNew() {
|
||||
@@ -147,16 +189,16 @@ func (as *TransactionRepository) Check(ctx context.Context, filtered *model.Filt
|
||||
|
||||
qry := tx.Model(wm)
|
||||
as.OnBeforeGetTransactionFilter(qry, flt)
|
||||
err = FilteredResponse(qry, wm, filtered)
|
||||
filtered, err = FilteredResponse(qry, wm, flt.Params)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400120"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transaction\" table: %s", err)
|
||||
return exceptionReturn
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return nil
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -195,7 +237,7 @@ func (as *TransactionRepository) Edit(ctx context.Context, tm *model.Transaction
|
||||
}
|
||||
|
||||
/*
|
||||
Bulk Edit
|
||||
BulkEdit
|
||||
|
||||
Updates row in transaction table by id.
|
||||
|
||||
@@ -239,6 +281,7 @@ Gets row from transaction table by id.
|
||||
func (as *TransactionRepository) Get(ctx context.Context, flt *filter.TransactionFilter) (*model.Transaction, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
wm := new(model.Transaction)
|
||||
wm.Id = flt.Id
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
@@ -259,10 +302,15 @@ func (as *TransactionRepository) OnBeforeGetTransactionFilter(qry *orm.Query, fl
|
||||
if flt.WalletId != "" {
|
||||
qry.Where("? = ?", pg.Ident("wallet_id"), flt.WalletId)
|
||||
}
|
||||
if flt.Id != "" {
|
||||
qry.Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), flt.Id)
|
||||
if flt.UserId != "" {
|
||||
qry.Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), flt.UserId)
|
||||
}
|
||||
if flt.NoPending && flt.TransactionStatusId != "" {
|
||||
if flt.TransactionStatusId != "" {
|
||||
qry.Where("? = ?", pg.Ident("transaction_status_id"), flt.TransactionStatusId)
|
||||
}
|
||||
}
|
||||
|
||||
func (as *TransactionRepository) CreateTx(ctx context.Context) (*pg.Tx, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
return db.Begin()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
@@ -33,22 +32,12 @@ Inserts new row to transaction status table.
|
||||
*model.TransactionType: Transaction Type object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusRepository) New(ctx context.Context, body *model.NewTransactionStatusBody) (*model.TransactionStatus, *model.Exception) {
|
||||
func (as *TransactionStatusRepository) New(ctx context.Context, tm *model.TransactionStatus) (*model.TransactionStatus, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
tm := new(model.TransactionStatus)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Status = body.Status
|
||||
|
||||
_, err := db.Model(tm).Insert()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400123"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"transactionStatus\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tm, nil
|
||||
@@ -66,19 +55,29 @@ Gets all rows from transaction status table.
|
||||
*[]model.TransactionStatus: List of Transaction status objects from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusRepository) GetAll(ctx context.Context, embed string) (*[]model.TransactionStatus, *model.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
func (as *TransactionStatusRepository) GetAll(ctx context.Context, flt *filter.TransactionStatusFilter, tx *pg.Tx) (*[]model.TransactionStatus, error) {
|
||||
var commit = false
|
||||
if tx == nil {
|
||||
commit = true
|
||||
db := as.db.WithContext(ctx)
|
||||
tx, _ = db.Begin()
|
||||
}
|
||||
|
||||
if commit {
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
wm := new([]model.TransactionStatus)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
err := common.GenerateEmbed(query, embed).Select()
|
||||
query := tx.Model(wm)
|
||||
as.OnBeforeGetTransactionStatusFilter(query, flt)
|
||||
err := common.GenerateEmbed(query, flt.Embed).Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400124"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting rows in \"transactionStatus\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if commit {
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
return wm, nil
|
||||
@@ -98,18 +97,25 @@ Gets row from transactionStatus table by id.
|
||||
*model.Subscription: Subscription row object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusRepository) Get(ctx context.Context, am *model.TransactionStatus, flt filter.TransactionStatusFilter) (*model.TransactionStatus, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
func (as *TransactionStatusRepository) Get(ctx context.Context, flt *filter.TransactionStatusFilter, tx *pg.Tx) (*model.TransactionStatus, error) {
|
||||
am := new(model.TransactionStatus)
|
||||
commit := false
|
||||
if tx == nil {
|
||||
commit = true
|
||||
db := as.db.WithContext(ctx)
|
||||
tx, _ = db.Begin()
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
qry := tx.Model(am)
|
||||
err := common.GenerateEmbed(qry, flt.Embed).WherePK().Select()
|
||||
err := common.GenerateEmbed(qry, flt.Embed).Select()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
if commit {
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
return am, nil
|
||||
}
|
||||
@@ -128,10 +134,11 @@ Gets row from transactionStatus table by id.
|
||||
*model.Subscription: Subscription row object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusRepository) GetTx(tx *pg.Tx, am *model.TransactionStatus, flt *filter.TransactionStatusFilter) (*model.TransactionStatus, error) {
|
||||
func (as *TransactionStatusRepository) GetTx(tx *pg.Tx, flt *filter.TransactionStatusFilter) (*model.TransactionStatus, error) {
|
||||
am := new(model.TransactionStatus)
|
||||
qry := tx.Model(am)
|
||||
as.OnBeforeGetTransactionStatusFilter(qry, flt)
|
||||
err := common.GenerateEmbed(qry, flt.Embed).WherePK().Select()
|
||||
err := common.GenerateEmbed(qry, flt.Embed).Select()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -140,6 +147,9 @@ func (as *TransactionStatusRepository) GetTx(tx *pg.Tx, am *model.TransactionSta
|
||||
}
|
||||
|
||||
func (as *TransactionStatusRepository) OnBeforeGetTransactionStatusFilter(qry *orm.Query, flt *filter.TransactionStatusFilter) {
|
||||
if flt.Id != "" {
|
||||
qry.Where("? = ?", pg.Ident("id"), flt.Id)
|
||||
}
|
||||
if flt.Status != "" {
|
||||
qry.Where("? = ?", pg.Ident("status"), flt.Status)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
@@ -31,22 +31,12 @@ Inserts new row to transaction type table.
|
||||
*model.TransactionType: Transaction Type object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionTypeRepository) New(ctx context.Context, body *model.NewTransactionTypeBody) (*model.TransactionType, *model.Exception) {
|
||||
func (as *TransactionTypeRepository) New(ctx context.Context, tm *model.TransactionType) (*model.TransactionType, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
tm := new(model.TransactionType)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Type = body.Type
|
||||
|
||||
_, err := db.Model(tm).Insert()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400125"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"transactionTypes\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tm, nil
|
||||
@@ -64,19 +54,15 @@ Gets all rows from transaction type table.
|
||||
*[]model.TransactionType: List of Transaction type objects from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionTypeRepository) GetAll(ctx context.Context, embed string) (*[]model.TransactionType, *model.Exception) {
|
||||
func (as *TransactionTypeRepository) GetAll(ctx context.Context, flt *filter.TransactionTypeFilter) (*[]model.TransactionType, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]model.TransactionType)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
err := common.GenerateEmbed(query, embed).Select()
|
||||
err := common.GenerateEmbed(query, flt.Embed).Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400133"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionTypes\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return wm, nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
"wallet-api/pkg/utl/configs"
|
||||
@@ -24,6 +25,118 @@ func NewUserRepository(db *pg.DB) *UserRepository {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Get
|
||||
|
||||
Gets row from transaction table by id.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.Auth: Authentication object
|
||||
string: id to search
|
||||
*model.Params: url query parameters
|
||||
Returns:
|
||||
*model.Transaction: Transaction object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (us *UserRepository) Get(ctx context.Context, flt *filter.UserFilter, tx *pg.Tx) (*model.User, error) {
|
||||
wm := new(model.User)
|
||||
wm.Id = flt.Id
|
||||
|
||||
commit := false
|
||||
if tx == nil {
|
||||
commit = true
|
||||
db := us.db.WithContext(ctx)
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
qry := tx.Model(wm)
|
||||
err := common.GenerateEmbed(qry, flt.Embed).WherePK().Select()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if commit {
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Edit
|
||||
|
||||
Updates row in transaction table by id.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.TransactionEdit: Object to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*model.Transaction: Transaction object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (us *UserRepository) Edit(ctx context.Context, tm *model.User, tx *pg.Tx) (*model.User, error) {
|
||||
commit := false
|
||||
if tx == nil {
|
||||
commit = true
|
||||
db := us.db.WithContext(ctx)
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
_, err := tx.Model(tm).WherePK().UpdateNotZero()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Model(tm).WherePK().Select()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if commit {
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Check
|
||||
|
||||
Inserts new row to users table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.User: User object to create
|
||||
Returns:
|
||||
*model.User: User object from database
|
||||
*model.Exception
|
||||
*/
|
||||
func (us *UserRepository) Check(ctx context.Context, tx *pg.Tx, checkBody *model.User) (*model.User, error) {
|
||||
check := new(model.User)
|
||||
|
||||
commit := false
|
||||
if tx == nil {
|
||||
commit = true
|
||||
db := us.db.WithContext(ctx)
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
err := tx.Model(check).Where("? = ?", pg.Ident("username"), checkBody.Username).WhereOr("? = ?", pg.Ident("email"), checkBody.Email).Select()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if commit {
|
||||
tx.Commit()
|
||||
}
|
||||
return check, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Create
|
||||
|
||||
@@ -36,38 +149,31 @@ Inserts new row to users table.
|
||||
*model.User: User object from database
|
||||
*model.Exception
|
||||
*/
|
||||
func (us *UserRepository) Create(ctx context.Context, registerBody *model.User) (*model.User, *model.Exception) {
|
||||
db := us.db.WithContext(ctx)
|
||||
|
||||
check := new(model.User)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||
if check.Username != "" || check.Email != "" {
|
||||
exceptionReturn.Message = "User already exists"
|
||||
exceptionReturn.ErrorCode = "400101"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return check, exceptionReturn
|
||||
func (us *UserRepository) Create(ctx context.Context, tx *pg.Tx, registerBody *model.User) (*model.User, error) {
|
||||
commit := false
|
||||
if tx == nil {
|
||||
commit = true
|
||||
db := us.db.WithContext(ctx)
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
||||
common.CheckError(err)
|
||||
|
||||
registerBody.Password = string(hashedPassword)
|
||||
_, err = tx.Model(registerBody).Insert()
|
||||
_, err := tx.Model(registerBody).Insert()
|
||||
|
||||
if err != nil {
|
||||
exceptionReturn.Message = "Error creating user"
|
||||
exceptionReturn.ErrorCode = "400102"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
if commit {
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
return registerBody, exceptionReturn
|
||||
return registerBody, nil
|
||||
}
|
||||
|
||||
func (us *UserRepository) CreateTx(ctx context.Context) (*pg.Tx, error) {
|
||||
db := us.db.WithContext(ctx)
|
||||
return db.Begin()
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
@@ -34,20 +35,11 @@ Inserts row to wallets table.
|
||||
*model.Wallet: Wallet object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletRepository) New(ctx context.Context, am *model.NewWalletBody) (*model.Wallet, *model.Exception) {
|
||||
func (as *WalletRepository) New(ctx context.Context, walletModel *model.Wallet) (*model.Wallet, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(model.Exception)
|
||||
walletModel := new(model.Wallet)
|
||||
walletModel.Init()
|
||||
walletModel.UserID = am.UserID
|
||||
walletModel.Name = am.Name
|
||||
_, err := db.Model(walletModel).Insert()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400126"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"wallets\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
return nil, err
|
||||
}
|
||||
return walletModel, nil
|
||||
}
|
||||
@@ -65,23 +57,15 @@ Updates row in wallets table by id.
|
||||
*model.Wallet: Wallet object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletRepository) Edit(ctx context.Context, body *model.WalletEdit, id string) (*model.Wallet, *model.Exception) {
|
||||
func (as *WalletRepository) Edit(ctx context.Context, tm *model.Wallet) (*model.Wallet, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(model.Exception)
|
||||
tm := new(model.Wallet)
|
||||
tm.Id = id
|
||||
tm.Name = body.Name
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
_, err := tx.Model(tm).WherePK().UpdateNotZero()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400127"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating row in \"wallets\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
@@ -102,23 +86,19 @@ Gets row in wallets table by id.
|
||||
*model.Wallet: Wallet object from database
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletRepository) Get(ctx context.Context, id string, params *model.Params) (*model.Wallet, *model.Exception) {
|
||||
func (as *WalletRepository) Get(ctx context.Context, flt *filter.WalletFilter) (*model.Wallet, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
wm := new(model.Wallet)
|
||||
wm.Id = id
|
||||
wm.Id = flt.Id
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
qry := tx.Model(wm)
|
||||
err := common.GenerateEmbed(qry, params.Embed).WherePK().Select()
|
||||
err := common.GenerateEmbed(qry, flt.Embed).WherePK().Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400128"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"wallets\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
@@ -138,20 +118,20 @@ Gets filtered rows from wallets table.
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletRepository) GetAll(ctx context.Context, am *model.Auth, filtered *model.FilteredResponse) *model.Exception {
|
||||
func (as *WalletRepository) GetAll(ctx context.Context, flt *filter.WalletFilter) (*model.FilteredResponse, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
db := as.db.WithContext(ctx)
|
||||
wm := new([]model.Wallet)
|
||||
|
||||
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||
err := FilteredResponse(query, wm, filtered)
|
||||
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), flt.UserId)
|
||||
filtered, err := FilteredResponse(query, wm, flt.Params)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400134"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting rows in \"wallets\" table: %s", err)
|
||||
return exceptionReturn
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
return nil
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -169,7 +149,7 @@ Calculates previous month, current and next month totals.
|
||||
*model.WalletHeader: generated wallet header object
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletRepository) GetHeader(ctx context.Context, am *model.Auth, walletId string) (*model.WalletHeader, *model.Exception) {
|
||||
func (as *WalletRepository) GetHeader(ctx context.Context, flt *filter.WalletHeaderFilter) (*model.WalletHeader, *model.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new(model.WalletHeader)
|
||||
@@ -189,9 +169,9 @@ func (as *WalletRepository) GetHeader(ctx context.Context, am *model.Auth, walle
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionStatuses\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
query2 := tx.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType")
|
||||
if walletId != "" {
|
||||
query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
query2 := tx.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), flt.UserId).Relation("TransactionType").Relation("SubscriptionType")
|
||||
if flt.WalletId != "" {
|
||||
query2.Where("? = ?", pg.Ident("wallet_id"), flt.WalletId)
|
||||
}
|
||||
query2.Select()
|
||||
if err != nil {
|
||||
@@ -210,9 +190,9 @@ func (as *WalletRepository) GetHeader(ctx context.Context, am *model.Auth, walle
|
||||
firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation)
|
||||
firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 1, 0, 0, 0, 0, currentLocation)
|
||||
|
||||
query := tx.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
|
||||
if walletId != "" {
|
||||
query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
query := tx.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), flt.UserId).Relation("TransactionType")
|
||||
if flt.WalletId != "" {
|
||||
query.Where("? = ?", pg.Ident("wallet_id"), flt.WalletId)
|
||||
}
|
||||
query = query.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id)
|
||||
query.Select()
|
||||
@@ -287,11 +267,16 @@ func (as *WalletRepository) GetHeader(ctx context.Context, am *model.Auth, walle
|
||||
}
|
||||
|
||||
wm.Currency = "USD"
|
||||
wm.WalletId = walletId
|
||||
wm.WalletId = flt.WalletId
|
||||
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
func (as *WalletRepository) CreateTx(ctx context.Context) (*pg.Tx, error) {
|
||||
db := as.db.WithContext(ctx)
|
||||
return db.Begin()
|
||||
}
|
||||
|
||||
/*
|
||||
addWhere
|
||||
|
||||
|
||||
Reference in New Issue
Block a user