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:
@@ -17,7 +17,9 @@ func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGr
|
|||||||
wc.SubscriptionService = as
|
wc.SubscriptionService = as
|
||||||
|
|
||||||
s.POST("", wc.New)
|
s.POST("", wc.New)
|
||||||
|
s.PUT("/:id", wc.Edit)
|
||||||
s.GET("", wc.GetAll)
|
s.GET("", wc.GetAll)
|
||||||
|
s.GET("/:id", wc.Get)
|
||||||
|
|
||||||
return wc
|
return wc
|
||||||
}
|
}
|
||||||
@@ -33,6 +35,36 @@ func (wc *SubscriptionController) New(c *gin.Context) {
|
|||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wc *SubscriptionController) Edit(c *gin.Context) {
|
||||||
|
body := new(models.SubscriptionEdit)
|
||||||
|
if err := c.ShouldBind(body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id := c.Param("id")
|
||||||
|
|
||||||
|
wm := wc.SubscriptionService.Edit(c, body, id)
|
||||||
|
c.JSON(200, wm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *SubscriptionController) Get(c *gin.Context) {
|
||||||
|
body := new(models.Auth)
|
||||||
|
params := new(models.Params)
|
||||||
|
|
||||||
|
auth := c.MustGet("auth")
|
||||||
|
body.Id = auth.(*models.Auth).Id
|
||||||
|
|
||||||
|
id := c.Param("id")
|
||||||
|
|
||||||
|
embed, _ := c.GetQuery("embed")
|
||||||
|
params.Embed = embed
|
||||||
|
|
||||||
|
fr := wc.SubscriptionService.Get(c, body, id, params)
|
||||||
|
|
||||||
|
c.JSON(200, fr)
|
||||||
|
}
|
||||||
|
|
||||||
func (wc *SubscriptionController) GetAll(c *gin.Context) {
|
func (wc *SubscriptionController) GetAll(c *gin.Context) {
|
||||||
body := new(models.Auth)
|
body := new(models.Auth)
|
||||||
auth := c.MustGet("auth")
|
auth := c.MustGet("auth")
|
||||||
|
|||||||
@@ -4,11 +4,7 @@ import "github.com/gin-gonic/gin"
|
|||||||
|
|
||||||
type FilteredResponse struct {
|
type FilteredResponse struct {
|
||||||
Items interface{} `json:"items"`
|
Items interface{} `json:"items"`
|
||||||
SortBy string `json:"sortBy"`
|
Params
|
||||||
Embed string `json:"embed"`
|
|
||||||
Page int `json:"page"`
|
|
||||||
Rpp int `json:"rpp"`
|
|
||||||
TotalRecords int `json:"totalRecords"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseFunc func(*gin.Context) *[]interface{}
|
type ResponseFunc func(*gin.Context) *[]interface{}
|
||||||
@@ -16,3 +12,11 @@ type ResponseFunc func(*gin.Context) *[]interface{}
|
|||||||
type MessageResponse struct {
|
type MessageResponse struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Params struct {
|
||||||
|
SortBy string `json:"sortBy"`
|
||||||
|
Embed string `json:"embed"`
|
||||||
|
Page int `json:"page"`
|
||||||
|
Rpp int `json:"rpp"`
|
||||||
|
TotalRecords int `json:"totalRecords"`
|
||||||
|
}
|
||||||
@@ -23,6 +23,16 @@ type Subscription struct {
|
|||||||
Amount float32 `json:"amount", pg:"amount,default:0"`
|
Amount float32 `json:"amount", pg:"amount,default:0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SubscriptionEdit struct {
|
||||||
|
tableName struct{} `pg:"subscriptions,alias:subscriptions"`
|
||||||
|
Id string `json:"id" form:"id"`
|
||||||
|
Description string `json:"description" form:"description"`
|
||||||
|
EndDate time.Time `json:"endDate" form:"endDate" `
|
||||||
|
HasEnd bool `json:"hasEnd" form:"hasEnd"`
|
||||||
|
WalletID string `json:"walletId" form:"walletId"`
|
||||||
|
Amount json.Number `json:"amount" form:"amount"`
|
||||||
|
}
|
||||||
|
|
||||||
type NewSubscriptionBody struct {
|
type NewSubscriptionBody struct {
|
||||||
WalletID string `json:"walletId" form:"walletId"`
|
WalletID string `json:"walletId" form:"walletId"`
|
||||||
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
|
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/utl/common"
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
"github.com/go-pg/pg/v10"
|
||||||
)
|
)
|
||||||
@@ -47,6 +48,27 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip
|
|||||||
return tm
|
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) {
|
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||||
db := as.Db.WithContext(ctx)
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
@@ -69,6 +91,29 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall
|
|||||||
tx.Commit()
|
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) {
|
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) {
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|||||||
Reference in New Issue
Block a user