mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
added exception messages to controllers
This commit is contained in:
@@ -51,6 +51,7 @@ func (rc *AuthController) PostLogin(c *gin.Context) {
|
||||
|
||||
if exceptionReturn.Message != "" {
|
||||
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, returnedUser)
|
||||
}
|
||||
@@ -74,6 +75,7 @@ func (rc *AuthController) PostRegister(c *gin.Context) {
|
||||
|
||||
if exceptionReturn.Message != "" {
|
||||
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, returnedUser.Payload())
|
||||
}
|
||||
@@ -94,6 +96,7 @@ func (rc *AuthController) Delete(c *gin.Context) {
|
||||
|
||||
if er.Message != "" {
|
||||
c.JSON(er.StatusCode, er)
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, mr)
|
||||
}
|
||||
@@ -114,6 +117,7 @@ func (rc *AuthController) CheckToken(c *gin.Context) {
|
||||
if err != nil {
|
||||
re.Valid = false
|
||||
c.AbortWithStatusJSON(400, re)
|
||||
return
|
||||
}
|
||||
|
||||
re.Valid = true
|
||||
|
||||
@@ -45,7 +45,11 @@ func (wc *SubscriptionTypeController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.SubscriptionTypeService.New(c, body)
|
||||
wm, exception := wc.SubscriptionTypeService.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -58,7 +62,10 @@ GetAll
|
||||
func (wc *SubscriptionTypeController) GetAll(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
|
||||
wm := wc.SubscriptionTypeService.GetAll(c, embed)
|
||||
|
||||
wm, exception := wc.SubscriptionTypeService.GetAll(c, embed)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -52,7 +52,13 @@ func (wc *SubscriptionController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.SubscriptionService.New(c, body)
|
||||
wm, exception := wc.SubscriptionService.New(c, body)
|
||||
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -71,7 +77,11 @@ func (wc *SubscriptionController) Edit(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
wm := wc.SubscriptionService.Edit(c, body, id)
|
||||
wm, exception := wc.SubscriptionService.Edit(c, body, id)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -93,7 +103,11 @@ func (wc *SubscriptionController) Get(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
params.Embed = embed
|
||||
|
||||
fr := wc.SubscriptionService.Get(c, body, id, params)
|
||||
fr, exception := wc.SubscriptionService.Get(c, body, id, params)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
@@ -113,7 +127,11 @@ func (wc *SubscriptionController) End(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
fr := wc.SubscriptionService.End(c, id)
|
||||
fr, exception := wc.SubscriptionService.End(c, id)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
@@ -132,7 +150,11 @@ func (wc *SubscriptionController) GetAll(c *gin.Context) {
|
||||
fr := FilteredResponse(c)
|
||||
wallet, _ := c.GetQuery("walletId")
|
||||
|
||||
wc.SubscriptionService.GetAll(c, body, wallet, fr)
|
||||
exception := wc.SubscriptionService.GetAll(c, body, wallet, fr)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
|
||||
@@ -45,7 +45,11 @@ func (wc *TransactionStatusController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.TransactionStatusService.New(c, body)
|
||||
wm, exception := wc.TransactionStatusService.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -58,7 +62,11 @@ GetAll
|
||||
func (wc *TransactionStatusController) GetAll(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
|
||||
wm := wc.TransactionStatusService.GetAll(c, embed)
|
||||
wm, exception := wc.TransactionStatusService.GetAll(c, embed)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -45,7 +45,11 @@ func (wc *TransactionTypeController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.TransactionTypeService.New(c, body)
|
||||
wm, exception := wc.TransactionTypeService.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -58,7 +62,11 @@ GetAll
|
||||
func (wc *TransactionTypeController) GetAll(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
|
||||
wm := wc.TransactionTypeService.GetAll(c, embed)
|
||||
wm, exception := wc.TransactionTypeService.GetAll(c, embed)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -30,8 +30,16 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou
|
||||
s.GET("", wc.GetAll)
|
||||
s.PUT("/:id", wc.Edit)
|
||||
s.GET("/:id", wc.Get)
|
||||
s.GET("check", wc.Check)
|
||||
s.PUT("/bulk", wc.BulkEdit)
|
||||
|
||||
bulkGroup := s.Group("bulk")
|
||||
{
|
||||
bulkGroup.PUT("", wc.BulkEdit)
|
||||
}
|
||||
|
||||
checkGroup := s.Group("check")
|
||||
{
|
||||
checkGroup.GET("check", wc.Check)
|
||||
}
|
||||
|
||||
return wc
|
||||
}
|
||||
@@ -49,7 +57,11 @@ func (wc *TransactionController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.TransactionService.New(c, body)
|
||||
wm, exception := wc.TransactionService.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -70,7 +82,11 @@ func (wc *TransactionController) GetAll(c *gin.Context) {
|
||||
noPendingQry, _ := c.GetQuery("noPending")
|
||||
noPending := noPendingQry != ""
|
||||
|
||||
wc.TransactionService.GetAll(c, body, wallet, fr, noPending)
|
||||
exception := wc.TransactionService.GetAll(c, body, wallet, fr, noPending)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
@@ -89,7 +105,11 @@ func (wc *TransactionController) Check(c *gin.Context) {
|
||||
fr := FilteredResponse(c)
|
||||
wallet, _ := c.GetQuery("walletId")
|
||||
|
||||
wc.TransactionService.Check(c, body, wallet, fr)
|
||||
exception := wc.TransactionService.Check(c, body, wallet, fr)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
@@ -109,7 +129,11 @@ func (wc *TransactionController) Edit(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
wm := wc.TransactionService.Edit(c, body, id)
|
||||
wm, exception := wc.TransactionService.Edit(c, body, id)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -126,7 +150,11 @@ func (wc *TransactionController) BulkEdit(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.TransactionService.BulkEdit(c, body)
|
||||
wm, exception := wc.TransactionService.BulkEdit(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -148,7 +176,11 @@ func (wc *TransactionController) Get(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
params.Embed = embed
|
||||
|
||||
fr := wc.TransactionService.Get(c, body, id, params)
|
||||
fr, exception := wc.TransactionService.Get(c, body, id, params)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
|
||||
@@ -44,7 +44,11 @@ func (wc *WalletsHeaderController) Get(c *gin.Context) {
|
||||
auth := c.MustGet("auth")
|
||||
body.Id = auth.(*models.Auth).Id
|
||||
|
||||
wm := wc.WalletService.GetHeader(c, body, walletId)
|
||||
wm, exception := wc.WalletService.GetHeader(c, body, walletId)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -51,7 +51,11 @@ func (wc *WalletsController) New(c *gin.Context) {
|
||||
get := c.MustGet("auth")
|
||||
body.UserID = get.(*models.Auth).Id
|
||||
|
||||
wm := wc.WalletService.New(c, body)
|
||||
wm, exception := wc.WalletService.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -68,7 +72,11 @@ func (wc *WalletsController) GetAll(c *gin.Context) {
|
||||
|
||||
fr := FilteredResponse(c)
|
||||
|
||||
wc.WalletService.GetAll(c, body, fr)
|
||||
exception := wc.WalletService.GetAll(c, body, fr)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
@@ -88,7 +96,11 @@ func (wc *WalletsController) Edit(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
wm := wc.WalletService.Edit(c, body, id)
|
||||
wm, exception := wc.WalletService.Edit(c, body, id)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -106,7 +118,11 @@ func (wc *WalletsController) Get(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
params.Embed = embed
|
||||
|
||||
fr := wc.WalletService.Get(c, id, params)
|
||||
fr, exception := wc.WalletService.Get(c, id, params)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
|
||||
@@ -23,4 +23,4 @@ func CORSMiddleware() gin.HandlerFunc {
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ Adds filters to query and executes it.
|
||||
interface{}: model to be mapped from query execution.
|
||||
*models.FilteredResponse: filter options.
|
||||
*/
|
||||
func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *models.FilteredResponse) {
|
||||
func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *models.FilteredResponse) error {
|
||||
if filtered.Page == 0 {
|
||||
filtered.Page = 1
|
||||
}
|
||||
@@ -33,4 +33,6 @@ func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *models.FilteredR
|
||||
|
||||
filtered.TotalRecords = count
|
||||
filtered.Items = mdl
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
@@ -21,19 +22,27 @@ Inserts new row to subscription type table.
|
||||
*models.NewSubscriptionTypeBody: Values to create new row
|
||||
Returns:
|
||||
*models.SubscriptionType: Created row from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubscriptionTypeBody) *models.SubscriptionType {
|
||||
func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubscriptionTypeBody) (*models.SubscriptionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.SubscriptionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Type = body.Type
|
||||
|
||||
db.Model(tm).Insert()
|
||||
_, err := db.Model(tm).Insert()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400114"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"subscriptionTypes\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -45,14 +54,22 @@ Gets all rows from subscription type table.
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) *[]models.SubscriptionType {
|
||||
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) (*[]models.SubscriptionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.SubscriptionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
common.GenerateEmbed(query, embed).Select()
|
||||
err := common.GenerateEmbed(query, embed).Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400135"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting rows in \"subscriptionTypes\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return wm
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
@@ -23,11 +24,13 @@ Inserts new row to subscription table.
|
||||
*models.NewSubscriptionBody: Request body
|
||||
Returns:
|
||||
*models.Subscription: Created Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscriptionBody) *models.Subscription {
|
||||
func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscriptionBody) (*models.Subscription, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.Subscription)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
customRange, _ := body.CustomRange.Int64()
|
||||
@@ -50,10 +53,16 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(tm).Insert()
|
||||
_, err := tx.Model(tm).Insert()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400109"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
return tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -67,10 +76,12 @@ Gets row from subscription table by id.
|
||||
params: *models.Params
|
||||
Returns:
|
||||
*models.Subscription: Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) *models.Subscription {
|
||||
func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) (*models.Subscription, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
wm := new(models.Subscription)
|
||||
wm.Id = id
|
||||
|
||||
@@ -78,11 +89,17 @@ func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id stri
|
||||
defer tx.Rollback()
|
||||
|
||||
qry := tx.Model(wm)
|
||||
common.GenerateEmbed(qry, params.Embed).WherePK().Select()
|
||||
err := common.GenerateEmbed(qry, params.Embed).WherePK().Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400129"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return wm
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -94,11 +111,14 @@ Gets filtered rows from subscription table.
|
||||
*models.Auth: Authentication object
|
||||
string: Wallet id to search
|
||||
*models.FilteredResponse: filter options
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) *models.Exception {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.Subscription)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
@@ -108,8 +128,16 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall
|
||||
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
}
|
||||
|
||||
FilteredResponse(query, wm, filtered)
|
||||
err := FilteredResponse(query, wm, filtered)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400110"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"subscription\" table: %s", err)
|
||||
return exceptionReturn
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -122,11 +150,13 @@ Updates row from subscription table by id.
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Subscription: Edited Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) Edit(ctx context.Context, body *models.SubscriptionEdit, id string) *models.Subscription {
|
||||
func (as *SubscriptionService) Edit(ctx context.Context, body *models.SubscriptionEdit, id string) (*models.Subscription, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm := new(models.Subscription)
|
||||
tm.Id = id
|
||||
@@ -139,11 +169,17 @@ func (as *SubscriptionService) Edit(ctx context.Context, body *models.Subscripti
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(tm).WherePK().UpdateNotZero()
|
||||
_, err := tx.Model(tm).WherePK().UpdateNotZero()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400111"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -157,9 +193,11 @@ Ends subscription with current date.
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Subscription: Created Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) End(ctx context.Context, id string) *models.Subscription {
|
||||
func (as *SubscriptionService) End(ctx context.Context, id string) (*models.Subscription, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm := new(models.Subscription)
|
||||
tm.Id = id
|
||||
@@ -169,11 +207,17 @@ func (as *SubscriptionService) End(ctx context.Context, id string) *models.Subsc
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(tm).WherePK().UpdateNotZero()
|
||||
_, err := tx.Model(tm).WherePK().UpdateNotZero()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400112"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -183,8 +227,11 @@ Generates and Inserts new Transaction rows from the subscription model.
|
||||
Args:
|
||||
*models.Subscription: Subscription model to generate new transactions from
|
||||
*pg.Tx: Postgres query context
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) {
|
||||
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) *models.Exception {
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
@@ -228,12 +275,20 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
if len(*transactions) > 0 {
|
||||
for _, trans := range *transactions {
|
||||
_, err := tx.Model(&trans).Where("? = ?", pg.Ident("transaction_date"), trans.TransactionDate).Where("? = ?", pg.Ident("subscription_id"), trans.SubscriptionID).OnConflict("DO NOTHING").SelectOrInsert()
|
||||
_, err = tx.Model(&trans).Where("? = ?", pg.Ident("transaction_date"), trans.TransactionDate).Where("? = ?", pg.Ident("subscription_id"), trans.SubscriptionID).OnConflict("DO NOTHING").SelectOrInsert()
|
||||
if err != nil {
|
||||
tx.Model(subModel).Set("? = ?", pg.Ident("last_transaction_date"), trans.TransactionDate).WherePK().Update()
|
||||
_, err = tx.Model(subModel).Set("? = ?", pg.Ident("last_transaction_date"), trans.TransactionDate).WherePK().Update()
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400113"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating row in \"subscription\" table: %s", err)
|
||||
return exceptionReturn
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
@@ -21,19 +22,27 @@ Inserts new row to transaction status table.
|
||||
*models.NewTransactionStatusBody: object to create
|
||||
Returns:
|
||||
*models.TransactionType: Transaction Type object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTransactionStatusBody) *models.TransactionStatus {
|
||||
func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTransactionStatusBody) (*models.TransactionStatus, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.TransactionStatus)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Status = body.Status
|
||||
|
||||
db.Model(tm).Insert()
|
||||
_, 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 tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -45,14 +54,22 @@ Gets all rows from transaction status table.
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.TransactionStatus: List of Transaction status objects from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) *[]models.TransactionStatus {
|
||||
func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) (*[]models.TransactionStatus, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.TransactionStatus)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
common.GenerateEmbed(query, embed).Select()
|
||||
err := common.GenerateEmbed(query, 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 wm
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
@@ -21,19 +22,27 @@ Inserts new row to transaction type table.
|
||||
*models.NewTransactionTypeBody: object to create
|
||||
Returns:
|
||||
*models.TransactionType: Transaction Type object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) *models.TransactionType {
|
||||
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) (*models.TransactionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.TransactionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Type = body.Type
|
||||
|
||||
db.Model(tm).Insert()
|
||||
_, 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 tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -45,14 +54,22 @@ Gets all rows from transaction type table.
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.TransactionType: List of Transaction type objects from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) *[]models.TransactionType {
|
||||
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) (*[]models.TransactionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.TransactionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
common.GenerateEmbed(query, embed).Select()
|
||||
err := common.GenerateEmbed(query, 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 wm
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
@@ -24,9 +25,11 @@ Inserts
|
||||
*models.NewTransactionBody: Transaction body object
|
||||
Returns:
|
||||
*models.Transaction: Transaction object
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) *models.Transaction {
|
||||
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) (*models.Transaction, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm := new(models.Transaction)
|
||||
transactionStatus := new(models.TransactionStatus)
|
||||
@@ -34,7 +37,13 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
err := tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400115"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionsStatus\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
@@ -50,10 +59,16 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti
|
||||
tm.TransactionDate = time.Now()
|
||||
}
|
||||
|
||||
tx.Model(tm).Insert()
|
||||
_, err = tx.Model(tm).Insert()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400116"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"transaction\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
return tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -64,19 +79,26 @@ Gets all rows from subscription type table.
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse, noPending bool) {
|
||||
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse, noPending bool) *models.Exception {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
wm := new([]models.Transaction)
|
||||
transactionStatus := new(models.TransactionStatus)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
err := tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400117"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionStatus\" table: %s", err)
|
||||
return exceptionReturn
|
||||
}
|
||||
|
||||
query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
||||
if walletId != "" {
|
||||
@@ -86,9 +108,16 @@ func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walle
|
||||
query = query.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id)
|
||||
}
|
||||
|
||||
FilteredResponse(query, wm, filtered)
|
||||
err = FilteredResponse(query, wm, filtered)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400118"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row(s) in \"transaction\" table: %s", err)
|
||||
return exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -99,20 +128,27 @@ Checks subscriptions and create transacitons.
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) Check(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||
func (as *TransactionService) Check(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) *models.Exception {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.Transaction)
|
||||
sm := new([]models.Subscription)
|
||||
transactionStatus := new(models.TransactionStatus)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "pending").Select()
|
||||
err := tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "pending").Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400119"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionStatus\" table: %s", err)
|
||||
return exceptionReturn
|
||||
}
|
||||
query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
||||
if walletId != "" {
|
||||
query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
@@ -131,9 +167,16 @@ func (as *TransactionService) Check(ctx context.Context, am *models.Auth, wallet
|
||||
}
|
||||
query = query.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id)
|
||||
|
||||
FilteredResponse(query, wm, filtered)
|
||||
err = FilteredResponse(query, wm, filtered)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400120"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transaction\" table: %s", err)
|
||||
return exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -146,12 +189,15 @@ Updates row in transaction table by id.
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) Edit(ctx context.Context, body *models.TransactionEdit, id string) *models.Transaction {
|
||||
func (as *TransactionService) Edit(ctx context.Context, body *models.TransactionEdit, id string) (*models.Transaction, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm := new(models.Transaction)
|
||||
tm.Id = id
|
||||
tm.Description = body.Description
|
||||
@@ -164,11 +210,26 @@ func (as *TransactionService) Edit(ctx context.Context, body *models.Transaction
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(tm).WherePK().UpdateNotZero()
|
||||
_, err := tx.Model(tm).WherePK().UpdateNotZero()
|
||||
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400107"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating row in \"transaction\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
err = tx.Model(tm).WherePK().Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400108"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transaction\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -181,13 +242,15 @@ Updates row in transaction table by id.
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.TransactionEdit) *[]models.Transaction {
|
||||
func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.TransactionEdit) (*[]models.Transaction, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
transactions := new([]models.Transaction)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
for _, transaction := range *body {
|
||||
|
||||
@@ -201,13 +264,20 @@ func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.Trans
|
||||
tm.TransactionDate = transaction.TransactionDate
|
||||
tm.Amount = float32(math.Round(amount*100) / 100)
|
||||
|
||||
tx.Model(tm).WherePK().UpdateNotZero()
|
||||
*transactions = append(*transactions, *tm)
|
||||
}
|
||||
|
||||
_, err := tx.Model(transactions).WherePK().UpdateNotZero()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400121"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating rows in \"transactions\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return transactions
|
||||
return transactions, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -221,10 +291,12 @@ Gets row from transaction table by id.
|
||||
*model.Params: url query parameters
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) *models.Transaction {
|
||||
func (as *TransactionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) (*models.Transaction, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
wm := new(models.Transaction)
|
||||
wm.Id = id
|
||||
|
||||
@@ -232,9 +304,15 @@ func (as *TransactionService) Get(ctx context.Context, am *models.Auth, id strin
|
||||
defer tx.Rollback()
|
||||
|
||||
qry := tx.Model(wm)
|
||||
common.GenerateEmbed(qry, params.Embed).WherePK().Select()
|
||||
err := common.GenerateEmbed(qry, params.Embed).WherePK().Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400122"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactions\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return wm
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
@@ -23,16 +24,24 @@ Inserts row to wallets table.
|
||||
*models.NewWalletBody: Object to be inserted
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) *models.Wallet {
|
||||
func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) (*models.Wallet, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
walletModel := new(models.Wallet)
|
||||
walletModel.Init()
|
||||
walletModel.UserID = am.UserID
|
||||
walletModel.Name = am.Name
|
||||
db.Model(walletModel).Insert()
|
||||
return walletModel
|
||||
_, 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 walletModel, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -45,10 +54,12 @@ Updates row in wallets table by id.
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id string) *models.Wallet {
|
||||
func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id string) (*models.Wallet, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
tm := new(models.Wallet)
|
||||
tm.Id = id
|
||||
tm.Name = body.Name
|
||||
@@ -56,11 +67,17 @@ func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id s
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(tm).WherePK().UpdateNotZero()
|
||||
_, 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
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -73,9 +90,11 @@ Gets row in wallets table by id.
|
||||
*models.Params: url query parameters
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) Get(ctx context.Context, id string, params *models.Params) *models.Wallet {
|
||||
func (as *WalletService) Get(ctx context.Context, id string, params *models.Params) (*models.Wallet, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
wm := new(models.Wallet)
|
||||
wm.Id = id
|
||||
@@ -84,11 +103,17 @@ func (as *WalletService) Get(ctx context.Context, id string, params *models.Para
|
||||
defer tx.Rollback()
|
||||
|
||||
qry := tx.Model(wm)
|
||||
common.GenerateEmbed(qry, params.Embed).WherePK().Select()
|
||||
err := common.GenerateEmbed(qry, params.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
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return wm
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -99,13 +124,23 @@ Gets filtered rows from wallets table.
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
*models.FilteredResponse: filter options
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) {
|
||||
func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) *models.Exception {
|
||||
exceptionReturn := new(models.Exception)
|
||||
db := as.Db.WithContext(ctx)
|
||||
wm := new([]models.Wallet)
|
||||
|
||||
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||
FilteredResponse(query, wm, filtered)
|
||||
err := FilteredResponse(query, wm, filtered)
|
||||
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
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -120,8 +155,9 @@ Calculates previous month, current and next month totals.
|
||||
string: wallet id to search
|
||||
Returns:
|
||||
*models.WalletHeader: generated wallet header object
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletId string) *models.WalletHeader {
|
||||
func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletId string) (*models.WalletHeader, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new(models.WalletHeader)
|
||||
@@ -129,16 +165,29 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI
|
||||
transactions := new([]models.Transaction)
|
||||
subscriptions := new([]models.Subscription)
|
||||
transactionStatus := new(models.TransactionStatus)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
err := tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400130"
|
||||
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.Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400131"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"subscriptions\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
@@ -155,6 +204,12 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI
|
||||
}
|
||||
query = query.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id)
|
||||
query.Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400132"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactions\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
for _, sub := range *subscriptions {
|
||||
@@ -222,7 +277,7 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI
|
||||
wm.Currency = "USD"
|
||||
wm.WalletId = walletId
|
||||
|
||||
return wm
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -235,6 +290,8 @@ If missing, it creates the item list.
|
||||
*[]models.WalletTransactions: list to append to
|
||||
string: wallet id to check
|
||||
models.Transaction: Transaction to append
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func addWhere(s *[]models.WalletTransactions, walletId string, e models.Transaction) {
|
||||
var exists bool
|
||||
|
||||
Reference in New Issue
Block a user