mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
partial repository layer added
This commit is contained in:
47
pkg/service/api.go
Normal file
47
pkg/service/api.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/repository"
|
||||
)
|
||||
|
||||
type ApiService struct {
|
||||
repository *repository.ApiRepository
|
||||
}
|
||||
|
||||
func NewApiService(repository *repository.ApiRepository) *ApiService {
|
||||
return &ApiService{
|
||||
repository,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
GetFirst
|
||||
|
||||
Gets first row from API table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
Returns:
|
||||
model.ApiModel: Api object from database.
|
||||
*/
|
||||
func (as ApiService) GetFirst(ctx context.Context) model.ApiModel {
|
||||
return as.repository.GetFirst(ctx)
|
||||
}
|
||||
|
||||
/*
|
||||
PostMigrate
|
||||
|
||||
Starts database migration.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Migration version
|
||||
Returns:
|
||||
*model.MessageResponse: Message response object.
|
||||
*model.Exception: Exception response object.
|
||||
*/
|
||||
func (as ApiService) PostMigrate(ctx context.Context, version string) (*model.MessageResponse, *model.Exception) {
|
||||
return as.repository.PostMigrate(ctx, version)
|
||||
}
|
||||
27
pkg/service/service.go
Normal file
27
pkg/service/service.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"go.uber.org/dig"
|
||||
"wallet-api/pkg/repository"
|
||||
)
|
||||
|
||||
/*
|
||||
InitializeServices
|
||||
|
||||
Initializes Dependency Injection modules for services
|
||||
|
||||
Args:
|
||||
*dig.Container: Dig Container
|
||||
*/
|
||||
func InitializeServices(c *dig.Container) {
|
||||
repository.InitializeRepositories(c)
|
||||
|
||||
c.Provide(NewApiService)
|
||||
c.Provide(NewSubscriptionService)
|
||||
c.Provide(NewSubscriptionTypeService)
|
||||
c.Provide(NewTransactionService)
|
||||
c.Provide(NewTransactionStatusService)
|
||||
c.Provide(NewTransactionTypeService)
|
||||
c.Provide(NewUserService)
|
||||
c.Provide(NewWalletService)
|
||||
}
|
||||
192
pkg/service/subscription.go
Normal file
192
pkg/service/subscription.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/repository"
|
||||
)
|
||||
|
||||
type SubscriptionService struct {
|
||||
repository *repository.SubscriptionRepository
|
||||
}
|
||||
|
||||
func NewSubscriptionService(repository *repository.SubscriptionRepository) *SubscriptionService {
|
||||
return &SubscriptionService{
|
||||
repository,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to subscription table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.NewSubscriptionBody: Request body
|
||||
Returns:
|
||||
*model.Subscription: Created Subscription row object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) New(ctx context.Context, body *model.NewSubscriptionBody) (*model.Subscription, *model.Exception) {
|
||||
tm := new(model.Subscription)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
customRange, _ := body.CustomRange.Int64()
|
||||
|
||||
tm.Init()
|
||||
tm.WalletID = body.WalletID
|
||||
tm.TransactionTypeID = body.TransactionTypeID
|
||||
tm.SubscriptionTypeID = body.SubscriptionTypeID
|
||||
tm.CustomRange = int(customRange)
|
||||
tm.Description = body.Description
|
||||
tm.StartDate = body.StartDate
|
||||
tm.HasEnd = body.HasEnd
|
||||
tm.EndDate = body.EndDate
|
||||
tm.Amount = float32(math.Round(amount*100) / 100)
|
||||
|
||||
if body.StartDate.IsZero() {
|
||||
tm.StartDate = time.Now()
|
||||
}
|
||||
|
||||
response, err := as.repository.New(ctx, tm)
|
||||
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400109"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Get
|
||||
|
||||
Gets row from subscription table by id.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.Auth: Authentication model
|
||||
string: subscription id to search
|
||||
params: *model.Params
|
||||
Returns:
|
||||
*model.Subscription: Subscription row object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) Get(ctx context.Context, am *model.Auth, flt filter.SubscriptionFilter) (*model.Subscription, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
wm := new(model.Subscription)
|
||||
wm.Id = flt.Id
|
||||
response, err := as.repository.Get(ctx, wm, flt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400129"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
|
||||
Gets filtered rows from subscription 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 *SubscriptionService) GetAll(ctx context.Context, flt *filter.SubscriptionFilter, filtered *model.FilteredResponse) *model.Exception {
|
||||
wm := new([]model.Subscription)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
err := as.repository.GetAll(ctx, wm, filtered, flt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400110"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"subscription\" table: %s", err)
|
||||
return exceptionReturn
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
Edit
|
||||
|
||||
Updates row from subscription table by id.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.SubscriptionEdit: Values to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*model.Subscription: Edited Subscription row object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) Edit(ctx context.Context, body *model.SubscriptionEdit, id string) (*model.Subscription, *model.Exception) {
|
||||
amount, _ := body.Amount.Float64()
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tm := new(model.Subscription)
|
||||
tm.Id = id
|
||||
tm.EndDate = body.EndDate
|
||||
tm.HasEnd = body.HasEnd
|
||||
tm.Description = body.Description
|
||||
tm.WalletID = body.WalletID
|
||||
tm.Amount = float32(math.Round(amount*100) / 100)
|
||||
|
||||
response, err := as.repository.Edit(ctx, tm)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400111"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
End
|
||||
|
||||
Updates row in subscription table by id.
|
||||
|
||||
Ends subscription with current date.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: id to search
|
||||
Returns:
|
||||
*model.Subscription: Created Subscription row object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) End(ctx context.Context, id string) (*model.Subscription, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tm := new(model.Subscription)
|
||||
tm.Id = id
|
||||
tm.EndDate = time.Now()
|
||||
tm.HasEnd = true
|
||||
|
||||
response, err := as.repository.End(ctx, tm)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400112"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
80
pkg/service/subscriptionType.go
Normal file
80
pkg/service/subscriptionType.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/repository"
|
||||
)
|
||||
|
||||
type SubscriptionTypeService struct {
|
||||
repository *repository.SubscriptionTypeRepository
|
||||
}
|
||||
|
||||
func NewSubscriptionTypeService(repository *repository.SubscriptionTypeRepository) *SubscriptionTypeService {
|
||||
return &SubscriptionTypeService{
|
||||
repository,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to subscription type table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.NewSubscriptionTypeBody: Values to create new row
|
||||
Returns:
|
||||
*model.SubscriptionType: Created row from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionTypeService) New(ctx context.Context, body *model.NewSubscriptionTypeBody) (*model.SubscriptionType, *model.Exception) {
|
||||
tm := new(model.SubscriptionType)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Type = body.Type
|
||||
|
||||
response, err := as.repository.New(ctx, tm)
|
||||
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 response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
|
||||
Gets all rows from subscription type table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]model.SubscriptionType: List of subscription type objects.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) (*[]model.SubscriptionType, *model.Exception) {
|
||||
wm := new([]model.SubscriptionType)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
flt := filter.NewSubscriptionTypeFilter(model.Params{
|
||||
Embed: embed,
|
||||
})
|
||||
response, err := as.repository.GetAll(ctx, flt, wm)
|
||||
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 response, nil
|
||||
}
|
||||
234
pkg/service/transaction.go
Normal file
234
pkg/service/transaction.go
Normal file
@@ -0,0 +1,234 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/repository"
|
||||
)
|
||||
|
||||
type TransactionService struct {
|
||||
repository *repository.TransactionRepository
|
||||
subscriptionRepository *repository.SubscriptionRepository
|
||||
transactionStatusService *TransactionStatusService
|
||||
}
|
||||
|
||||
func NewTransactionService(repository *repository.TransactionRepository, sr *repository.SubscriptionRepository, tss *TransactionStatusService) *TransactionService {
|
||||
return &TransactionService{
|
||||
repository: repository,
|
||||
subscriptionRepository: sr,
|
||||
transactionStatusService: tss,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New row into transaction table
|
||||
|
||||
Inserts
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.NewTransactionBody: Transaction body object
|
||||
Returns:
|
||||
*model.Transaction: Transaction object
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) New(ctx context.Context, body *model.NewTransactionBody) (*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 {
|
||||
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()
|
||||
}
|
||||
|
||||
response, err := as.repository.New(ctx, tm)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400116"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"transaction\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
|
||||
Gets all rows from subscription type table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*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 {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
err := as.repository.GetAll(ctx, filtered, 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
|
||||
}
|
||||
|
||||
/*
|
||||
Check
|
||||
|
||||
Checks subscriptions and create transacitons.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) Check(ctx context.Context, flt *filter.TransactionFilter) *model.Exception {
|
||||
exceptionReturn := new(model.Exception)
|
||||
filtered := new(model.FilteredResponse)
|
||||
|
||||
err := as.repository.Check(ctx, filtered, 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
|
||||
}
|
||||
|
||||
/*
|
||||
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 (as *TransactionService) Edit(ctx context.Context, body *model.TransactionEdit, id string) (*model.Transaction, *model.Exception) {
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
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 {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400107"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating row in \"transaction\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Bulk Edit
|
||||
|
||||
Updates row in transaction table by id.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
?[]model.Transaction Bulk Edit: Object to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*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)
|
||||
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
|
||||
exceptionReturn.ErrorCode = "400121"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error updating rows in \"transactions\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
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 (as *TransactionService) Get(ctx context.Context, flt *filter.TransactionFilter) (*model.Transaction, *model.Exception) {
|
||||
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
response, err := as.repository.Get(ctx, flt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400122"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactions\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
104
pkg/service/transactionStatus.go
Normal file
104
pkg/service/transactionStatus.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type TransactionStatusService struct {
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewTransactionStatusService(db *pg.DB) *TransactionStatusService {
|
||||
return &TransactionStatusService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to transaction status table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.NewTransactionStatusBody: object to create
|
||||
Returns:
|
||||
*model.TransactionType: Transaction Type object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusService) New(ctx context.Context, body *model.NewTransactionStatusBody) (*model.TransactionStatus, *model.Exception) {
|
||||
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 tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
|
||||
Gets all rows from transaction status table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]model.TransactionStatus: List of Transaction status objects from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) (*[]model.TransactionStatus, *model.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]model.TransactionStatus)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
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, nil
|
||||
}
|
||||
|
||||
func (as *TransactionStatusService) Get(ctx context.Context, flt *filter.TransactionStatusFilter) (*model.TransactionStatus, *model.Exception) {
|
||||
transactionStatus := new(model.TransactionStatus)
|
||||
exceptionReturn := new(model.Exception)
|
||||
if flt.Id != "" {
|
||||
transactionStatus.Id = flt.Id
|
||||
}
|
||||
if flt.Status != "" {
|
||||
transactionStatus.Status = flt.Status
|
||||
}
|
||||
response, err := as.repository.Get(ctx, wm, flt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400129"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"subscription\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
83
pkg/service/transactionType.go
Normal file
83
pkg/service/transactionType.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type TransactionTypeService struct {
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewTransactionTypeService(db *pg.DB) *TransactionTypeService {
|
||||
return &TransactionTypeService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to transaction type table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.NewTransactionTypeBody: object to create
|
||||
Returns:
|
||||
*model.TransactionType: Transaction Type object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionTypeService) New(ctx context.Context, body *model.NewTransactionTypeBody) (*model.TransactionType, *model.Exception) {
|
||||
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 tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
|
||||
Gets all rows from transaction type table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]model.TransactionType: List of Transaction type objects from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) (*[]model.TransactionType, *model.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]model.TransactionType)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
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, nil
|
||||
}
|
||||
210
pkg/service/user.go
Normal file
210
pkg/service/user.go
Normal file
@@ -0,0 +1,210 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
"wallet-api/pkg/utl/configs"
|
||||
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewUserService(db *pg.DB) *UserService {
|
||||
return &UserService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Create
|
||||
|
||||
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 *UserService) 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
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
||||
common.CheckError(err)
|
||||
|
||||
registerBody.Password = string(hashedPassword)
|
||||
_, err = tx.Model(registerBody).Insert()
|
||||
|
||||
if err != nil {
|
||||
exceptionReturn.Message = "Error creating user"
|
||||
exceptionReturn.ErrorCode = "400102"
|
||||
exceptionReturn.StatusCode = 400
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return registerBody, exceptionReturn
|
||||
}
|
||||
|
||||
/*
|
||||
Login
|
||||
|
||||
Gets row from users table by email and valid password.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.Login: object to search
|
||||
Returns:
|
||||
*model.Token: new session token
|
||||
*model.Exception
|
||||
*/
|
||||
func (us *UserService) Login(ctx context.Context, loginBody *model.Login) (*model.Token, *model.Exception) {
|
||||
db := us.db.WithContext(ctx)
|
||||
|
||||
check := new(model.User)
|
||||
exceptionReturn := new(model.Exception)
|
||||
tokenPayload := new(model.Token)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||
|
||||
if check.Email == "" {
|
||||
exceptionReturn.Message = "Email not found"
|
||||
exceptionReturn.ErrorCode = "400103"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
if !check.IsActive {
|
||||
exceptionReturn.Message = "Can't log in. User is deactivated."
|
||||
exceptionReturn.ErrorCode = "400106"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
if bcrypt.CompareHashAndPassword([]byte(check.Password), []byte(loginBody.Password)) != nil {
|
||||
exceptionReturn.Message = "Incorrect password"
|
||||
exceptionReturn.ErrorCode = "400104"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
token, err := CreateToken(check, loginBody.RememberMe)
|
||||
common.CheckError(err)
|
||||
|
||||
tokenPayload.Token = token
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
/*
|
||||
Deactivate
|
||||
|
||||
Updates row in users table.
|
||||
|
||||
IsActive column is set to false
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.Auth: Authentication object
|
||||
Returns:
|
||||
*model.MessageResponse
|
||||
*model.Exception
|
||||
*/
|
||||
func (us *UserService) Deactivate(ctx context.Context, auth *model.Auth) (*model.MessageResponse, *model.Exception) {
|
||||
db := us.db.WithContext(ctx)
|
||||
|
||||
mm := new(model.MessageResponse)
|
||||
me := new(model.Exception)
|
||||
um := new(model.User)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
err := tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select()
|
||||
|
||||
if err != nil {
|
||||
me.ErrorCode = "404101"
|
||||
me.Message = "User not found"
|
||||
me.StatusCode = 404
|
||||
return mm, me
|
||||
}
|
||||
um.IsActive = false
|
||||
_, err = tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Update()
|
||||
|
||||
if err != nil {
|
||||
me.ErrorCode = "400105"
|
||||
me.Message = "Could not deactivate user"
|
||||
me.StatusCode = 400
|
||||
return mm, me
|
||||
}
|
||||
|
||||
mm.Message = "User successfully deactivated."
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return mm, me
|
||||
}
|
||||
|
||||
/*
|
||||
CreateToken
|
||||
|
||||
Generates new jwt token.
|
||||
|
||||
It encodes the user id. Based on rememberMe it is valid through 48hours or 2hours.
|
||||
|
||||
Args:
|
||||
*model.User: User object to encode
|
||||
bool: Should function generate longer lasting token (48hrs)
|
||||
Returns:
|
||||
string: Generated token
|
||||
error: Error that occured in the process
|
||||
*/
|
||||
func CreateToken(user *model.User, rememberMe bool) (string, error) {
|
||||
atClaims := jwt.MapClaims{}
|
||||
atClaims["authorized"] = true
|
||||
atClaims["id"] = user.Id
|
||||
if rememberMe {
|
||||
atClaims["exp"] = time.Now().Add(time.Hour * 48).Unix()
|
||||
} else {
|
||||
atClaims["exp"] = time.Now().Add(time.Hour * 2).Unix()
|
||||
}
|
||||
|
||||
secret := os.Getenv("ACCESS_SECRET")
|
||||
if secret == "" {
|
||||
secret = configs.Secret
|
||||
}
|
||||
|
||||
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
||||
token, err := at.SignedString([]byte(secret))
|
||||
|
||||
return token, err
|
||||
}
|
||||
323
pkg/service/wallet.go
Normal file
323
pkg/service/wallet.go
Normal file
@@ -0,0 +1,323 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type WalletService struct {
|
||||
db *pg.DB
|
||||
subscriptionService *SubscriptionService
|
||||
}
|
||||
|
||||
func NewWalletService(db *pg.DB, ss *SubscriptionService) *WalletService {
|
||||
return &WalletService{
|
||||
db: db,
|
||||
subscriptionService: ss,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts row to wallets table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.NewWalletBody: Object to be inserted
|
||||
Returns:
|
||||
*model.Wallet: Wallet object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) New(ctx context.Context, am *model.NewWalletBody) (*model.Wallet, *model.Exception) {
|
||||
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 walletModel, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Edit
|
||||
|
||||
Updates row in wallets table by id.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.WalletEdit: Object to be edited
|
||||
string: id to search
|
||||
Returns:
|
||||
*model.Wallet: Wallet object from database.
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) Edit(ctx context.Context, body *model.WalletEdit, id string) (*model.Wallet, *model.Exception) {
|
||||
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
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Get
|
||||
|
||||
Gets row in wallets table by id.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: id to search
|
||||
*model.Params: url query parameters
|
||||
Returns:
|
||||
*model.Wallet: Wallet object from database
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) Get(ctx context.Context, id string, params *model.Params) (*model.Wallet, *model.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
wm := new(model.Wallet)
|
||||
wm.Id = id
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
qry := tx.Model(wm)
|
||||
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, nil
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
|
||||
Gets filtered rows from wallets table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.Auth: Authentication object
|
||||
*model.FilteredResponse: filter options
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) GetAll(ctx context.Context, am *model.Auth, filtered *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)
|
||||
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
|
||||
}
|
||||
|
||||
/*
|
||||
GetHeader
|
||||
|
||||
Gets row from wallets table.
|
||||
|
||||
Calculates previous month, current and next month totals.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*model.Auth: Authentication object
|
||||
string: wallet id to search
|
||||
Returns:
|
||||
*model.WalletHeader: generated wallet header object
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) GetHeader(ctx context.Context, am *model.Auth, walletId string) (*model.WalletHeader, *model.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new(model.WalletHeader)
|
||||
wallets := new([]model.WalletTransactions)
|
||||
transactions := new([]model.Transaction)
|
||||
subscriptions := new([]model.Subscription)
|
||||
transactionStatus := new(model.TransactionStatus)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
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()
|
||||
|
||||
currentYear, currentMonth, _ := now.Date()
|
||||
currentLocation := now.Location()
|
||||
|
||||
firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
|
||||
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 = 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 {
|
||||
stopDate := firstOfMonthAfterNext
|
||||
if sub.HasEnd && sub.EndDate.Before(firstOfMonthAfterNext) {
|
||||
stopDate = sub.EndDate
|
||||
}
|
||||
startDate := sub.StartDate
|
||||
for startDate.Before(stopDate) {
|
||||
trans := sub.ToTrans()
|
||||
trans.TransactionDate = startDate
|
||||
if startDate.After(firstOfNextMonth) || startDate.Equal(firstOfNextMonth) {
|
||||
*transactions = append(*transactions, *trans)
|
||||
}
|
||||
if sub.SubscriptionType.Type == "monthly" {
|
||||
startDate = startDate.AddDate(0, sub.CustomRange, 0)
|
||||
} else if sub.SubscriptionType.Type == "weekly" {
|
||||
startDate = startDate.AddDate(0, 0, 7*sub.CustomRange)
|
||||
} else if sub.SubscriptionType.Type == "daily" {
|
||||
startDate = startDate.AddDate(0, 0, sub.CustomRange)
|
||||
} else {
|
||||
startDate = startDate.AddDate(sub.CustomRange, 0, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, trans := range *transactions {
|
||||
addWhere(wallets, trans.WalletID, trans)
|
||||
}
|
||||
|
||||
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(firstOfNextMonth) && 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(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(firstOfMonth) {
|
||||
if trans.TransactionType.Type == "expense" {
|
||||
(*wallets)[i].LastMonth -= trans.Amount
|
||||
} else {
|
||||
(*wallets)[i].LastMonth += trans.Amount
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for _, wallet := range *wallets {
|
||||
wm.LastMonth += wallet.LastMonth
|
||||
wm.CurrentBalance += wallet.CurrentBalance + wallet.LastMonth
|
||||
wm.NextMonth += wallet.NextMonth + wallet.CurrentBalance + wallet.LastMonth
|
||||
}
|
||||
|
||||
wm.Currency = "USD"
|
||||
wm.WalletId = walletId
|
||||
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
addWhere
|
||||
|
||||
Appends Transaction to the belonging walletId.
|
||||
|
||||
If missing, it creates the item list.
|
||||
|
||||
Args:
|
||||
*[]model.WalletTransactions: list to append to
|
||||
string: wallet id to check
|
||||
model.Transaction: Transaction to append
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func addWhere(s *[]model.WalletTransactions, walletId string, e model.Transaction) {
|
||||
var exists bool
|
||||
for a := range *s {
|
||||
if (*s)[a].WalletId == walletId {
|
||||
(*s)[a].Transactions = append((*s)[a].Transactions, e)
|
||||
exists = true
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
var walletTransaction model.WalletTransactions
|
||||
walletTransaction.WalletId = walletId
|
||||
walletTransaction.Transactions = append(walletTransaction.Transactions, e)
|
||||
*s = append(*s, walletTransaction)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user