mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
WA-10 - Implemented transactions
This commit is contained in:
42
pkg/controllers/transactionTypes.go
Normal file
42
pkg/controllers/transactionTypes.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 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)
|
||||
}
|
||||
43
pkg/controllers/transactions.go
Normal file
43
pkg/controllers/transactions.go
Normal file
@@ -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)
|
||||
}
|
||||
32
pkg/migrate/migrations/transactionTypes.go
Normal file
32
pkg/migrate/migrations/transactionTypes.go
Normal file
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
32
pkg/migrate/migrations/transactions.go
Normal file
32
pkg/migrate/migrations/transactions.go
Normal file
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
13
pkg/models/transactionTypes.go
Normal file
13
pkg/models/transactionTypes.go
Normal file
@@ -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"`
|
||||
}
|
||||
21
pkg/models/transactions.go
Normal file
21
pkg/models/transactions.go
Normal file
@@ -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"`
|
||||
}
|
||||
33
pkg/services/transactionTypes.go
Normal file
33
pkg/services/transactionTypes.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 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
|
||||
}
|
||||
35
pkg/services/transactions.go
Normal file
35
pkg/services/transactions.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user