package data import ( "fmt" "time" "github.com/spf13/viper" "gorm.io/driver/postgres" "gorm.io/gorm" ) type ModelBase struct { ID uint `gorm:"primarykey" json:"id"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"` } type Bonus struct { ModelBase Goal float64 `json:"goal"` Percent float64 `json:"percent"` RewardFundID uint `json:"rewardFundID"` } type Queue struct { ModelBase Name string `gorm:"type:varchar(50)" json:"name"` Orders []QueueOrder `json:"order"` } type RewardFund struct { ModelBase Asset string `json:"asset"` FundWallet string `json:"fundWallet"` SellingWallet string `json:"sellingWallet"` IssuerWallet string `json:"issuerWallet"` Memo string `json:"memo"` Price float64 `gorm:"type:decimal(19,7)" json:"price"` AmountAvailable float64 `gorm:"type:decimal(19,7)" json:"amountAvailable"` MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"` Contributions []Contribution `json:"contributions"` Title string `gorm:"type:varchar(50)" json:"title"` Description string `gorm:"type:text" json:"description"` Bonuses []Bonus `json:"bonuses"` QueueOrder QueueOrder `json:"queueOrder"` } type QueueOrder struct { ModelBase QueueID uint `json:"queueID"` RewardFundID uint `json:"rewardFundID"` Order uint16 `gorm:"type:smallint" json:"order"` } type Contribution struct { ModelBase Wallet string `json:"wallet"` Amount float64 `gorm:"type:decimal(19,7)" json:"amount"` TransactionID string `json:"transactionID"` RewardFundID uint `json:"rewardFundID"` } type User struct { ModelBase Username string `json:"username"` Password string `json:"password"` Privileges uint `json:"admin"` } var Db *gorm.DB func InitializeDatabase() { var err error dcs := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s", viper.GetString("database.host"), viper.GetString("database.user"), viper.GetString("database.password"), viper.GetString("database.name"), viper.GetInt("database.port"), viper.GetString("database.ssl")) Db, err = gorm.Open(postgres.Open(dcs), &gorm.Config{}) if err != nil { panic("Could not open database") } err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, QueueOrder{}, Contribution{}, Bonus{}) if err != nil { panic("Could not migrate database") } }