The backend for the project formerly known as signet, now known as beignet.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

91 wiersze
2.6 KiB

  1. package data
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/spf13/viper"
  6. "gorm.io/driver/postgres"
  7. "gorm.io/gorm"
  8. )
  9. type ModelBase struct {
  10. ID uint `gorm:"primarykey" json:"id"`
  11. CreatedAt time.Time `json:"createdAt"`
  12. UpdatedAt time.Time `json:"updatedAt"`
  13. DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
  14. }
  15. type Bonus struct {
  16. ModelBase
  17. Goal float64 `json:"goal"`
  18. Percent float64 `json:"percent"`
  19. RewardFundID uint `json:"rewardFundID"`
  20. }
  21. type Queue struct {
  22. ModelBase
  23. Name string `gorm:"type:varchar(50)" json:"name"`
  24. Orders []QueueOrder `json:"order"`
  25. }
  26. type RewardFund struct {
  27. ModelBase
  28. Asset string `json:"asset"`
  29. FundWallet string `json:"fundWallet"`
  30. SellingWallet string `json:"sellingWallet"`
  31. IssuerWallet string `json:"issuerWallet"`
  32. Memo string `json:"memo"`
  33. Price float64 `gorm:"type:decimal(19,7)" json:"price"`
  34. AmountAvailable float64 `gorm:"type:decimal(19,7)" json:"amountAvailable"`
  35. MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"`
  36. Contributions []Contribution `json:"contributions"`
  37. Title string `gorm:"type:varchar(50)" json:"title"`
  38. Description string `gorm:"type:text" json:"description"`
  39. Bonuses []Bonus `json:"bonuses"`
  40. QueueOrder QueueOrder `json:"queueOrder"`
  41. }
  42. type QueueOrder struct {
  43. ModelBase
  44. QueueID uint `json:"queueID"`
  45. RewardFundID uint `json:"rewardFundID"`
  46. Order uint16 `gorm:"type:smallint" json:"order"`
  47. }
  48. type Contribution struct {
  49. ModelBase
  50. Wallet string `json:"wallet"`
  51. Amount float64 `gorm:"type:decimal(19,7)" json:"amount"`
  52. TransactionID string `json:"transactionID"`
  53. RewardFundID uint `json:"rewardFundID"`
  54. }
  55. type User struct {
  56. ModelBase
  57. Username string `json:"username"`
  58. Password string `json:"password"`
  59. Privileges uint `json:"admin"`
  60. }
  61. var Db *gorm.DB
  62. func InitializeDatabase() {
  63. var err error
  64. dcs := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s",
  65. viper.GetString("database.host"),
  66. viper.GetString("database.user"),
  67. viper.GetString("database.password"),
  68. viper.GetString("database.name"),
  69. viper.GetInt("database.port"),
  70. viper.GetString("database.ssl"))
  71. Db, err = gorm.Open(postgres.Open(dcs), &gorm.Config{})
  72. if err != nil {
  73. panic("Could not open database")
  74. }
  75. err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, QueueOrder{}, Contribution{}, Bonus{})
  76. if err != nil {
  77. panic("Could not migrate database")
  78. }
  79. }