subscription controller, service and model

This commit is contained in:
Fran Jurmanović
2021-06-16 21:33:26 +02:00
parent ebcb507629
commit 150339628e
12 changed files with 347 additions and 0 deletions

View File

@@ -12,12 +12,19 @@ func Start(conn *pg.DB) error {
walletsMigration := migrations.WalletsMigration{Db: conn}
transactionTypesMigration := migrations.TransactionTypesMigration{Db: conn}
transactionsMigration := migrations.TransactionsMigration{Db: conn}
subscriptionTypesMigration := migrations.SubscriptionTypesMigration{Db: conn}
subscriptionsMigration := migrations.SubscriptionsMigration{Db: conn}
err := apiMigration.Create()
err = usersMigration.Create()
err = walletsMigration.Create()
err = transactionTypesMigration.Create()
err = transactionsMigration.Create()
err = subscriptionTypesMigration.Create()
err = subscriptionsMigration.Create()
err = transactionTypesMigration.Populate()
err = subscriptionTypesMigration.Populate()
return err
}

View File

@@ -0,0 +1,67 @@
package migrations
import (
"fmt"
"log"
"wallet-api/pkg/models"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
type SubscriptionTypesMigration struct {
Db *pg.DB
}
func (am *SubscriptionTypesMigration) Create() error {
models := []interface{}{
(*models.SubscriptionType)(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)
return err
} else {
fmt.Println("Table created successfully")
}
}
return nil
}
func (am *SubscriptionTypesMigration) Populate() error {
daily := new(models.SubscriptionType)
weekly := new(models.SubscriptionType)
monthly := new(models.SubscriptionType)
yearly := new(models.SubscriptionType)
daily.Init()
daily.Name = "Daily"
daily.Type = "daily"
weekly.Init()
weekly.Name = "Weekly"
weekly.Type = "weekly"
monthly.Init()
monthly.Name = "Monthly"
monthly.Type = "monthly"
yearly.Init()
yearly.Name = "Yearly"
yearly.Type = "yearly"
_, err := am.Db.Model(daily).Where("? = ?", pg.Ident("type"), daily.Type).SelectOrInsert()
_, err = am.Db.Model(weekly).Where("? = ?", pg.Ident("type"), weekly.Type).SelectOrInsert()
_, err = am.Db.Model(monthly).Where("? = ?", pg.Ident("type"), monthly.Type).SelectOrInsert()
_, err = am.Db.Model(yearly).Where("? = ?", pg.Ident("type"), yearly.Type).SelectOrInsert()
return err
}

View File

@@ -0,0 +1,34 @@
package migrations
import (
"fmt"
"log"
"wallet-api/pkg/models"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
type SubscriptionsMigration struct {
Db *pg.DB
}
func (am *SubscriptionsMigration) Create() error {
models := []interface{}{
(*models.Subscription)(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)
return err
} else {
fmt.Println("Table created successfully")
}
}
return nil
}

View File

@@ -32,3 +32,22 @@ func (am *TransactionTypesMigration) Create() error {
}
return nil
}
func (am *TransactionTypesMigration) Populate() error {
gain := new(models.TransactionType)
expense := new(models.TransactionType)
gain.Init()
gain.Name = "Gain"
gain.Type = "gain"
expense.Init()
expense.Name = "Expense"
expense.Type = "expense"
_, err := am.Db.Model(gain).Where("? = ?", pg.Ident("type"), gain.Type).SelectOrInsert()
_, err = am.Db.Model(expense).Where("? = ?", pg.Ident("type"), expense.Type).SelectOrInsert()
return err
}