add repositories and fixed services

This commit is contained in:
Fran Jurmanović
2022-10-07 23:48:35 +02:00
parent 82e97fc97f
commit ec863d55b3
36 changed files with 703 additions and 497 deletions

View File

@@ -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 {