mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-11-11 07:41:10 +00:00
add repositories and fixed services
This commit is contained in:
@@ -12,7 +12,7 @@ type ApiService struct {
|
||||
|
||||
func NewApiService(repository *repository.ApiRepository) *ApiService {
|
||||
return &ApiService{
|
||||
repository,
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,6 @@ Starts database migration.
|
||||
*model.MessageResponse: Message response object.
|
||||
*model.Exception: Exception response object.
|
||||
*/
|
||||
func (as ApiService) PostMigrate(ctx context.Context, version string) (*model.MessageResponse, *model.Exception) {
|
||||
func (as ApiService) PostMigrate(ctx context.Context, version string) []error {
|
||||
return as.repository.PostMigrate(ctx, version)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"go.uber.org/dig"
|
||||
"wallet-api/pkg/repository"
|
||||
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
|
||||
@@ -3,7 +3,6 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
@@ -16,7 +15,7 @@ type SubscriptionService struct {
|
||||
|
||||
func NewSubscriptionService(repository *repository.SubscriptionRepository) *SubscriptionService {
|
||||
return &SubscriptionService{
|
||||
repository,
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,28 +31,9 @@ Inserts new row to subscription table.
|
||||
*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)
|
||||
func (as *SubscriptionService) New(ctx context.Context, tm *model.Subscription) (*model.Subscription, *model.Exception) {
|
||||
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 {
|
||||
@@ -79,11 +59,9 @@ Gets row from subscription table by id.
|
||||
*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) {
|
||||
func (as *SubscriptionService) Get(ctx context.Context, 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)
|
||||
response, err := as.repository.Get(ctx, flt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400129"
|
||||
@@ -107,19 +85,18 @@ Gets filtered rows from subscription table.
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) GetAll(ctx context.Context, flt *filter.SubscriptionFilter, filtered *model.FilteredResponse) *model.Exception {
|
||||
wm := new([]model.Subscription)
|
||||
func (as *SubscriptionService) GetAll(ctx context.Context, flt *filter.SubscriptionFilter) (*model.FilteredResponse, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
err := as.repository.GetAll(ctx, wm, filtered, flt)
|
||||
filtered, err := as.repository.GetAll(ctx, 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, exceptionReturn
|
||||
}
|
||||
|
||||
return nil
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -135,17 +112,9 @@ Updates row from subscription table by id.
|
||||
*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)
|
||||
func (as *SubscriptionService) Edit(ctx context.Context, tm *model.Subscription) (*model.Subscription, *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)
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
response, err := as.repository.Edit(ctx, tm)
|
||||
if err != nil {
|
||||
|
||||
@@ -14,7 +14,7 @@ type SubscriptionTypeService struct {
|
||||
|
||||
func NewSubscriptionTypeService(repository *repository.SubscriptionTypeRepository) *SubscriptionTypeService {
|
||||
return &SubscriptionTypeService{
|
||||
repository,
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,14 +30,9 @@ Inserts new row to subscription type table.
|
||||
*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)
|
||||
func (as *SubscriptionTypeService) New(ctx context.Context, tm *model.SubscriptionType) (*model.SubscriptionType, *model.Exception) {
|
||||
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
|
||||
@@ -61,14 +56,10 @@ Gets all rows from subscription type table.
|
||||
*[]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)
|
||||
func (as *SubscriptionTypeService) GetAll(ctx context.Context, flt *filter.SubscriptionTypeFilter) (*[]model.SubscriptionType, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
flt := filter.NewSubscriptionTypeFilter(model.Params{
|
||||
Embed: embed,
|
||||
})
|
||||
response, err := as.repository.GetAll(ctx, flt, wm)
|
||||
response, err := as.repository.GetAll(ctx, flt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400135"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,18 +5,16 @@ import (
|
||||
"fmt"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"wallet-api/pkg/repository"
|
||||
)
|
||||
|
||||
type TransactionStatusService struct {
|
||||
db *pg.DB
|
||||
repository *repository.TransactionStatusRepository
|
||||
}
|
||||
|
||||
func NewTransactionStatusService(db *pg.DB) *TransactionStatusService {
|
||||
func NewTransactionStatusService(repository *repository.TransactionStatusRepository) *TransactionStatusService {
|
||||
return &TransactionStatusService{
|
||||
db: db,
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,17 +30,10 @@ Inserts new row to transaction status table.
|
||||
*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)
|
||||
func (as *TransactionStatusService) New(ctx context.Context, tm *model.TransactionStatus) (*model.TransactionStatus, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Status = body.Status
|
||||
|
||||
_, err := db.Model(tm).Insert()
|
||||
response, err := as.repository.New(ctx, tm)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400123"
|
||||
@@ -50,7 +41,7 @@ func (as *TransactionStatusService) New(ctx context.Context, body *model.NewTran
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return tm, nil
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -65,14 +56,10 @@ Gets all rows from transaction status table.
|
||||
*[]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)
|
||||
func (as *TransactionStatusService) GetAll(ctx context.Context, flt *filter.TransactionStatusFilter) (*[]model.TransactionStatus, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
err := common.GenerateEmbed(query, embed).Select()
|
||||
response, err := as.repository.GetAll(ctx, flt, nil)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400124"
|
||||
@@ -80,7 +67,7 @@ func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) (*
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return wm, nil
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (as *TransactionStatusService) Get(ctx context.Context, flt *filter.TransactionStatusFilter) (*model.TransactionStatus, *model.Exception) {
|
||||
@@ -92,7 +79,7 @@ func (as *TransactionStatusService) Get(ctx context.Context, flt *filter.Transac
|
||||
if flt.Status != "" {
|
||||
transactionStatus.Status = flt.Status
|
||||
}
|
||||
response, err := as.repository.Get(ctx, wm, flt)
|
||||
response, err := as.repository.Get(ctx, flt, nil)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400129"
|
||||
|
||||
@@ -3,19 +3,18 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"wallet-api/pkg/repository"
|
||||
)
|
||||
|
||||
type TransactionTypeService struct {
|
||||
db *pg.DB
|
||||
repository *repository.TransactionTypeRepository
|
||||
}
|
||||
|
||||
func NewTransactionTypeService(db *pg.DB) *TransactionTypeService {
|
||||
func NewTransactionTypeService(repository *repository.TransactionTypeRepository) *TransactionTypeService {
|
||||
return &TransactionTypeService{
|
||||
db: db,
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,17 +30,10 @@ Inserts new row to transaction type table.
|
||||
*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)
|
||||
func (as *TransactionTypeService) New(ctx context.Context, tm *model.TransactionType) (*model.TransactionType, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Type = body.Type
|
||||
|
||||
_, err := db.Model(tm).Insert()
|
||||
response, err := as.repository.New(ctx, tm)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400125"
|
||||
@@ -49,7 +41,7 @@ func (as *TransactionTypeService) New(ctx context.Context, body *model.NewTransa
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return tm, nil
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -64,14 +56,10 @@ Gets all rows from transaction type table.
|
||||
*[]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)
|
||||
func (as *TransactionTypeService) GetAll(ctx context.Context, flt *filter.TransactionTypeFilter) (*[]model.TransactionType, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
err := common.GenerateEmbed(query, embed).Select()
|
||||
response, err := as.repository.GetAll(ctx, flt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400133"
|
||||
@@ -79,5 +67,5 @@ func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) (*[]
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return wm, nil
|
||||
return response, nil
|
||||
}
|
||||
|
||||
@@ -4,23 +4,23 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/repository"
|
||||
"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
|
||||
repository *repository.UserRepository
|
||||
}
|
||||
|
||||
func NewUserService(db *pg.DB) *UserService {
|
||||
func NewUserService(repository *repository.UserRepository) *UserService {
|
||||
return &UserService{
|
||||
db: db,
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,37 +37,41 @@ Inserts new row to users table.
|
||||
*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()
|
||||
tx, _ := us.repository.CreateTx(ctx)
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||
check, err := us.repository.Check(ctx, tx, registerBody)
|
||||
if err != nil {
|
||||
exceptionReturn.Message = "Error checking user"
|
||||
exceptionReturn.ErrorCode = "400139"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
if check.Username != "" || check.Email != "" {
|
||||
exceptionReturn.Message = "User already exists"
|
||||
exceptionReturn.ErrorCode = "400101"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return check, exceptionReturn
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
||||
common.CheckError(err)
|
||||
|
||||
registerBody.Password = string(hashedPassword)
|
||||
_, err = tx.Model(registerBody).Insert()
|
||||
|
||||
us.repository.Create(ctx, tx, registerBody)
|
||||
if err != nil {
|
||||
exceptionReturn.Message = "Error creating user"
|
||||
exceptionReturn.ErrorCode = "400102"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return registerBody, exceptionReturn
|
||||
return registerBody, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -82,17 +86,24 @@ Gets row from users table by email and valid password.
|
||||
*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)
|
||||
func (us *UserService) Login(ctx context.Context, body *model.Login) (*model.Token, *model.Exception) {
|
||||
exceptionReturn := new(model.Exception)
|
||||
tokenPayload := new(model.Token)
|
||||
loginBody := new(model.User)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
loginBody.Email = body.Email
|
||||
loginBody.Username = body.Email
|
||||
|
||||
tx, _ := us.repository.CreateTx(ctx)
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||
check, err := us.repository.Check(ctx, tx, loginBody)
|
||||
if err != nil {
|
||||
exceptionReturn.Message = "Error checking user"
|
||||
exceptionReturn.ErrorCode = "400139"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
if check.Email == "" {
|
||||
exceptionReturn.Message = "Email not found"
|
||||
@@ -108,20 +119,18 @@ func (us *UserService) Login(ctx context.Context, loginBody *model.Login) (*mode
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
if bcrypt.CompareHashAndPassword([]byte(check.Password), []byte(loginBody.Password)) != nil {
|
||||
if bcrypt.CompareHashAndPassword([]byte(check.Password), []byte(body.Password)) != nil {
|
||||
exceptionReturn.Message = "Incorrect password"
|
||||
exceptionReturn.ErrorCode = "400104"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
token, err := CreateToken(check, loginBody.RememberMe)
|
||||
token, err := CreateToken(check, body.RememberMe)
|
||||
common.CheckError(err)
|
||||
|
||||
tokenPayload.Token = token
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
@@ -139,39 +148,35 @@ IsActive column is set to false
|
||||
*model.MessageResponse
|
||||
*model.Exception
|
||||
*/
|
||||
func (us *UserService) Deactivate(ctx context.Context, auth *model.Auth) (*model.MessageResponse, *model.Exception) {
|
||||
db := us.db.WithContext(ctx)
|
||||
|
||||
func (us *UserService) Deactivate(ctx context.Context, flt *filter.UserFilter) (*model.MessageResponse, *model.Exception) {
|
||||
mm := new(model.MessageResponse)
|
||||
me := new(model.Exception)
|
||||
um := new(model.User)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
tx, _ := us.repository.CreateTx(ctx)
|
||||
defer tx.Rollback()
|
||||
|
||||
err := tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select()
|
||||
|
||||
um, err := us.repository.Get(ctx, flt, tx)
|
||||
if err != nil {
|
||||
me.ErrorCode = "404101"
|
||||
me.Message = "User not found"
|
||||
me.StatusCode = 404
|
||||
return mm, me
|
||||
return nil, me
|
||||
}
|
||||
um.IsActive = false
|
||||
_, err = tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Update()
|
||||
|
||||
um.IsActive = false
|
||||
_, err = us.repository.Edit(ctx, um, tx)
|
||||
if err != nil {
|
||||
me.ErrorCode = "400105"
|
||||
me.Message = "Could not deactivate user"
|
||||
me.StatusCode = 400
|
||||
return mm, me
|
||||
return nil, me
|
||||
}
|
||||
|
||||
mm.Message = "User successfully deactivated."
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return mm, me
|
||||
return mm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -4,21 +4,24 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"wallet-api/pkg/filter"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"wallet-api/pkg/repository"
|
||||
)
|
||||
|
||||
type WalletService struct {
|
||||
db *pg.DB
|
||||
subscriptionService *SubscriptionService
|
||||
repository *repository.WalletRepository
|
||||
subscriptionRepository *repository.SubscriptionRepository
|
||||
transactionStatusRepository *repository.TransactionStatusRepository
|
||||
transactionRepository *repository.TransactionRepository
|
||||
}
|
||||
|
||||
func NewWalletService(db *pg.DB, ss *SubscriptionService) *WalletService {
|
||||
func NewWalletService(repository *repository.WalletRepository, subscriptionRepository *repository.SubscriptionRepository, transactionStatusRepository *repository.TransactionStatusRepository, transactionRepository *repository.TransactionRepository) *WalletService {
|
||||
return &WalletService{
|
||||
db: db,
|
||||
subscriptionService: ss,
|
||||
repository: repository,
|
||||
subscriptionRepository: subscriptionRepository,
|
||||
transactionStatusRepository: transactionStatusRepository,
|
||||
transactionRepository: transactionRepository,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,21 +38,19 @@ Inserts row to wallets table.
|
||||
*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()
|
||||
response, err := as.repository.New(ctx, walletModel)
|
||||
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
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -65,28 +66,17 @@ Updates row in wallets table by id.
|
||||
*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)
|
||||
|
||||
func (as *WalletService) Edit(ctx context.Context, tm *model.Wallet) (*model.Wallet, *model.Exception) {
|
||||
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()
|
||||
response, err := as.repository.Edit(ctx, tm)
|
||||
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
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -102,18 +92,10 @@ Gets row in wallets table by id.
|
||||
*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)
|
||||
func (as *WalletService) Get(ctx context.Context, flt *filter.WalletFilter) (*model.Wallet, *model.Exception) {
|
||||
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()
|
||||
response, err := as.repository.Get(ctx, flt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400128"
|
||||
@@ -121,9 +103,7 @@ func (as *WalletService) Get(ctx context.Context, id string, params *model.Param
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return wm, nil
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -138,20 +118,17 @@ Gets filtered rows from wallets table.
|
||||
Returns:
|
||||
*model.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) GetAll(ctx context.Context, am *model.Auth, filtered *model.FilteredResponse) *model.Exception {
|
||||
func (as *WalletService) GetAll(ctx context.Context, flt *filter.WalletFilter) (*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)
|
||||
response, err := as.repository.GetAll(ctx, flt)
|
||||
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, exceptionReturn
|
||||
}
|
||||
return nil
|
||||
return response, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -169,31 +146,30 @@ Calculates previous month, current and next month totals.
|
||||
*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)
|
||||
|
||||
func (as *WalletService) GetHeader(ctx context.Context, flt *filter.WalletHeaderFilter) (*model.WalletHeader, *model.Exception) {
|
||||
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()
|
||||
tx, _ := as.repository.CreateTx(ctx)
|
||||
defer tx.Rollback()
|
||||
|
||||
err := tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
trStFlt := filter.NewTransactionStatusFilter(model.Params{})
|
||||
trStFlt.Status = "completed"
|
||||
transactionStatuses, err := as.transactionStatusRepository.GetAll(ctx, trStFlt, tx)
|
||||
transactionStatus := (*transactionStatuses)[0]
|
||||
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()
|
||||
|
||||
subFlt := filter.NewSubscriptionFilter(model.Params{Embed: "TransactionType,SubscriptionType"})
|
||||
subFlt.WalletId = flt.WalletId
|
||||
subscriptions, err = as.subscriptionRepository.GetAllTx(tx, subFlt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400131"
|
||||
@@ -210,12 +186,10 @@ func (as *WalletService) GetHeader(ctx context.Context, am *model.Auth, walletId
|
||||
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()
|
||||
trFlt := filter.NewTransactionFilter(model.Params{Embed: "TransactionType,Wallet"})
|
||||
trFlt.WalletId = flt.WalletId
|
||||
trFlt.TransactionStatusId = transactionStatus.Id
|
||||
transactions, err = as.transactionRepository.GetAllTx(tx, trFlt)
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400132"
|
||||
@@ -287,7 +261,7 @@ func (as *WalletService) GetHeader(ctx context.Context, am *model.Auth, walletId
|
||||
}
|
||||
|
||||
wm.Currency = "USD"
|
||||
wm.WalletId = walletId
|
||||
wm.WalletId = flt.WalletId
|
||||
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user