mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
subscription controller, service and model
This commit is contained in:
@@ -19,12 +19,16 @@ func Routes(s *gin.Engine, db *pg.DB) {
|
||||
walletHeader := ver.Group("wallet/wallet-header", middleware.Auth)
|
||||
transaction := ver.Group("transaction", middleware.Auth)
|
||||
transactionType := ver.Group("transaction-type", middleware.Auth)
|
||||
subscription := ver.Group("subscription", middleware.Auth)
|
||||
subscriptionType := ver.Group("subscription-type", middleware.Auth)
|
||||
|
||||
apiService := services.ApiService{Db: db}
|
||||
usersService := services.UsersService{Db: db}
|
||||
walletService := services.WalletService{Db: db}
|
||||
transactionService := services.TransactionService{Db: db}
|
||||
transactionTypeService := services.TransactionTypeService{Db: db}
|
||||
subscriptionService := services.SubscriptionService{Db: db}
|
||||
subscriptionTypeService := services.SubscriptionTypeService{Db: db}
|
||||
|
||||
controllers.NewApiController(&apiService, api)
|
||||
controllers.NewAuthController(&usersService, auth)
|
||||
@@ -32,4 +36,6 @@ func Routes(s *gin.Engine, db *pg.DB) {
|
||||
controllers.NewWalletsHeaderController(&walletService, walletHeader)
|
||||
controllers.NewTransactionController(&transactionService, transaction)
|
||||
controllers.NewTransactionTypeController(&transactionTypeService, transactionType)
|
||||
controllers.NewSubscriptionController(&subscriptionService, subscription)
|
||||
controllers.NewSubscriptionTypeController(&subscriptionTypeService, subscriptionType)
|
||||
}
|
||||
|
||||
42
pkg/controllers/subscriptionTypes.go
Normal file
42
pkg/controllers/subscriptionTypes.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SubscriptionTypeController struct {
|
||||
SubscriptionTypeService *services.SubscriptionTypeService
|
||||
}
|
||||
|
||||
func NewSubscriptionTypeController(as *services.SubscriptionTypeService, s *gin.RouterGroup) *SubscriptionTypeController {
|
||||
wc := new(SubscriptionTypeController)
|
||||
wc.SubscriptionTypeService = as
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.GetAll)
|
||||
|
||||
return wc
|
||||
}
|
||||
|
||||
func (wc *SubscriptionTypeController) New(c *gin.Context) {
|
||||
body := new(models.NewSubscriptionTypeBody)
|
||||
if err := c.ShouldBind(body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.SubscriptionTypeService.New(body)
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
func (wc *SubscriptionTypeController) GetAll(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
|
||||
wm := wc.SubscriptionTypeService.GetAll(embed)
|
||||
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
47
pkg/controllers/subscriptions.go
Normal file
47
pkg/controllers/subscriptions.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SubscriptionController struct {
|
||||
SubscriptionService *services.SubscriptionService
|
||||
}
|
||||
|
||||
func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGroup) *SubscriptionController {
|
||||
wc := new(SubscriptionController)
|
||||
wc.SubscriptionService = as
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.GetAll)
|
||||
|
||||
return wc
|
||||
}
|
||||
|
||||
func (wc *SubscriptionController) New(c *gin.Context) {
|
||||
body := new(models.NewSubscriptionBody)
|
||||
if err := c.ShouldBind(body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.SubscriptionService.New(body)
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
func (wc *SubscriptionController) GetAll(c *gin.Context) {
|
||||
body := new(models.Auth)
|
||||
auth := c.MustGet("auth")
|
||||
body.Id = auth.(*models.Auth).Id
|
||||
|
||||
fr := FilteredResponse(c)
|
||||
wallet, _ := c.GetQuery("walletId")
|
||||
|
||||
wc.SubscriptionService.GetAll(body, wallet, fr)
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
@@ -12,12 +12,19 @@ func Start(conn *pg.DB) error {
|
||||
walletsMigration := migrations.WalletsMigration{Db: conn}
|
||||
transactionTypesMigration := migrations.TransactionTypesMigration{Db: conn}
|
||||
transactionsMigration := migrations.TransactionsMigration{Db: conn}
|
||||
subscriptionTypesMigration := migrations.SubscriptionTypesMigration{Db: conn}
|
||||
subscriptionsMigration := migrations.SubscriptionsMigration{Db: conn}
|
||||
|
||||
err := apiMigration.Create()
|
||||
err = usersMigration.Create()
|
||||
err = walletsMigration.Create()
|
||||
err = transactionTypesMigration.Create()
|
||||
err = transactionsMigration.Create()
|
||||
err = subscriptionTypesMigration.Create()
|
||||
err = subscriptionsMigration.Create()
|
||||
|
||||
err = transactionTypesMigration.Populate()
|
||||
err = subscriptionTypesMigration.Populate()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
67
pkg/migrate/migrations/subscriptionTypes.go
Normal file
67
pkg/migrate/migrations/subscriptionTypes.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type SubscriptionTypesMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *SubscriptionTypesMigration) Create() error {
|
||||
models := []interface{}{
|
||||
(*models.SubscriptionType)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (am *SubscriptionTypesMigration) Populate() error {
|
||||
daily := new(models.SubscriptionType)
|
||||
weekly := new(models.SubscriptionType)
|
||||
monthly := new(models.SubscriptionType)
|
||||
yearly := new(models.SubscriptionType)
|
||||
|
||||
daily.Init()
|
||||
daily.Name = "Daily"
|
||||
daily.Type = "daily"
|
||||
|
||||
weekly.Init()
|
||||
weekly.Name = "Weekly"
|
||||
weekly.Type = "weekly"
|
||||
|
||||
monthly.Init()
|
||||
monthly.Name = "Monthly"
|
||||
monthly.Type = "monthly"
|
||||
|
||||
yearly.Init()
|
||||
yearly.Name = "Yearly"
|
||||
yearly.Type = "yearly"
|
||||
|
||||
_, err := am.Db.Model(daily).Where("? = ?", pg.Ident("type"), daily.Type).SelectOrInsert()
|
||||
|
||||
_, err = am.Db.Model(weekly).Where("? = ?", pg.Ident("type"), weekly.Type).SelectOrInsert()
|
||||
|
||||
_, err = am.Db.Model(monthly).Where("? = ?", pg.Ident("type"), monthly.Type).SelectOrInsert()
|
||||
|
||||
_, err = am.Db.Model(yearly).Where("? = ?", pg.Ident("type"), yearly.Type).SelectOrInsert()
|
||||
|
||||
return err
|
||||
}
|
||||
34
pkg/migrate/migrations/subscriptions.go
Normal file
34
pkg/migrate/migrations/subscriptions.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type SubscriptionsMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *SubscriptionsMigration) Create() error {
|
||||
models := []interface{}{
|
||||
(*models.Subscription)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -32,3 +32,22 @@ func (am *TransactionTypesMigration) Create() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (am *TransactionTypesMigration) Populate() error {
|
||||
gain := new(models.TransactionType)
|
||||
expense := new(models.TransactionType)
|
||||
|
||||
gain.Init()
|
||||
gain.Name = "Gain"
|
||||
gain.Type = "gain"
|
||||
|
||||
expense.Init()
|
||||
expense.Name = "Expense"
|
||||
expense.Type = "expense"
|
||||
|
||||
_, err := am.Db.Model(gain).Where("? = ?", pg.Ident("type"), gain.Type).SelectOrInsert()
|
||||
|
||||
_, err = am.Db.Model(expense).Where("? = ?", pg.Ident("type"), expense.Type).SelectOrInsert()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
13
pkg/models/subscriptionTypes.go
Normal file
13
pkg/models/subscriptionTypes.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package models
|
||||
|
||||
type SubscriptionType struct {
|
||||
tableName struct{} `pg:"subscriptionTypes,alias:subscriptionTypes"`
|
||||
BaseModel
|
||||
Name string `json:"name" pg:"name"`
|
||||
Type string `json:"type" pg:"type"`
|
||||
}
|
||||
|
||||
type NewSubscriptionTypeBody struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Type string `json:"type" form:"type"`
|
||||
}
|
||||
32
pkg/models/subscriptions.go
Normal file
32
pkg/models/subscriptions.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Subscription struct {
|
||||
tableName struct{} `pg:"subscriptions,alias:subscriptions"`
|
||||
BaseModel
|
||||
Description string `json:"description" pg:"description"`
|
||||
StartDate time.Time `json:"startDate" pg:"start_date"`
|
||||
SubscriptionTypeID string `json:"subscriptionTypeId" pg:"subscription_type_id"`
|
||||
SubscriptionType *SubscriptionType `json:"subscriptionType", pg:"rel:has-one, fk:subscription_type_id"`
|
||||
CustomRange int `json:"customRange", pg:"custom_range"`
|
||||
WalletID string `json:"walletId", pg:"wallet_id"`
|
||||
Wallet *Wallet `json:"wallet" pg:"rel:has-one, fk:wallet_id"`
|
||||
TransactionTypeID string `json:"transactionTypeId", pg:"transaction_type_id"`
|
||||
TransactionType *TransactionType `json:"transactionType", pg:"rel:has-one, fk:transaction_type_id"`
|
||||
LastTransactionDate time.Time `json:"lastTransactionDate", pg:"last_transaction_date"`
|
||||
Amount int `json:"amount", pg:"amount"`
|
||||
}
|
||||
|
||||
type NewSubscriptionBody struct {
|
||||
WalletID string `json:"walletId" form:"walletId"`
|
||||
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
|
||||
SubscriptionTypeID string `json:"subscriptionTypeId" pg:"subscription_type_id"`
|
||||
CustomRange int `json:"customRange", pg:"custom_range"`
|
||||
StartDate time.Time `json:"startDate" pg:"start_date"`
|
||||
Description string `json:"description" form:"description"`
|
||||
Amount json.Number `json:"amount" form:"amount"`
|
||||
}
|
||||
@@ -15,6 +15,8 @@ type Transaction struct {
|
||||
Amount int `json:"amount", pg:"amount"`
|
||||
Wallet *Wallet `json:"wallet" pg:"rel:has-one, fk:wallet_id"`
|
||||
TransactionDate time.Time `json:"transactionDate" pg:"transaction_date"`
|
||||
SubscriptionID string `json:"subscriptionId", pg:"subscription_id"`
|
||||
Subscription *Subscription `json:"subscription", pg:"rel:has-one, fk:subscription_id"`
|
||||
}
|
||||
|
||||
type NewTransactionBody struct {
|
||||
|
||||
33
pkg/services/subscriptionTypes.go
Normal file
33
pkg/services/subscriptionTypes.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type SubscriptionTypeService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (as *SubscriptionTypeService) New(body *models.NewSubscriptionTypeBody) *models.SubscriptionType {
|
||||
tm := new(models.SubscriptionType)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Type = body.Type
|
||||
|
||||
as.Db.Model(tm).Insert()
|
||||
|
||||
return tm
|
||||
}
|
||||
|
||||
func (as *SubscriptionTypeService) GetAll(embed string) *[]models.SubscriptionType {
|
||||
wm := new([]models.SubscriptionType)
|
||||
|
||||
query := as.Db.Model(wm)
|
||||
common.GenerateEmbed(query, embed).Select()
|
||||
|
||||
return wm
|
||||
}
|
||||
45
pkg/services/subscriptions.go
Normal file
45
pkg/services/subscriptions.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type SubscriptionService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Subscription {
|
||||
tm := new(models.Subscription)
|
||||
|
||||
amount, _ := body.Amount.Int64()
|
||||
|
||||
tm.Init()
|
||||
tm.WalletID = body.WalletID
|
||||
tm.TransactionTypeID = body.TransactionTypeID
|
||||
tm.SubscriptionTypeID = body.SubscriptionTypeID
|
||||
tm.CustomRange = body.CustomRange
|
||||
tm.Description = body.Description
|
||||
tm.StartDate = body.StartDate
|
||||
tm.Amount = int(amount)
|
||||
|
||||
if body.StartDate.IsZero() {
|
||||
tm.StartDate = time.Now()
|
||||
}
|
||||
|
||||
as.Db.Model(tm).Insert()
|
||||
|
||||
return tm
|
||||
}
|
||||
|
||||
func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||
wm := new([]models.Subscription)
|
||||
|
||||
query := as.Db.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
||||
if walletId != "" {
|
||||
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
}
|
||||
FilteredResponse(query, wm, filtered)
|
||||
}
|
||||
Reference in New Issue
Block a user