The backend for the project formerly known as signet, now known as beignet.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

94 rindas
2.7 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. }
  64. var Db *gorm.DB
  65. func InitializeDatabase() {
  66. var err error
  67. dcs := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s",
  68. viper.GetString("database.host"),
  69. viper.GetString("database.user"),
  70. viper.GetString("database.password"),
  71. viper.GetString("database.name"),
  72. viper.GetInt("database.port"),
  73. viper.GetString("database.ssl"))
  74. Db, err = gorm.Open(postgres.Open(dcs), &gorm.Config{})
  75. if err != nil {
  76. log.Error().Err(err).Msg("Could not open database")
  77. }
  78. err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, QueueOrder{}, Contribution{}, Bonus{})
  79. if err != nil {
  80. log.Error().Err(err).Msg("Could not migrate database")
  81. }
  82. }