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:
@@ -3,24 +3,23 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/repository"
|
||||
"wallet-api/pkg/utl/common"
|
||||
)
|
||||
|
||||
type TransactionService struct {
|
||||
repository *repository.TransactionRepository
|
||||
subscriptionRepository *repository.SubscriptionRepository
|
||||
transactionStatusService *TransactionStatusService
|
||||
repository *repository.TransactionRepository
|
||||
subscriptionRepository *repository.SubscriptionRepository
|
||||
transactionStatusRepository *repository.TransactionStatusRepository
|
||||
}
|
||||
|
||||
func NewTransactionService(repository *repository.TransactionRepository, sr *repository.SubscriptionRepository, tss *TransactionStatusService) *TransactionService {
|
||||
func NewTransactionService(repository *repository.TransactionRepository, sr *repository.SubscriptionRepository, tsr *repository.TransactionStatusRepository) *TransactionService {
|
||||
return &TransactionService{
|
||||
repository: repository,
|
||||
subscriptionRepository: sr,
|
||||
transactionStatusService: tss,
|
||||
repository: repository,
|
||||
subscriptionRepository: sr,
|
||||
transactionStatusRepository: tsr,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,39 +35,41 @@ Inserts
|
||||
*model.Transaction: Transaction object
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) New(ctx context.Context, body *model.NewTransactionBody) (*model.Transaction, *model.Exception) {
|
||||
func (as *TransactionService) New(ctx context.Context, tm *model.Transaction) (*model.Transaction, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
tm := new(model.Transaction)
|
||||
|
||||
tsFlt := filter.NewTransactionStatusFilter(model.Params{})
|
||||
tsFlt.Status = "completed"
|
||||
transactionStatus, exceptionReturn := as.transactionStatusService.Get(ctx, tsFlt)
|
||||
|
||||
if exceptionReturn != nil {
|
||||
tx, err := as.repository.CreateTx(ctx)
|
||||
defer tx.Rollback()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400136"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error beginning transaction: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
tm.Init()
|
||||
tm.WalletID = body.WalletID
|
||||
tm.TransactionTypeID = body.TransactionTypeID
|
||||
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()
|
||||
tsFlt := filter.NewTransactionStatusFilter(model.Params{})
|
||||
tsFlt.Status = "completed"
|
||||
transactionStatuses, err := as.transactionStatusRepository.GetAll(ctx, tsFlt, tx)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400138"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error fetching transactionStatus: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
response, err := as.repository.New(ctx, tm)
|
||||
var transactionStatus = common.Find[model.TransactionStatus](transactionStatuses, func(status *model.TransactionStatus) bool {
|
||||
return status.Status == "completed"
|
||||
})
|
||||
tm.TransactionStatusID = transactionStatus.Id
|
||||
|
||||
response, err := as.repository.New(ctx, tm, tx)
|
||||
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 response, nil
|
||||
}
|
||||
@@ -84,23 +85,23 @@ Gets all rows from subscription type table.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) GetAll(ctx context.Context, filtered *model.FilteredResponse, flt *filter.TransactionFilter) *model.Exception {
|
||||
func (as *TransactionService) GetAll(ctx context.Context, flt *filter.TransactionFilter) (*model.FilteredResponse, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
err := as.repository.GetAll(ctx, filtered, flt)
|
||||
filtered, err := as.repository.GetAll(ctx, flt)
|
||||
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
|
||||
}
|
||||
return nil
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Check
|
||||
|
||||
Checks subscriptions and create transacitons.
|
||||
Checks subscriptions and create transactions.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
@@ -108,18 +109,17 @@ Checks subscriptions and create transacitons.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) Check(ctx context.Context, flt *filter.TransactionFilter) *model.Exception {
|
||||
func (as *TransactionService) Check(ctx context.Context, flt *filter.TransactionFilter) (*model.FilteredResponse, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
filtered := new(model.FilteredResponse)
|
||||
|
||||
err := as.repository.Check(ctx, filtered, flt)
|
||||
filtered, err := as.repository.Check(ctx, flt)
|
||||
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
|
||||
}
|
||||
return nil
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -135,20 +135,9 @@ Updates row in transaction table by id.
|
||||
*model.Transaction: Transaction object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) Edit(ctx context.Context, body *model.TransactionEdit, id string) (*model.Transaction, *model.Exception) {
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
func (as *TransactionService) Edit(ctx context.Context, tm *model.Transaction) (*model.Transaction, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tm := new(model.Transaction)
|
||||
tm.Id = id
|
||||
tm.Description = body.Description
|
||||
tm.WalletID = body.WalletID
|
||||
tm.TransactionTypeID = body.TransactionTypeID
|
||||
tm.TransactionDate = body.TransactionDate
|
||||
tm.TransactionStatusID = body.TransactionStatusID
|
||||
tm.Amount = float32(math.Round(amount*100) / 100)
|
||||
|
||||
response, err := as.repository.Edit(ctx, tm)
|
||||
|
||||
if err != nil {
|
||||
@@ -162,7 +151,7 @@ func (as *TransactionService) Edit(ctx context.Context, body *model.TransactionE
|
||||
}
|
||||
|
||||
/*
|
||||
Bulk Edit
|
||||
BulkEdit
|
||||
|
||||
Updates row in transaction table by id.
|
||||
|
||||
@@ -174,25 +163,9 @@ Updates row in transaction table by id.
|
||||
*model.Transaction: Transaction object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) BulkEdit(ctx context.Context, body *[]model.TransactionEdit) (*[]model.Transaction, *model.Exception) {
|
||||
transactions := new([]model.Transaction)
|
||||
func (as *TransactionService) BulkEdit(ctx context.Context, transactions *[]model.Transaction) (*[]model.Transaction, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
for _, transaction := range *body {
|
||||
|
||||
amount, _ := transaction.Amount.Float64()
|
||||
|
||||
tm := new(model.Transaction)
|
||||
tm.Id = transaction.Id
|
||||
tm.Description = transaction.Description
|
||||
tm.WalletID = transaction.WalletID
|
||||
tm.TransactionTypeID = transaction.TransactionTypeID
|
||||
tm.TransactionDate = transaction.TransactionDate
|
||||
tm.Amount = float32(math.Round(amount*100) / 100)
|
||||
|
||||
*transactions = append(*transactions, *tm)
|
||||
}
|
||||
|
||||
response, err := as.repository.BulkEdit(ctx, transactions)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
|
||||
Reference in New Issue
Block a user