added migration route

This commit is contained in:
Fran Jurmanović
2021-05-25 21:36:41 +02:00
parent 7257684d19
commit 8a9759f66e
8 changed files with 50 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
package controllers package controllers
import ( import (
"wallet-api/pkg/middleware"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -15,6 +16,7 @@ func NewApiController(as *services.ApiService, s *gin.RouterGroup) *ApiControlle
ac.ApiService = as ac.ApiService = as
s.GET("", ac.getFirst) s.GET("", ac.getFirst)
s.POST("migrate", middleware.Auth, ac.postMigrate)
return ac return ac
} }
@@ -23,3 +25,13 @@ func (ac *ApiController) getFirst(c *gin.Context) {
apiModel := ac.ApiService.GetFirst() apiModel := ac.ApiService.GetFirst()
c.JSON(200, apiModel) c.JSON(200, apiModel)
} }
func (ac *ApiController) postMigrate(c *gin.Context) {
mr, er := ac.ApiService.PostMigrate()
if er.Message != "" {
c.JSON(er.StatusCode, er)
} else {
c.JSON(200, mr)
}
}

View File

@@ -6,16 +6,18 @@ import (
"github.com/go-pg/pg/v10" "github.com/go-pg/pg/v10"
) )
func Start(conn *pg.DB) { func Start(conn *pg.DB) error {
apiMigration := migrations.ApiMigration{Db: conn} apiMigration := migrations.ApiMigration{Db: conn}
usersMigration := migrations.UsersMigration{Db: conn} usersMigration := migrations.UsersMigration{Db: conn}
walletsMigration := migrations.WalletsMigration{Db: conn} walletsMigration := migrations.WalletsMigration{Db: conn}
transactionTypesMigration := migrations.TransactionTypesMigration{Db: conn} transactionTypesMigration := migrations.TransactionTypesMigration{Db: conn}
transactionsMigration := migrations.TransactionsMigration{Db: conn} transactionsMigration := migrations.TransactionsMigration{Db: conn}
apiMigration.Create() err := apiMigration.Create()
usersMigration.Create() err = usersMigration.Create()
walletsMigration.Create() err = walletsMigration.Create()
transactionTypesMigration.Create() err = transactionTypesMigration.Create()
transactionsMigration.Create() err = transactionsMigration.Create()
return err
} }

View File

@@ -13,7 +13,7 @@ type ApiMigration struct {
Db *pg.DB Db *pg.DB
} }
func (am *ApiMigration) Create() { func (am *ApiMigration) Create() error {
models := []interface{}{ models := []interface{}{
(*models.ApiModel)(nil), (*models.ApiModel)(nil),
} }
@@ -24,8 +24,10 @@ func (am *ApiMigration) Create() {
}) })
if err != nil { if err != nil {
log.Printf("Error Creating Table: %s", err) log.Printf("Error Creating Table: %s", err)
return err
} else { } else {
fmt.Println("Table created successfully") fmt.Println("Table created successfully")
} }
} }
return nil
} }

View File

@@ -13,7 +13,7 @@ type TransactionTypesMigration struct {
Db *pg.DB Db *pg.DB
} }
func (am *TransactionTypesMigration) Create() { func (am *TransactionTypesMigration) Create() error {
models := []interface{}{ models := []interface{}{
(*models.TransactionType)(nil), (*models.TransactionType)(nil),
} }
@@ -25,8 +25,10 @@ func (am *TransactionTypesMigration) Create() {
}) })
if err != nil { if err != nil {
log.Printf("Error Creating Table: %s", err) log.Printf("Error Creating Table: %s", err)
return err
} else { } else {
fmt.Println("Table created successfully") fmt.Println("Table created successfully")
} }
} }
return nil
} }

View File

@@ -13,7 +13,7 @@ type TransactionsMigration struct {
Db *pg.DB Db *pg.DB
} }
func (am *TransactionsMigration) Create() { func (am *TransactionsMigration) Create() error {
models := []interface{}{ models := []interface{}{
(*models.Transaction)(nil), (*models.Transaction)(nil),
} }
@@ -25,8 +25,10 @@ func (am *TransactionsMigration) Create() {
}) })
if err != nil { if err != nil {
log.Printf("Error Creating Table: %s", err) log.Printf("Error Creating Table: %s", err)
return err
} else { } else {
fmt.Println("Table created successfully") fmt.Println("Table created successfully")
} }
} }
return nil
} }

View File

@@ -13,7 +13,7 @@ type UsersMigration struct {
Db *pg.DB Db *pg.DB
} }
func (am *UsersMigration) Create() { func (am *UsersMigration) Create() error {
models := []interface{}{ models := []interface{}{
(*models.User)(nil), (*models.User)(nil),
} }
@@ -24,8 +24,10 @@ func (am *UsersMigration) Create() {
}) })
if err != nil { if err != nil {
log.Printf("Error Creating Table: %s", err) log.Printf("Error Creating Table: %s", err)
return err
} else { } else {
fmt.Println("Table created successfully") fmt.Println("Table created successfully")
} }
} }
return nil
} }

View File

@@ -13,7 +13,7 @@ type WalletsMigration struct {
Db *pg.DB Db *pg.DB
} }
func (am *WalletsMigration) Create() { func (am *WalletsMigration) Create() error {
models := []interface{}{ models := []interface{}{
(*models.Wallet)(nil), (*models.Wallet)(nil),
} }
@@ -25,8 +25,10 @@ func (am *WalletsMigration) Create() {
}) })
if err != nil { if err != nil {
log.Printf("Error Creating Table: %s", err) log.Printf("Error Creating Table: %s", err)
return err
} else { } else {
fmt.Println("Table created successfully") fmt.Println("Table created successfully")
} }
} }
return nil
} }

View File

@@ -1,6 +1,7 @@
package services package services
import ( import (
"wallet-api/pkg/migrate"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"github.com/go-pg/pg/v10" "github.com/go-pg/pg/v10"
@@ -15,3 +16,17 @@ func (as *ApiService) GetFirst() models.ApiModel {
as.Db.Model(&apiModel).First() as.Db.Model(&apiModel).First()
return apiModel return apiModel
} }
func (as *ApiService) PostMigrate() (*models.MessageResponse, *models.Exception) {
mr := new(models.MessageResponse)
er := new(models.Exception)
err := migrate.Start(as.Db)
if err != nil {
er.ErrorCode = "400999"
er.StatusCode = 400
er.Message = err.Error()
}
return mr, er
}