explore.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package view
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/metarare/metarare_api/common"
  6. "github.com/metarare/metarare_api/models"
  7. "gorm.io/gorm"
  8. )
  9. type ResultSet struct {
  10. *models.CollectionProfile
  11. IsOfficial bool `json:"is_official"`
  12. }
  13. func SelectCollectionListByKeyword(db *gorm.DB, _filter common.Filter) (error, []ResultSet) {
  14. var infos []ResultSet
  15. err := fmt.Errorf("")
  16. if _filter.CollectionName == "" {
  17. err = db.Table("collection_profile").
  18. Select(`
  19. collection.is_official as is_official,
  20. collection_profile.id,
  21. collection_profile.collection_id,
  22. collection_profile.created_at,
  23. collection_profile.updated_at,
  24. collection_profile.deleted_at,
  25. collection_profile.name,
  26. collection_profile.description,
  27. collection_profile.thumbnail_image,
  28. collection_profile.cover_image
  29. `).
  30. Joins(" left join (select token.collection_id, sum(price) as tvl from sale left join token on sale.token_id = token.id group by token.collection_id) as stats on stats.collection_id = collection_profile.collection_id").
  31. Joins("left join collection on collection_profile.collection_id = collection.id").
  32. Order("tvl desc").Limit(10000).Find(&infos).Error
  33. } else {
  34. keyword := "%" + _filter.CollectionName + "%"
  35. err = db.Table("collection_profile").Where("LOWER(name) like LOWER(?)", keyword).Find(&infos).Error
  36. }
  37. if errors.Is(err, gorm.ErrRecordNotFound) {
  38. fmt.Println(err.Error())
  39. return err, []ResultSet{}
  40. }
  41. return nil, infos
  42. }
  43. func SelectExploreItems(db *gorm.DB, _filter common.Filter, authUserID uint64) (error, []common.ExpItem) {
  44. var _items []common.ExpItem
  45. err := GetExploreItemQuery(db, _filter, authUserID).Find(&_items).Error
  46. if errors.Is(err, gorm.ErrRecordNotFound) {
  47. fmt.Println(err.Error())
  48. return err, []common.ExpItem{}
  49. }
  50. return nil, _items
  51. }
  52. func SelectExploreItemsByTokenName(db *gorm.DB, _filter common.Filter, authUserID uint64) (error, common.SearchExpItems) {
  53. _searchItem := common.SearchExpItems{}
  54. keyword := "%" + _filter.TokenName + "%"
  55. err := GetExploreItemQuery(db, _filter, authUserID).Where("LOWER(token.name) like LOWER(?)", keyword).Find(&_searchItem.Result).Error
  56. GetSuggestionSearch(db, _filter).Limit(10).Find(&_searchItem.Suggestion)
  57. if errors.Is(err, gorm.ErrRecordNotFound) {
  58. fmt.Println(err.Error())
  59. return err, common.SearchExpItems{}
  60. }
  61. return nil, _searchItem
  62. }
  63. func HintQueryByToken(db *gorm.DB, _filter common.Filter) (error, []string) {
  64. var _items []string
  65. keyword := "%" + _filter.TokenName + "%"
  66. err := GetHintExploreQuery(db, _filter, keyword).Limit(10).Find(&_items).Error
  67. if errors.Is(err, gorm.ErrRecordNotFound) {
  68. fmt.Println(err.Error())
  69. return err, []string{}
  70. }
  71. return nil, _items
  72. }