The backend for the project formerly known as signet, now known as beignet.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

95 řádky
2.8 KiB

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