mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
added subscription edit
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"math"
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
@@ -47,6 +48,27 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip
|
||||
return tm
|
||||
}
|
||||
|
||||
func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) *models.Subscription {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new(models.Subscription)
|
||||
wm.Id = id
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
qry := tx.Model(wm)
|
||||
common.GenerateEmbed(qry, params.Embed).WherePK().Select()
|
||||
|
||||
if (*wm).HasNew() {
|
||||
as.SubToTrans(wm, tx)
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return wm
|
||||
}
|
||||
|
||||
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
@@ -69,6 +91,29 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
func (as *SubscriptionService) Edit(ctx context.Context, body *models.SubscriptionEdit, id string) *models.Subscription {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
tm := new(models.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)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(tm).WherePK().UpdateNotZero()
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tm
|
||||
}
|
||||
|
||||
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) {
|
||||
|
||||
now := time.Now()
|
||||
|
||||
Reference in New Issue
Block a user