added transaction status routes

This commit is contained in:
Fran Jurmanović
2021-09-05 11:40:21 +02:00
parent fa09e2ee08
commit 862aded788
9 changed files with 259 additions and 5 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 {