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.

91 lines
2.5 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. SellingWallet string `json:"sellingWallet"`
  32. IssuerWallet string `json:"issuerWallet"`
  33. Memo string `json:"memo"`
  34. Price float64 `gorm:"type:decimal(19,7)" json:"price"`
  35. AmountAvailable float64 `gorm:"type:decimal(19,7)" json:"amountAvailable"`
  36. MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"`
  37. TelegramLink string `json:"telegramLink"`
  38. Contributions []Contribution `json:"contributions"`
  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. log.Error().Err(err).Msg("Could not open database")
  74. }
  75. err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, QueueOrder{}, Contribution{}, Bonus{})
  76. if err != nil {
  77. log.Error().Err(err).Msg("Could not migrate database")
  78. }
  79. }