The backend for the project formerly known as signet, now known as beignet.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB

  1. package data
  2. import (
  3. "fmt"
  4. "github.com/spf13/viper"
  5. "gorm.io/driver/postgres"
  6. "gorm.io/gorm"
  7. )
  8. type Bonus struct {
  9. Goal float64 `json:"goal"`
  10. Percent float64 `json:"percent"`
  11. RewardFundID uint `json:"rewardFundID"`
  12. }
  13. type RewardFund struct {
  14. gorm.Model
  15. Asset string `json:"asset"`
  16. FundWallet string `json:"fundWallet"`
  17. FundSecret string `json:"fundSecret"`
  18. IssuerWallet string `json:"issuerWallet"`
  19. Memo string `json:"memo"`
  20. Price float64 `json:"price"`
  21. AmountGoal float64 `gorm:"type:decimal(19,7)" json:"amountGoal"`
  22. MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"`
  23. Contributions []Contribution `json:"contributions"`
  24. Title string `gorm:"type:varchar(50)" json:"title"`
  25. Description string `gorm:"type:text" json:"description"`
  26. Bonuses []Bonus `json:"bonuses"`
  27. }
  28. type Contribution struct {
  29. gorm.Model
  30. Wallet string `json:"wallet"`
  31. Amount float64 `gorm:"type:decimal(19,7)" json:"amount"`
  32. TransactionID string `json:"transactionID"`
  33. RewardFundID uint `json:"rewardFundID"`
  34. Tags []AppliedTag `json:"tags"`
  35. }
  36. type Tag struct {
  37. gorm.Model
  38. Description string `json:"description"`
  39. Active bool `json:"active"`
  40. Contribution AppliedTag `json:"contribution"`
  41. }
  42. type AppliedTag struct {
  43. gorm.Model
  44. TagID uint `json:"tagID"`
  45. ContributionID uint `json:"contributionID"`
  46. }
  47. type User struct {
  48. gorm.Model
  49. Username string `json:"username"`
  50. Password string `json:"password"`
  51. Privileges uint `json:"admin"`
  52. }
  53. var Db *gorm.DB
  54. func InitializeDatabase() {
  55. var err error
  56. dcs := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s",
  57. viper.GetString("database.host"),
  58. viper.GetString("database.user"),
  59. viper.GetString("database.password"),
  60. viper.GetString("database.name"),
  61. viper.GetInt("database.port"),
  62. viper.GetString("database.ssl"))
  63. Db, err = gorm.Open(postgres.Open(dcs), &gorm.Config{})
  64. err = Db.AutoMigrate(User{}, RewardFund{}, Contribution{}, Bonus{})
  65. if err != nil {
  66. panic("Could not migrate database")
  67. }
  68. }