user.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package view
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/metarare/metarare_api/api/constant"
  6. "github.com/metarare/metarare_api/common"
  7. "gorm.io/gorm"
  8. )
  9. // Logic
  10. func SelectRelatedCollection(db *gorm.DB, id uint64, filter common.Filter) (error, []CollectionInfo) {
  11. err, address := getAddressByUserID(db, id)
  12. if err != nil {
  13. return err, []CollectionInfo{}
  14. }
  15. var collectionIDs []uint64
  16. db.Table("collection").Where("owner_address = (?)", address).Find(&collectionIDs)
  17. collections, err := GetCollectionInfomation(db, collectionIDs, id)
  18. return nil, collections
  19. }
  20. func SelectOnsaleItems(db *gorm.DB, id uint64, _filter common.Filter) (error, []common.ExpItem) {
  21. var _items []common.ExpItem
  22. _filter.SaleStatus = "ongoing"
  23. err := GetExploreItemQuery(db, _filter, id).Where("user_info.id = (?) AND sale.id NOT IN (?)", id, constant.HideFromListIds).Find(&_items).Error
  24. if errors.Is(err, gorm.ErrRecordNotFound) {
  25. fmt.Println(err.Error())
  26. return err, []common.ExpItem{}
  27. }
  28. return nil, _items
  29. }
  30. func SelectOwnedTokens(db *gorm.DB, id uint64, filter common.Filter) (error, []common.ExpItem) {
  31. var _items []common.ExpItem
  32. err, _id := getAddressByUserID(db, id)
  33. if err != nil {
  34. return err, []common.ExpItem{}
  35. }
  36. err = GetExploreItemQuery(db, filter, id).Where("token.owner_address = (?) AND sale.status != (?) AND sale.id NOT IN (?)", _id, "ongoing", constant.HideFromListIds).Find(&_items).Error
  37. if errors.Is(err, gorm.ErrRecordNotFound) {
  38. fmt.Println(err.Error())
  39. return err, []common.ExpItem{}
  40. }
  41. return nil, _items
  42. }
  43. type UserProfile struct {
  44. ID uint64 `json:"id"`
  45. CoverImage string `json:"cover_image"`
  46. ThumbnailImage string `json:"thumbnail_image"`
  47. IsArtist string `json:"is_artist"`
  48. Name string `json:"name"`
  49. Address string `json:"address"`
  50. Description string `json:"description"`
  51. Twitter string `json:"twitter"`
  52. IsOwner bool `json:"is_owner"`
  53. }
  54. func SelectUserInfo(db *gorm.DB, id uint64, ownerID uint64) (error, UserProfile) {
  55. var _up UserProfile
  56. err := db.Table("user").Select(`
  57. user.id,
  58. user_profile.cover_image,
  59. user_profile.thumbnail_image,
  60. (select if(artist_profile.id is not null , true, false)) as is_artist,
  61. user_profile.name,
  62. user_wallet.address,
  63. user_profile.description,
  64. user_profile.twitter,
  65. IF(user.id = ?, true, false) AS is_owner
  66. `, ownerID).
  67. Joins("left join user_profile on user.id = user_profile.user_id ").
  68. Joins("left join user_wallet on user.id = user_wallet.user_id").
  69. Joins("left join artist_profile on user.id = artist_profile.user_id").
  70. Where("user.id = (?)", id).Find(&_up).Error
  71. if errors.Is(err, gorm.ErrRecordNotFound) {
  72. fmt.Println(err.Error())
  73. return err, UserProfile{}
  74. }
  75. return nil, _up
  76. }
  77. func SelectLikedTokens(db *gorm.DB, userID uint64, _filter common.Filter) (error, []common.ExpItem) {
  78. var _items []common.ExpItem
  79. var tokenIDs []uint64
  80. err := db.Table("user_like").Select("token_id").Where("user_id = ? and is_like = (?)", userID, true).Group("token_id").Find(&tokenIDs).Error
  81. err = GetExploreItemQuery(db, _filter, userID).Where("token.id in (?) and user_like.user_id is not null AND user_like.token_id is not null AND user_like.deleted_at is null", tokenIDs).Order("user_like.updated_at desc").Find(&_items).Error
  82. if errors.Is(err, gorm.ErrRecordNotFound) {
  83. fmt.Println(err.Error())
  84. return err, []common.ExpItem{}
  85. }
  86. return nil, _items
  87. }
  88. func SelectUserBidHistory(db *gorm.DB, userID uint64, _filter common.Filter) (error, []common.ExpItem) {
  89. var _items []common.ExpItem
  90. var _participatedSaleList []uint64
  91. err := db.Table("bid_log").
  92. Select("sale_id").
  93. Joins("left join user_wallet on bid_log.address = user_wallet.address").
  94. Where("user_wallet.user_id = (?)", userID).Find(&_participatedSaleList).Error
  95. err = GetExploreItemQuery(db, _filter, userID).
  96. Where("sale.id in (?)", _participatedSaleList).Find(&_items).Error
  97. if errors.Is(err, gorm.ErrRecordNotFound) {
  98. fmt.Println(err.Error())
  99. return err, []common.ExpItem{}
  100. }
  101. return nil, _items
  102. }
  103. func SelectUserActivities(db *gorm.DB, userID uint64) (error, []common.ActivityItem) {
  104. var _logs []common.ActivityItem
  105. err := GetActivityItemQuery(db).Where("log_relation.user_id = (?)", userID).Find(&_logs).Error
  106. if errors.Is(err, gorm.ErrRecordNotFound) {
  107. fmt.Println(err.Error())
  108. return err, []common.ActivityItem{}
  109. }
  110. return nil, _logs
  111. }
  112. type SimpleProfile struct {
  113. SimpleProfile UserBaseData `json:"simple_profile"`
  114. Setting Settings `json:"settings"`
  115. }
  116. type UserBaseData struct {
  117. UserID uint64 `json:"user_id"`
  118. Name string `json:"name"`
  119. Address string `json:"address"`
  120. Email string `json:"email"`
  121. IsArtist bool `json:"is_artist"`
  122. EthBalance float64 `json:"eth_balance"`
  123. AvailableEthBalance float64 `json:"available_eth_balance"`
  124. AvailableMfBalance float64 `json:"available_mf_balance"`
  125. AvailableMrBalance float64 `json:"available_mr_balance"`
  126. MfBalance float64 `json:"mf_balance"`
  127. MrBalance float64 `json:"mr_balance"`
  128. ThumbnailImage string `json:"thumbnail_image"`
  129. CoverImage string `json:"cover_image"`
  130. Description string `json:"description"`
  131. Twitter string `json:"twitter"`
  132. }
  133. type Settings struct {
  134. ServiceCommission float64 `json:"service_commission"`
  135. NetworkCommission float64 `json:"network_commission"`
  136. }
  137. func GetSimpleProfile(db *gorm.DB, userID uint64) *gorm.DB {
  138. query := db.Select(`user.id AS user_id, user_profile.name, user_profile.description, user_profile.thumbnail_image, user_profile.cover_image,
  139. user_profile.twitter, user_wallet.address, user_authentication.email,
  140. IF(artist_profile.id IS NOT NULL, true, false) AS is_artist`).Table("user_profile").
  141. Joins("INNER JOIN user ON user.id = user_profile.user_id").
  142. Joins("INNER JOIN user_wallet ON user_wallet.user_id = user.id").
  143. Joins("INNER JOIN user_authentication ON user_authentication.user_id = user.id").
  144. Joins("LEFT JOIN artist_profile ON artist_profile.user_id = user.id").
  145. Where("user.id = ?", userID)
  146. return query
  147. }