mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
added transaction status routes
This commit is contained in:
@@ -29,6 +29,7 @@ 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)
|
||||
transactionStatus := ver.Group("transaction-status", middleware.Auth)
|
||||
|
||||
s.NoRoute(func(c *gin.Context) {
|
||||
c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"})
|
||||
@@ -42,6 +43,7 @@ func Routes(s *gin.Engine, db *pg.DB) {
|
||||
transactionTypeService := services.TransactionTypeService{Db: db}
|
||||
subscriptionService := services.SubscriptionService{Db: db}
|
||||
subscriptionTypeService := services.SubscriptionTypeService{Db: db}
|
||||
transactionStatusService := services.TransactionStatusService{Db: db}
|
||||
|
||||
walletService.Ss = &subscriptionService
|
||||
transactionService.Ss = &subscriptionService
|
||||
@@ -54,4 +56,5 @@ func Routes(s *gin.Engine, db *pg.DB) {
|
||||
controllers.NewTransactionTypeController(&transactionTypeService, transactionType)
|
||||
controllers.NewSubscriptionController(&subscriptionService, subscription)
|
||||
controllers.NewSubscriptionTypeController(&subscriptionTypeService, subscriptionType)
|
||||
controllers.NewTransactionStatusController(&transactionStatusService, transactionStatus)
|
||||
}
|
||||
|
||||
@@ -1 +1,64 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TransactionStatusController struct {
|
||||
TransactionStatusService *services.TransactionStatusService
|
||||
}
|
||||
|
||||
/*
|
||||
NewTransactionStatusController
|
||||
|
||||
Initializes TransactionStatusController.
|
||||
Args:
|
||||
*services.TransactionStatusService: Transaction Staus service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*TransactionStatusController: Controller for "transaction-status" route interactions
|
||||
*/
|
||||
func NewTransactionStatusController(as *services.TransactionStatusService, s *gin.RouterGroup) *TransactionStatusController {
|
||||
wc := new(TransactionStatusController)
|
||||
wc.TransactionStatusService = as
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.GetAll)
|
||||
|
||||
return wc
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
Args:
|
||||
*gin.Context: Gin Application Context
|
||||
*/
|
||||
// ROUTE (POST /transaction-status)
|
||||
func (wc *TransactionStatusController) New(c *gin.Context) {
|
||||
body := new(models.NewTransactionStatusBody)
|
||||
if err := c.ShouldBind(body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
wm := wc.TransactionStatusService.New(c, body)
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
Args:
|
||||
*gin.Context: Gin Application Context
|
||||
*/
|
||||
// ROUTE (GET /transaction-status)
|
||||
func (wc *TransactionStatusController) GetAll(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
|
||||
wm := wc.TransactionStatusService.GetAll(c, embed)
|
||||
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
@@ -1 +1,40 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
/*
|
||||
CreateTableTransactionStatus
|
||||
|
||||
Creates transaction_status table if it does not exist.
|
||||
Args:
|
||||
*pg.DB: Postgres database client
|
||||
Returns:
|
||||
error: Returns if there is an error with table creation
|
||||
*/
|
||||
func CreateTableTransactionStatus(db pg.DB) error {
|
||||
models := []interface{}{
|
||||
(*models.TransactionStatus)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error creating table \"transaction_status\": %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Table \"transaction_status\" created successfully")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1 +1,62 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
/*
|
||||
PopulateTransactionStatus
|
||||
|
||||
Populates transaction_status table if it exists.
|
||||
Args:
|
||||
*pg.DB: Postgres database client
|
||||
Returns:
|
||||
error: Returns if there is an error with populating table
|
||||
*/
|
||||
func PopulateTransactionStatus(db pg.DB) error {
|
||||
completed := new(models.TransactionStatus)
|
||||
pending := new(models.TransactionStatus)
|
||||
deleted := new(models.TransactionStatus)
|
||||
|
||||
completed.Init()
|
||||
completed.Name = "Completed"
|
||||
completed.Status = "completed"
|
||||
|
||||
pending.Init()
|
||||
pending.Name = "Pending"
|
||||
pending.Status = "pending"
|
||||
|
||||
deleted.Init()
|
||||
deleted.Name = "Deleted"
|
||||
deleted.Status = "deleted"
|
||||
|
||||
_, err := db.Model(completed).Where("? = ?", pg.Ident("status"), completed.Status).SelectOrInsert()
|
||||
if err != nil {
|
||||
log.Printf("Error inserting row into \"transactionStatus\" table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Row inserted successfully into \"transactionStatus\" table.")
|
||||
}
|
||||
|
||||
_, err = db.Model(pending).Where("? = ?", pg.Ident("status"), pending.Status).SelectOrInsert()
|
||||
if err != nil {
|
||||
log.Printf("Error inserting row into \"transactionStatus\" table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Row inserted successfully into \"transactionStatus\" table.")
|
||||
}
|
||||
|
||||
_, err = db.Model(deleted).Where("? = ?", pg.Ident("status"), pending.Status).SelectOrInsert()
|
||||
if err != nil {
|
||||
log.Printf("Error inserting row into \"transactionStatus\" table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Row inserted successfully into \"transactionStatus\" table.")
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -33,10 +33,24 @@ func Start(conn *pg.DB, version string) {
|
||||
PopulateTransactionTypes,
|
||||
},
|
||||
}
|
||||
migration003 := Migration{
|
||||
Version: "003",
|
||||
Migrations: []interface{}{
|
||||
CreateTableTransactionStatus,
|
||||
},
|
||||
}
|
||||
migration004 := Migration{
|
||||
Version: "004",
|
||||
Migrations: []interface{}{
|
||||
PopulateTransactionStatus,
|
||||
},
|
||||
}
|
||||
|
||||
migrationsMap := []Migration{
|
||||
migration001,
|
||||
migration002,
|
||||
migration003,
|
||||
migration004,
|
||||
}
|
||||
|
||||
for _, migrationCol := range migrationsMap {
|
||||
|
||||
@@ -1 +1,13 @@
|
||||
package models
|
||||
|
||||
type TransactionStatus struct {
|
||||
tableName struct{} `pg:"transactionStatus,alias:transactionStatus"`
|
||||
BaseModel
|
||||
Name string `json:"name" pg:"name"`
|
||||
Status string `json:"status" pg:"status,notnull"`
|
||||
}
|
||||
|
||||
type NewTransactionStatusBody struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Status string `json:"status" form:"status"`
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ type Transaction struct {
|
||||
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"`
|
||||
TransactionStatusID string `json:"transactionStatusId", pg:"transaction_status_id"`
|
||||
TransactionStatus *TransactionStatus `json:"transactionStatus", pg:"rel:has-one, fk:transaction_status_id"`
|
||||
WalletID string `json:"walletId", pg:"wallet_id"`
|
||||
Amount float32 `json:"amount", pg:"amount,default:0"`
|
||||
Wallet *Wallet `json:"wallet" pg:"rel:has-one, fk:wallet_id"`
|
||||
|
||||
@@ -1 +1,58 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type TransactionStatusService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to transaction status table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewTransactionStatusBody: object to create
|
||||
Returns:
|
||||
*models.TransactionType: Transaction Type object from database.
|
||||
*/
|
||||
func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTransactionStatusBody) *models.TransactionStatus {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.TransactionStatus)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Status = body.Status
|
||||
|
||||
db.Model(tm).Insert()
|
||||
|
||||
return tm
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
|
||||
Gets all rows from transaction status table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.TransactionStatus: List of Transaction status objects from database.
|
||||
*/
|
||||
func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) *[]models.TransactionStatus {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.TransactionStatus)
|
||||
|
||||
query := db.Model(wm)
|
||||
common.GenerateEmbed(query, embed).Select()
|
||||
|
||||
return wm
|
||||
}
|
||||
|
||||
@@ -16,24 +16,26 @@ type TransactionService struct {
|
||||
}
|
||||
|
||||
/*
|
||||
GetAll
|
||||
New new row into transaction table
|
||||
|
||||
Gets all rows from subscription type table.
|
||||
Inserts
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
*models.NewTransactionBody: Transaction body object
|
||||
Returns:
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*models.Transaction: Transaction object
|
||||
*/
|
||||
// Inserts new row to transaction table.
|
||||
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) *models.Transaction {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.Transaction)
|
||||
transactionStatus := new(models.TransactionStatus)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select()
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
tm.Init()
|
||||
@@ -42,6 +44,7 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti
|
||||
tm.Description = body.Description
|
||||
tm.TransactionDate = body.TransactionDate
|
||||
tm.Amount = float32(math.Round(amount*100) / 100)
|
||||
tm.TransactionStatusID = transactionStatus.Id
|
||||
|
||||
if body.TransactionDate.IsZero() {
|
||||
tm.TransactionDate = time.Now()
|
||||
|
||||
Reference in New Issue
Block a user