upgraded migrations and context usage

This commit is contained in:
Fran Jurmanović
2021-07-03 00:01:25 +02:00
parent 4189a0d333
commit 788ff3a146
33 changed files with 321 additions and 251 deletions

View File

@@ -1,6 +1,7 @@
package services
import (
"context"
"wallet-api/pkg/migrate"
"wallet-api/pkg/models"
@@ -11,22 +12,21 @@ type ApiService struct {
Db *pg.DB
}
func (as *ApiService) GetFirst() models.ApiModel {
func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel {
db := as.Db.WithContext(ctx)
apiModel := models.ApiModel{Api: "Works"}
as.Db.Model(&apiModel).First()
db.Model(&apiModel).First()
return apiModel
}
func (as *ApiService) PostMigrate() (*models.MessageResponse, *models.Exception) {
func (as *ApiService) PostMigrate(ctx context.Context, version string) (*models.MessageResponse, *models.Exception) {
db := as.Db.WithContext(ctx)
mr := new(models.MessageResponse)
er := new(models.Exception)
err := migrate.Start(as.Db)
if err != nil {
er.ErrorCode = "400999"
er.StatusCode = 400
er.Message = err.Error()
}
migrate.Start(db, version)
return mr, er
}

View File

@@ -1,6 +1,7 @@
package services
import (
"context"
"wallet-api/pkg/models"
"wallet-api/pkg/utl/common"
@@ -11,22 +12,26 @@ type SubscriptionTypeService struct {
Db *pg.DB
}
func (as *SubscriptionTypeService) New(body *models.NewSubscriptionTypeBody) *models.SubscriptionType {
func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubscriptionTypeBody) *models.SubscriptionType {
db := as.Db.WithContext(ctx)
tm := new(models.SubscriptionType)
tm.Init()
tm.Name = body.Name
tm.Type = body.Type
as.Db.Model(tm).Insert()
db.Model(tm).Insert()
return tm
}
func (as *SubscriptionTypeService) GetAll(embed string) *[]models.SubscriptionType {
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) *[]models.SubscriptionType {
db := as.Db.WithContext(ctx)
wm := new([]models.SubscriptionType)
query := as.Db.Model(wm)
query := db.Model(wm)
common.GenerateEmbed(query, embed).Select()
return wm

View File

@@ -1,6 +1,7 @@
package services
import (
"context"
"math"
"time"
"wallet-api/pkg/models"
@@ -12,7 +13,9 @@ type SubscriptionService struct {
Db *pg.DB
}
func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Subscription {
func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscriptionBody) *models.Subscription {
db := as.Db.WithContext(ctx)
tm := new(models.Subscription)
amount, _ := body.Amount.Float64()
@@ -33,7 +36,7 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
tm.StartDate = time.Now()
}
tx, _ := as.Db.Begin()
tx, _ := db.Begin()
defer tx.Rollback()
tx.Model(tm).Insert()
@@ -44,10 +47,12 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
return tm
}
func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
db := as.Db.WithContext(ctx)
wm := new([]models.Subscription)
tx, _ := as.Db.Begin()
tx, _ := db.Begin()
defer tx.Rollback()
query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)

View File

@@ -1,6 +1,7 @@
package services
import (
"context"
"wallet-api/pkg/models"
"wallet-api/pkg/utl/common"
@@ -11,22 +12,26 @@ type TransactionTypeService struct {
Db *pg.DB
}
func (as *TransactionTypeService) New(body *models.NewTransactionTypeBody) *models.TransactionType {
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) *models.TransactionType {
db := as.Db.WithContext(ctx)
tm := new(models.TransactionType)
tm.Init()
tm.Name = body.Name
tm.Type = body.Type
as.Db.Model(tm).Insert()
db.Model(tm).Insert()
return tm
}
func (as *TransactionTypeService) GetAll(embed string) *[]models.TransactionType {
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) *[]models.TransactionType {
db := as.Db.WithContext(ctx)
wm := new([]models.TransactionType)
query := as.Db.Model(wm)
query := db.Model(wm)
common.GenerateEmbed(query, embed).Select()
return wm

View File

@@ -1,6 +1,7 @@
package services
import (
"context"
"math"
"time"
"wallet-api/pkg/models"
@@ -13,7 +14,9 @@ type TransactionService struct {
Ss *SubscriptionService
}
func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction {
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) *models.Transaction {
db := as.Db.WithContext(ctx)
tm := new(models.Transaction)
amount, _ := body.Amount.Float64()
@@ -29,16 +32,18 @@ func (as *TransactionService) New(body *models.NewTransactionBody) *models.Trans
tm.TransactionDate = time.Now()
}
as.Db.Model(tm).Insert()
db.Model(tm).Insert()
return tm
}
func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
db := as.Db.WithContext(ctx)
wm := new([]models.Transaction)
sm := new([]models.Subscription)
tx, _ := as.Db.Begin()
tx, _ := db.Begin()
defer tx.Rollback()
query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)

View File

@@ -1,6 +1,7 @@
package services
import (
"context"
"os"
"time"
"wallet-api/pkg/models"
@@ -17,11 +18,13 @@ type UsersService struct {
Db *pg.DB
}
func (us *UsersService) Create(registerBody *models.User) (*models.User, *models.Exception) {
func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (*models.User, *models.Exception) {
db := us.Db.WithContext(ctx)
check := new(models.User)
exceptionReturn := new(models.Exception)
tx, _ := us.Db.Begin()
tx, _ := db.Begin()
defer tx.Rollback()
tx.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
@@ -49,12 +52,14 @@ func (us *UsersService) Create(registerBody *models.User) (*models.User, *models
return registerBody, exceptionReturn
}
func (us *UsersService) Login(loginBody *models.Login) (*models.Token, *models.Exception) {
func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*models.Token, *models.Exception) {
db := us.Db.WithContext(ctx)
check := new(models.User)
exceptionReturn := new(models.Exception)
tokenPayload := new(models.Token)
us.Db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
if check.Email == "" {
exceptionReturn.Message = "Email not found"
exceptionReturn.ErrorCode = "400103"
@@ -84,12 +89,14 @@ func (us *UsersService) Login(loginBody *models.Login) (*models.Token, *models.E
return tokenPayload, exceptionReturn
}
func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse, *models.Exception) {
func (us *UsersService) Deactivate(ctx context.Context, auth *models.Auth) (*models.MessageResponse, *models.Exception) {
db := us.Db.WithContext(ctx)
mm := new(models.MessageResponse)
me := new(models.Exception)
um := new(models.User)
tx, _ := us.Db.Begin()
tx, _ := db.Begin()
defer tx.Rollback()
err := tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select()

View File

@@ -1,6 +1,7 @@
package services
import (
"context"
"sync"
"time"
"wallet-api/pkg/models"
@@ -14,40 +15,46 @@ type WalletService struct {
Ss *SubscriptionService
}
func (as *WalletService) New(am *models.NewWalletBody) *models.Wallet {
func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) *models.Wallet {
db := as.Db.WithContext(ctx)
walletModel := new(models.Wallet)
walletModel.Init()
walletModel.UserID = am.UserID
walletModel.Name = am.Name
as.Db.Model(walletModel).Insert()
db.Model(walletModel).Insert()
return walletModel
}
func (as *WalletService) Get(am *models.Auth, embed string) *models.Wallet {
func (as *WalletService) Get(ctx context.Context, am *models.Auth, embed string) *models.Wallet {
db := as.Db.WithContext(ctx)
wm := new(models.Wallet)
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
common.GenerateEmbed(query, embed).Select()
return wm
}
func (as *WalletService) GetAll(am *models.Auth, filtered *models.FilteredResponse) {
func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) {
db := as.Db.WithContext(ctx)
wm := new([]models.Wallet)
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
FilteredResponse(query, wm, filtered)
}
func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.WalletHeader {
func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletId string) *models.WalletHeader {
db := as.Db.WithContext(ctx)
wm := new(models.WalletHeader)
wallets := new([]models.WalletTransactions)
var wg sync.WaitGroup
transactions := new([]models.Transaction)
subscriptions := new([]models.Subscription)
tx, _ := as.Db.Begin()
tx, _ := db.Begin()
defer tx.Rollback()
query2 := tx.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType")
@@ -83,22 +90,22 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
for i, wallet := range *wallets {
for _, trans := range wallet.Transactions {
tzFirstOfMonthAfterNext := firstOfMonthAfterNext.In(trans.TransactionDate.Location())
tzFirstOfNextMonth := firstOfNextMonth.In(trans.TransactionDate.Location())
tzFirstOfMonth := firstOfMonth.In(trans.TransactionDate.Location())
if trans.TransactionDate.Before(tzFirstOfNextMonth) && trans.TransactionDate.After(tzFirstOfMonth) || trans.TransactionDate.Equal(tzFirstOfMonth) {
// tzFirstOfMonthAfterNext := firstOfMonthAfterNext.In(trans.TransactionDate.Location())
// tzFirstOfNextMonth := firstOfNextMonth.In(trans.TransactionDate.Location())
// tzFirstOfMonth := firstOfMonth.In(trans.TransactionDate.Location())
if trans.TransactionDate.Before(firstOfMonth) && trans.TransactionDate.After(firstOfMonth) || trans.TransactionDate.Equal(firstOfMonth) {
if trans.TransactionType.Type == "expense" {
(*wallets)[i].CurrentBalance -= trans.Amount
} else {
(*wallets)[i].CurrentBalance += trans.Amount
}
} else if trans.TransactionDate.Before(tzFirstOfMonthAfterNext) && trans.TransactionDate.After(tzFirstOfNextMonth) {
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
if trans.TransactionType.Type == "expense" {
(*wallets)[i].NextMonth -= trans.Amount
} else {
(*wallets)[i].NextMonth += trans.Amount
}
} else if trans.TransactionDate.Before(tzFirstOfMonth) {
} else if trans.TransactionDate.Before(firstOfMonth) {
if trans.TransactionType.Type == "expense" {
(*wallets)[i].LastMonth -= trans.Amount
} else {