diff --git a/pkg/controllers/transactionTypes.go b/pkg/controllers/transactionTypes.go new file mode 100644 index 0000000..22187f0 --- /dev/null +++ b/pkg/controllers/transactionTypes.go @@ -0,0 +1,42 @@ +package controllers + +import ( + "net/http" + "wallet-api/pkg/models" + "wallet-api/pkg/services" + + "github.com/gin-gonic/gin" +) + +type TransactionTypeController struct { + TransactionTypeService *services.TransactionTypeService +} + +func NewTransactionTypeController(as *services.TransactionTypeService, s *gin.RouterGroup) *TransactionTypeController { + wc := new(TransactionTypeController) + wc.TransactionTypeService = as + + s.POST("", wc.New) + s.GET("", wc.GetAll) + + return wc +} + +func (wc *TransactionTypeController) New(c *gin.Context) { + body := new(models.NewTransactionTypeBody) + if err := c.ShouldBindJSON(body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + wm := wc.TransactionTypeService.New(body) + c.JSON(200, wm) +} + +func (wc *TransactionTypeController) GetAll(c *gin.Context) { + embed, _ := c.GetQuery("embed") + + wm := wc.TransactionTypeService.GetAll(embed) + + c.JSON(200, wm) +} diff --git a/pkg/controllers/transactions.go b/pkg/controllers/transactions.go new file mode 100644 index 0000000..3a50c19 --- /dev/null +++ b/pkg/controllers/transactions.go @@ -0,0 +1,43 @@ +package controllers + +import ( + "net/http" + "wallet-api/pkg/models" + "wallet-api/pkg/services" + + "github.com/gin-gonic/gin" +) + +type TransactionController struct { + TransactionService *services.TransactionService +} + +func NewTransactionController(as *services.TransactionService, s *gin.RouterGroup) *TransactionController { + wc := new(TransactionController) + wc.TransactionService = as + + s.POST("", wc.New) + s.GET("", wc.GetAll) + + return wc +} + +func (wc *TransactionController) New(c *gin.Context) { + body := new(models.NewTransactionBody) + if err := c.ShouldBindJSON(body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + wm := wc.TransactionService.New(body) + c.JSON(200, wm) +} + +func (wc *TransactionController) GetAll(c *gin.Context) { + embed, _ := c.GetQuery("embed") + wallet, _ := c.GetQuery("walletId") + + wm := wc.TransactionService.GetAll(wallet, embed) + + c.JSON(200, wm) +} diff --git a/pkg/migrate/migrations/transactionTypes.go b/pkg/migrate/migrations/transactionTypes.go new file mode 100644 index 0000000..5e16c8b --- /dev/null +++ b/pkg/migrate/migrations/transactionTypes.go @@ -0,0 +1,32 @@ +package migrations + +import ( + "fmt" + "log" + "wallet-api/pkg/models" + + "github.com/go-pg/pg/v10" + "github.com/go-pg/pg/v10/orm" +) + +type TransactionTypesMigration struct { + Db *pg.DB +} + +func (am *TransactionTypesMigration) Create() { + models := []interface{}{ + (*models.TransactionType)(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) + } else { + fmt.Println("Table created successfully") + } + } +} diff --git a/pkg/migrate/migrations/transactions.go b/pkg/migrate/migrations/transactions.go new file mode 100644 index 0000000..4df2047 --- /dev/null +++ b/pkg/migrate/migrations/transactions.go @@ -0,0 +1,32 @@ +package migrations + +import ( + "fmt" + "log" + "wallet-api/pkg/models" + + "github.com/go-pg/pg/v10" + "github.com/go-pg/pg/v10/orm" +) + +type TransactionsMigration struct { + Db *pg.DB +} + +func (am *TransactionsMigration) Create() { + models := []interface{}{ + (*models.Transaction)(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) + } else { + fmt.Println("Table created successfully") + } + } +} diff --git a/pkg/models/transactionTypes.go b/pkg/models/transactionTypes.go new file mode 100644 index 0000000..f6e2932 --- /dev/null +++ b/pkg/models/transactionTypes.go @@ -0,0 +1,13 @@ +package models + +type TransactionType struct { + tableName struct{} `pg:"transactionTypes,alias:transactionTypes"` + BaseModel + Name string `json:"name" pg:"name"` + Type string `json:"type" pg:"type"` +} + +type NewTransactionTypeBody struct { + Name string `json:"name"` + Type string `json:"type"` +} diff --git a/pkg/models/transactions.go b/pkg/models/transactions.go new file mode 100644 index 0000000..278deea --- /dev/null +++ b/pkg/models/transactions.go @@ -0,0 +1,21 @@ +package models + +import "time" + +type Transaction struct { + tableName struct{} `pg:"transactions,alias:transactions"` + BaseModel + Description string `json:"description" pg:"description"` + TransactionTypeID string `json:"transactionTypeId", pg:"transaction_type_id"` + TransactionType *TransactionType `json:"transactionType", pg:"rel:has-one, fk:transaction_type_id"` + WalletID string `json:"walletId", pg:"wallet_id"` + Wallet *Wallet `json:"wallet" pg:"rel:has-one, fk:wallet_id"` + TransactionDate time.Time `json:"transactionDate" pg:"transaction_date"` +} + +type NewTransactionBody struct { + WalletID string `json:"walletId"` + TransactionTypeID string `json:"transactionTypeId"` + TransactionDate time.Time `json:"transactionDate"` + Description string `json:"description"` +} diff --git a/pkg/services/transactionTypes.go b/pkg/services/transactionTypes.go new file mode 100644 index 0000000..dcaa2c9 --- /dev/null +++ b/pkg/services/transactionTypes.go @@ -0,0 +1,33 @@ +package services + +import ( + "wallet-api/pkg/models" + "wallet-api/pkg/utl/common" + + "github.com/go-pg/pg/v10" +) + +type TransactionTypeService struct { + Db *pg.DB +} + +func (as *TransactionTypeService) New(body *models.NewTransactionTypeBody) *models.TransactionType { + tm := new(models.TransactionType) + + tm.Init() + tm.Name = body.Name + tm.Type = body.Type + + as.Db.Model(tm).Insert() + + return tm +} + +func (as *TransactionTypeService) GetAll(embed string) *[]models.TransactionType { + wm := new([]models.TransactionType) + + query := as.Db.Model(wm) + common.GenerateEmbed(query, embed).Select() + + return wm +} diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go new file mode 100644 index 0000000..11b25b7 --- /dev/null +++ b/pkg/services/transactions.go @@ -0,0 +1,35 @@ +package services + +import ( + "wallet-api/pkg/models" + "wallet-api/pkg/utl/common" + + "github.com/go-pg/pg/v10" +) + +type TransactionService struct { + Db *pg.DB +} + +func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction { + tm := new(models.Transaction) + + tm.Init() + tm.WalletID = body.WalletID + tm.TransactionTypeID = body.TransactionTypeID + tm.Description = body.Description + tm.TransactionDate = body.TransactionDate + + as.Db.Model(tm).Insert() + + return tm +} + +func (as *TransactionService) GetAll(walletId string, embed string) *[]models.Transaction { + wm := new([]models.Transaction) + + query := as.Db.Model(wm).Where("? = ?", pg.Ident("wallet_id"), walletId) + common.GenerateEmbed(query, embed).Select() + + return wm +}