mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
added transaction status routes
This commit is contained in:
@@ -1 +1,58 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type TransactionStatusService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to transaction status table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewTransactionStatusBody: object to create
|
||||
Returns:
|
||||
*models.TransactionType: Transaction Type object from database.
|
||||
*/
|
||||
func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTransactionStatusBody) *models.TransactionStatus {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.TransactionStatus)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Status = body.Status
|
||||
|
||||
db.Model(tm).Insert()
|
||||
|
||||
return tm
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
|
||||
Gets all rows from transaction status table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.TransactionStatus: List of Transaction status objects from database.
|
||||
*/
|
||||
func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) *[]models.TransactionStatus {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.TransactionStatus)
|
||||
|
||||
query := db.Model(wm)
|
||||
common.GenerateEmbed(query, embed).Select()
|
||||
|
||||
return wm
|
||||
}
|
||||
|
||||
@@ -16,24 +16,26 @@ type TransactionService struct {
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
New new row into transaction table
|
||||
|
||||
Gets all rows from subscription type table.
|
||||
Inserts
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
*models.NewTransactionBody: Transaction body object
|
||||
Returns:
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*models.Transaction: Transaction object
|
||||
*/
|
||||
// Inserts new row to transaction table.
|
||||
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) *models.Transaction {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.Transaction)
|
||||
transactionStatus := new(models.TransactionStatus)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
tm.Init()
|
||||
@@ -42,6 +44,7 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti
|
||||
tm.Description = body.Description
|
||||
tm.TransactionDate = body.TransactionDate
|
||||
tm.Amount = float32(math.Round(amount*100) / 100)
|
||||
tm.TransactionStatusID = transactionStatus.Id
|
||||
|
||||
if body.TransactionDate.IsZero() {
|
||||
tm.TransactionDate = time.Now()
|
||||
|
||||
Reference in New Issue
Block a user