package view import ( "errors" "fmt" "github.com/metarare/metarare_api/common" "github.com/metarare/metarare_api/models" "gorm.io/gorm" ) type ResultSet struct { *models.CollectionProfile IsOfficial bool `json:"is_official"` } func SelectCollectionListByKeyword(db *gorm.DB, _filter common.Filter) (error, []ResultSet) { var infos []ResultSet err := fmt.Errorf("") if _filter.CollectionName == "" { err = db.Table("collection_profile"). Select(` collection.is_official as is_official, collection_profile.id, collection_profile.collection_id, collection_profile.created_at, collection_profile.updated_at, collection_profile.deleted_at, collection_profile.name, collection_profile.description, collection_profile.thumbnail_image, collection_profile.cover_image `). 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"). Joins("left join collection on collection_profile.collection_id = collection.id"). Order("tvl desc").Limit(10).Find(&infos).Error } else { keyword := "%" + _filter.CollectionName + "%" err = db.Table("collection_profile").Where("name like ?", keyword).Find(&infos).Error } if errors.Is(err, gorm.ErrRecordNotFound) { fmt.Println(err.Error()) return err, []ResultSet{} } return nil, infos } func SelectExploreItems(db *gorm.DB, _filter common.Filter, authUserID uint64) (error, []common.ExpItem) { var _items []common.ExpItem err := GetExploreItemQuery(db, _filter, authUserID).Find(&_items).Error if errors.Is(err, gorm.ErrRecordNotFound) { fmt.Println(err.Error()) return err, []common.ExpItem{} } return nil, _items } func SelectExploreItemsByTokenName(db *gorm.DB, _filter common.Filter, authUserID uint64) (error, common.SearchExpItems) { _searchItem := common.SearchExpItems{} keyword := "%" + _filter.TokenName + "%" err := GetExploreItemQuery(db, _filter, authUserID).Where("LOWER(token.name) like LOWER(?)", keyword).Find(&_searchItem.Result).Error GetSuggestionSearch(db, _filter).Limit(10).Find(&_searchItem.Suggestion) if errors.Is(err, gorm.ErrRecordNotFound) { fmt.Println(err.Error()) return err, common.SearchExpItems{} } return nil, _searchItem } func HintQueryByToken(db *gorm.DB, _filter common.Filter) (error, []string) { var _items []string keyword := "%" + _filter.TokenName + "%" err := GetHintExploreQuery(db, _filter, keyword).Limit(10).Find(&_items).Error if errors.Is(err, gorm.ErrRecordNotFound) { fmt.Println(err.Error()) return err, []string{} } return nil, _items }