From f2bc3dc9fe58955a92a911529696333fd2a50b52 Mon Sep 17 00:00:00 2001 From: Fran Jurmanovic Date: Fri, 30 Jul 2021 18:28:44 +0200 Subject: [PATCH 1/5] added subscription edit --- pkg/controllers/subscriptions.go | 32 +++++++++++++++++++++++ pkg/models/models.go | 14 ++++++---- pkg/models/subscriptions.go | 14 ++++++++-- pkg/services/subscriptions.go | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 7 deletions(-) diff --git a/pkg/controllers/subscriptions.go b/pkg/controllers/subscriptions.go index eac96ad..60e7a49 100644 --- a/pkg/controllers/subscriptions.go +++ b/pkg/controllers/subscriptions.go @@ -17,7 +17,9 @@ func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGr wc.SubscriptionService = as s.POST("", wc.New) + s.PUT("/:id", wc.Edit) s.GET("", wc.GetAll) + s.GET("/:id", wc.Get) return wc } @@ -33,6 +35,36 @@ func (wc *SubscriptionController) New(c *gin.Context) { 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) { body := new(models.Auth) auth := c.MustGet("auth") diff --git a/pkg/models/models.go b/pkg/models/models.go index 96ff832..6b79356 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -4,11 +4,7 @@ import "github.com/gin-gonic/gin" type FilteredResponse struct { Items interface{} `json:"items"` - SortBy string `json:"sortBy"` - Embed string `json:"embed"` - Page int `json:"page"` - Rpp int `json:"rpp"` - TotalRecords int `json:"totalRecords"` + Params } type ResponseFunc func(*gin.Context) *[]interface{} @@ -16,3 +12,11 @@ type ResponseFunc func(*gin.Context) *[]interface{} type MessageResponse struct { 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"` +} \ No newline at end of file diff --git a/pkg/models/subscriptions.go b/pkg/models/subscriptions.go index 5384216..3d7e856 100644 --- a/pkg/models/subscriptions.go +++ b/pkg/models/subscriptions.go @@ -23,6 +23,16 @@ type Subscription struct { 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 { WalletID string `json:"walletId" form:"walletId"` TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"` @@ -60,7 +70,7 @@ func (cm *Subscription) HasNew() bool { } return false case "weekly": - lastDate := time.Now().AddDate(0, 0, -(7*cm.CustomRange)) + lastDate := time.Now().AddDate(0, 0, -(7 * cm.CustomRange)) if cm.LastTransactionDate.Before(lastDate) { return true } @@ -80,4 +90,4 @@ func (cm *Subscription) HasNew() bool { } } return true -} \ No newline at end of file +} diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index 8981c53..fcd930a 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -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() From 3c2cd4710f92a9ebef926cdeef91252f2bf6f91b Mon Sep 17 00:00:00 2001 From: Fran Jurmanovic Date: Fri, 30 Jul 2021 19:56:27 +0200 Subject: [PATCH 2/5] added subscription end --- pkg/controllers/subscriptions.go | 16 +++++++++++++++- pkg/middleware/cors.go | 2 +- pkg/services/subscriptions.go | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/pkg/controllers/subscriptions.go b/pkg/controllers/subscriptions.go index 60e7a49..b4c6f1c 100644 --- a/pkg/controllers/subscriptions.go +++ b/pkg/controllers/subscriptions.go @@ -16,6 +16,7 @@ func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGr wc := new(SubscriptionController) wc.SubscriptionService = as + s.PUT("/end/:id", wc.End) s.POST("", wc.New) s.PUT("/:id", wc.Edit) s.GET("", wc.GetAll) @@ -56,7 +57,7 @@ func (wc *SubscriptionController) Get(c *gin.Context) { body.Id = auth.(*models.Auth).Id id := c.Param("id") - + embed, _ := c.GetQuery("embed") params.Embed = embed @@ -65,6 +66,19 @@ func (wc *SubscriptionController) Get(c *gin.Context) { c.JSON(200, fr) } +func (wc *SubscriptionController) End(c *gin.Context) { + body := new(models.Auth) + + auth := c.MustGet("auth") + body.Id = auth.(*models.Auth).Id + + id := c.Param("id") + + fr := wc.SubscriptionService.End(c, id) + + c.JSON(200, fr) +} + func (wc *SubscriptionController) GetAll(c *gin.Context) { body := new(models.Auth) auth := c.MustGet("auth") diff --git a/pkg/middleware/cors.go b/pkg/middleware/cors.go index 23d62cc..55c7ece 100644 --- a/pkg/middleware/cors.go +++ b/pkg/middleware/cors.go @@ -8,7 +8,7 @@ func CORSMiddleware() gin.HandlerFunc { c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT") - + if c.Request.Method == "OPTIONS" { c.AbortWithStatus(204) return diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index fcd930a..3eb1c01 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -114,6 +114,24 @@ func (as *SubscriptionService) Edit(ctx context.Context, body *models.Subscripti return tm } +func (as *SubscriptionService) End(ctx context.Context, id string) *models.Subscription { + db := as.Db.WithContext(ctx) + + tm := new(models.Subscription) + tm.Id = id + tm.EndDate = time.Now() + tm.HasEnd = true + + 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() From 1297f48645f91a1f0c4551f97f4d154e38cb1c94 Mon Sep 17 00:00:00 2001 From: Fran Jurmanovic Date: Fri, 30 Jul 2021 22:26:33 +0200 Subject: [PATCH 3/5] transaction edit --- pkg/controllers/subscriptions.go | 2 +- pkg/controllers/transactions.go | 32 ++++++++++++++++++++++ pkg/models/transactions.go | 9 ++++++ pkg/services/transactions.go | 47 +++++++++++++++++++++++++++++++- 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/pkg/controllers/subscriptions.go b/pkg/controllers/subscriptions.go index b4c6f1c..30c4b0c 100644 --- a/pkg/controllers/subscriptions.go +++ b/pkg/controllers/subscriptions.go @@ -19,8 +19,8 @@ func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGr s.PUT("/end/:id", wc.End) s.POST("", wc.New) s.PUT("/:id", wc.Edit) - s.GET("", wc.GetAll) s.GET("/:id", wc.Get) + s.GET("", wc.GetAll) return wc } diff --git a/pkg/controllers/transactions.go b/pkg/controllers/transactions.go index b800691..540751d 100644 --- a/pkg/controllers/transactions.go +++ b/pkg/controllers/transactions.go @@ -18,6 +18,8 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou s.POST("", wc.New) s.GET("", wc.GetAll) + s.PUT("/:id", wc.Edit) + s.GET("/:id", wc.Get) return wc } @@ -45,3 +47,33 @@ func (wc *TransactionController) GetAll(c *gin.Context) { c.JSON(200, fr) } + +func (wc *TransactionController) Edit(c *gin.Context) { + body := new(models.TransactionEdit) + if err := c.ShouldBind(body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + id := c.Param("id") + + wm := wc.TransactionService.Edit(c, body, id) + c.JSON(200, wm) +} + +func (wc *TransactionController) 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.TransactionService.Get(c, body, id, params) + + c.JSON(200, fr) +} diff --git a/pkg/models/transactions.go b/pkg/models/transactions.go index 6dd9de9..2076a3c 100644 --- a/pkg/models/transactions.go +++ b/pkg/models/transactions.go @@ -26,3 +26,12 @@ type NewTransactionBody struct { Description string `json:"description" form:"description"` Amount json.Number `json:"amount" form:"amount"` } + +type TransactionEdit struct { + Id string `json:"id" form:"id"` + WalletID string `json:"walletId" form:"walletId"` + TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"` + TransactionDate time.Time `json:"transactionDate" form:"transactionDate"` + Description string `json:"description" form:"description"` + Amount json.Number `json:"amount" form:"amount"` +} diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index a7406af..0e04e9d 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -5,6 +5,7 @@ import ( "math" "time" "wallet-api/pkg/models" + "wallet-api/pkg/utl/common" "github.com/go-pg/pg/v10" ) @@ -19,6 +20,9 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti tm := new(models.Transaction) + tx, _ := db.Begin() + defer tx.Rollback() + amount, _ := body.Amount.Float64() tm.Init() @@ -32,7 +36,8 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti tm.TransactionDate = time.Now() } - db.Model(tm).Insert() + tx.Model(tm).Insert() + tx.Commit() return tm } @@ -67,3 +72,43 @@ func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walle tx.Commit() } + +func (as *TransactionService) Edit(ctx context.Context, body *models.TransactionEdit, id string) *models.Transaction { + db := as.Db.WithContext(ctx) + + amount, _ := body.Amount.Float64() + + tm := new(models.Transaction) + tm.Id = id + tm.Description = body.Description + tm.WalletID = body.WalletID + tm.TransactionTypeID = body.TransactionTypeID + tm.TransactionDate = body.TransactionDate + 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 *TransactionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) *models.Transaction { + db := as.Db.WithContext(ctx) + + wm := new(models.Transaction) + wm.Id = id + + tx, _ := db.Begin() + defer tx.Rollback() + + qry := tx.Model(wm) + common.GenerateEmbed(qry, params.Embed).WherePK().Select() + + tx.Commit() + + return wm +} From 149fbfa21199de3a0ead312884d6764a4b515479 Mon Sep 17 00:00:00 2001 From: Fran Jurmanovic Date: Fri, 30 Jul 2021 23:40:23 +0200 Subject: [PATCH 4/5] wallet edit --- pkg/controllers/wallets.go | 41 ++++++++++++++++++++++++++------------ pkg/models/wallets.go | 5 +++++ pkg/services/wallets.go | 30 ++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/pkg/controllers/wallets.go b/pkg/controllers/wallets.go index 62c266f..12e865b 100644 --- a/pkg/controllers/wallets.go +++ b/pkg/controllers/wallets.go @@ -18,6 +18,8 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle s.POST("", wc.New) s.GET("", wc.GetAll) + s.PUT("/:id", wc.Edit) + s.GET("/:id", wc.Get) return wc } @@ -37,18 +39,6 @@ func (wc *WalletsController) New(c *gin.Context) { c.JSON(200, wm) } -func (wc *WalletsController) Get(c *gin.Context) { - body := new(models.Auth) - - embed, _ := c.GetQuery("embed") - auth := c.MustGet("auth") - body.Id = auth.(*models.Auth).Id - - wm := wc.WalletService.Get(c, body, embed) - - c.JSON(200, wm) -} - func (wc *WalletsController) GetAll(c *gin.Context) { body := new(models.Auth) auth := c.MustGet("auth") @@ -59,5 +49,30 @@ func (wc *WalletsController) GetAll(c *gin.Context) { wc.WalletService.GetAll(c, body, fr) c.JSON(200, fr) - +} + +func (wc *WalletsController) Edit(c *gin.Context) { + body := new(models.WalletEdit) + if err := c.ShouldBind(body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + id := c.Param("id") + + wm := wc.WalletService.Edit(c, body, id) + c.JSON(200, wm) +} + +func (wc *WalletsController) Get(c *gin.Context) { + params := new(models.Params) + + id := c.Param("id") + + embed, _ := c.GetQuery("embed") + params.Embed = embed + + fr := wc.WalletService.Get(c, id, params) + + c.JSON(200, fr) } diff --git a/pkg/models/wallets.go b/pkg/models/wallets.go index b421d4f..c45ed24 100644 --- a/pkg/models/wallets.go +++ b/pkg/models/wallets.go @@ -28,3 +28,8 @@ type WalletTransactions struct { LastMonth float32 NextMonth float32 } + +type WalletEdit struct { + Id string `json:"id" form:"id"` + Name string `json:"name" form:"name"` +} diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index e5b55fb..6ab4e44 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -25,13 +25,36 @@ func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) *mod return walletModel } -func (as *WalletService) Get(ctx context.Context, am *models.Auth, embed string) *models.Wallet { +func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id string) *models.Wallet { + db := as.Db.WithContext(ctx) + + tm := new(models.Wallet) + tm.Id = id + tm.Name = body.Name + + tx, _ := db.Begin() + defer tx.Rollback() + + tx.Model(tm).WherePK().UpdateNotZero() + + tx.Commit() + + return tm +} + +func (as *WalletService) Get(ctx context.Context, id string, params *models.Params) *models.Wallet { db := as.Db.WithContext(ctx) wm := new(models.Wallet) + wm.Id = id - query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id) - common.GenerateEmbed(query, embed).Select() + tx, _ := db.Begin() + defer tx.Rollback() + + qry := tx.Model(wm) + common.GenerateEmbed(qry, params.Embed).WherePK().Select() + + tx.Commit() return wm } @@ -148,7 +171,6 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI wm.Currency = "USD" wm.WalletId = walletId - return wm } From 26e39322e41c72b583ba9ca708b16327a351baa1 Mon Sep 17 00:00:00 2001 From: Fran Jurmanovic Date: Mon, 2 Aug 2021 04:31:09 -0700 Subject: [PATCH 5/5] fixes --- pkg/api/routes.go | 5 +++++ pkg/controllers/subscriptions.go | 14 ++++++++++++-- pkg/middleware/cors.go | 2 +- pkg/models/subscriptions.go | 4 ++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 5d7eec7..2fdb9a1 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -21,6 +21,11 @@ func Routes(s *gin.Engine, db *pg.DB) { transactionType := ver.Group("transaction-type", middleware.Auth) subscription := ver.Group("subscription", middleware.Auth) subscriptionType := ver.Group("subscription-type", middleware.Auth) + + s.NoRoute(func(c *gin.Context) { + c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"}) + }) + apiService := services.ApiService{Db: db} usersService := services.UsersService{Db: db} diff --git a/pkg/controllers/subscriptions.go b/pkg/controllers/subscriptions.go index 30c4b0c..fd41880 100644 --- a/pkg/controllers/subscriptions.go +++ b/pkg/controllers/subscriptions.go @@ -16,12 +16,16 @@ func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGr wc := new(SubscriptionController) wc.SubscriptionService = as - s.PUT("/end/:id", wc.End) s.POST("", wc.New) s.PUT("/:id", wc.Edit) s.GET("/:id", wc.Get) s.GET("", wc.GetAll) + se := s.Group("/end") + { + se.POST("", wc.End) + } + return wc } @@ -72,7 +76,13 @@ func (wc *SubscriptionController) End(c *gin.Context) { auth := c.MustGet("auth") body.Id = auth.(*models.Auth).Id - id := c.Param("id") + end := new(models.SubscriptionEnd) + if err := c.ShouldBind(end); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + id := end.Id fr := wc.SubscriptionService.End(c, id) diff --git a/pkg/middleware/cors.go b/pkg/middleware/cors.go index 55c7ece..23d62cc 100644 --- a/pkg/middleware/cors.go +++ b/pkg/middleware/cors.go @@ -8,7 +8,7 @@ func CORSMiddleware() gin.HandlerFunc { c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT") - + if c.Request.Method == "OPTIONS" { c.AbortWithStatus(204) return diff --git a/pkg/models/subscriptions.go b/pkg/models/subscriptions.go index 3d7e856..ce7f29f 100644 --- a/pkg/models/subscriptions.go +++ b/pkg/models/subscriptions.go @@ -45,6 +45,10 @@ type NewSubscriptionBody struct { Amount json.Number `json:"amount" form:"amount"` } +type SubscriptionEnd struct { + Id string `json:"id" form:"id"` +} + func (cm *Subscription) ToTrans() *Transaction { trans := new(Transaction) trans.Init()