sale.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package view
  2. import (
  3. "time"
  4. "github.com/metarare/metarare_api/models"
  5. "gorm.io/gorm"
  6. )
  7. type DetailSaleData struct {
  8. Sale Sale `json:"sale"`
  9. Traits []models.Traits `json:"traits"`
  10. }
  11. type Sale struct {
  12. ID uint64 `json:"id"`
  13. TokenID uint64 `json:"token_id"`
  14. TokenUID string `json:"token_uid"`
  15. TokenType string `json:"token_type"`
  16. ContentURL string `json:"content_url"`
  17. Name string `json:"name"`
  18. Description string `json:"description"`
  19. SaleType string `json:"sale_type"`
  20. Status string `json:"status"`
  21. Price float64 `json:"price"`
  22. HighestPrice float64 `json:"highest_price"` // 서브쿼리
  23. MyBiddingPrice float64 `json:"my_bidding_price"`
  24. StartPrice float64 `json:"start_price"`
  25. StartAt time.Time `json:"start_at"`
  26. EndAt time.Time `json:"end_at"`
  27. Royalties uint32 `json:"royalties"`
  28. ArtistProfileImage string `json:"artist_profile_image"`
  29. ArtistName string `json:"artist_name"`
  30. ArtistIsOfficial bool `json:"artist_is_official"`
  31. LikeCount int `json:"like_count"`
  32. CollectionThumbnailImage string `json:"collection_thumbnail_image"`
  33. CollectionName string `json:"collection_name"`
  34. UserProfileImage string `json:"user_profile_image"`
  35. UserName string `json:"user_name"`
  36. OwnerIsOfficial bool `json:"owner_is_official"`
  37. Network string `json:"network"`
  38. Currency string `json:"currency"`
  39. IsOwner bool `json:"is_owner"`
  40. IsLike bool `json:"is_like"`
  41. TotalCount int `json:"total_count"`
  42. Index int `json:"index"`
  43. CollectionIsOfficial bool `json:"collection_is_official"`
  44. }
  45. type Owner struct {
  46. IsOwner bool `json:"is_owner"`
  47. }
  48. type BidList struct {
  49. ThumbnailImage string `json:"thumbnail_image"`
  50. Name string `json:"name"`
  51. Price float64 `json:"price"`
  52. IsCancel bool `json:"is_cancel"`
  53. Currency string `json:"currency"`
  54. CreatedAt time.Time `json:"created_at"`
  55. IsOfficial bool `json:"is_official"`
  56. }
  57. func GetSaleData(db *gorm.DB, address string, userID uint64, saleID string) *gorm.DB {
  58. querySet := db.Select(`sale.id, token.id AS token_id, token.uid AS token_uid, token.type AS token_type, token.content_url, token.name, token.description, sale.sale_type, sale.status, sale.price,
  59. (select price from bid_log where bid_log.is_cancel = false AND sale_id = sale.id order by id desc LIMIT 1) AS highest_price,
  60. IF(bl.id IS NOT NULL, bl.price, 0) AS my_bidding_price, sale.start_price, sale.start_at, sale.end_at, token.total_count, token.index,
  61. token.royalties, artist.thumbnail_image AS artist_profile_image, artist.name AS artist_name, token.like_count, IF(artist_profile_user.id IS NULL, false, true) AS artist_is_official,
  62. collection_profile.thumbnail_image AS collection_thumbnail_image, collection_profile.name AS collection_name, collection.is_official AS collection_is_official,
  63. user_profile.thumbnail_image AS user_profile_image, user_profile.name AS user_name, collection.network, sale.currency, IF(artist_profile.id IS NULL, false, true) AS owner_is_official,
  64. IF(user_like.id IS NOT NULL AND user_like.is_like = 1, true, false) AS is_like, IF(token.owner_address = ?, true, false) AS is_owner`, address).Table("sale").
  65. Joins("INNER JOIN token ON token.id = sale.token_id").
  66. Joins("INNER JOIN collection ON collection.id = token.collection_id").
  67. Joins("INNER JOIN collection_profile ON collection_profile.collection_id = collection.id").
  68. Joins("INNER JOIN user_wallet AS artist_wallet ON artist_wallet.address = token.creator_address").
  69. Joins("INNER JOIN user_profile AS artist ON artist.user_id = artist_wallet.user_id").
  70. Joins("INNER JOIN user_wallet ON user_wallet.address = token.owner_address").
  71. Joins("INNER JOIN user_profile ON user_profile.user_id = user_wallet.user_id").
  72. Joins("LEFT JOIN artist_profile AS artist_profile_user ON artist_profile_user.user_id = artist.user_id").
  73. Joins("LEFT JOIN artist_profile ON artist_profile.user_id = user_wallet.user_id").
  74. Joins("LEFT JOIN bid_log AS bl ON bl.address = ? AND bl.sale_id = ? AND is_cancel = 0", address, saleID).
  75. Joins("LEFT JOIN user_like ON user_like.token_id = token.id AND user_like.user_id = ?", userID)
  76. return querySet
  77. }
  78. func IsOwner(db *gorm.DB, userID uint64) *gorm.DB {
  79. querySet := db.Select("IF(user_wallet.address = token.owner_address, true, false) AS is_owner").Table("sale").
  80. Joins("INNER JOIN token ON token.id = sale.token_id").
  81. Joins("INNER JOIN user ON user.id = ?", userID).
  82. Joins("INNER JOIN user_wallet ON user_wallet.user_id = user.id")
  83. return querySet
  84. }
  85. func GetBidList(db *gorm.DB) *gorm.DB {
  86. querySet := db.Select(`user_profile.thumbnail_image, user_profile.name, bid_log.price, bid_log.is_cancel, sale.currency, bid_log.created_at,
  87. IF(artist_profile.id IS NOT NULL, true, false) AS is_official`).Table("bid_log").
  88. Joins("INNER JOIN sale ON sale.id = bid_log.sale_id").
  89. Joins("INNER JOIN user_wallet ON user_wallet.address = bid_log.address").
  90. Joins("INNER JOIN user_profile ON user_profile.user_id = user_wallet.user_id").
  91. Joins("LEFT JOIN artist_profile ON artist_profile.user_id = user_wallet.user_id")
  92. return querySet
  93. }