123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- package view
- import (
- "errors"
- "fmt"
- "github.com/metarare/metarare_api/api/constant"
- "github.com/metarare/metarare_api/common"
- "gorm.io/gorm"
- )
- // Logic
- func SelectRelatedCollection(db *gorm.DB, id uint64, filter common.Filter) (error, []CollectionInfo) {
- err, address := getAddressByUserID(db, id)
- if err != nil {
- return err, []CollectionInfo{}
- }
- var collectionIDs []uint64
- db.Table("collection").Where("owner_address = (?)", address).Find(&collectionIDs)
- collections, err := GetCollectionInfomation(db, collectionIDs, id)
- return nil, collections
- }
- func SelectOnsaleItems(db *gorm.DB, id uint64, _filter common.Filter) (error, []common.ExpItem) {
- var _items []common.ExpItem
- _filter.SaleStatus = "ongoing"
- err := GetExploreItemQuery(db, _filter, id).Where("user_info.id = (?) AND sale.id NOT IN (?)", id, constant.HideFromListIds).Find(&_items).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- fmt.Println(err.Error())
- return err, []common.ExpItem{}
- }
- return nil, _items
- }
- func SelectOwnedTokens(db *gorm.DB, id uint64, filter common.Filter) (error, []common.ExpItem) {
- var _items []common.ExpItem
- err, _id := getAddressByUserID(db, id)
- if err != nil {
- return err, []common.ExpItem{}
- }
- err = GetExploreItemQuery(db, filter, id).Where("token.owner_address = (?) AND sale.status != (?) AND sale.id NOT IN (?)", _id, "ongoing", constant.HideFromListIds).Find(&_items).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- fmt.Println(err.Error())
- return err, []common.ExpItem{}
- }
- return nil, _items
- }
- type UserProfile struct {
- ID uint64 `json:"id"`
- CoverImage string `json:"cover_image"`
- ThumbnailImage string `json:"thumbnail_image"`
- IsArtist string `json:"is_artist"`
- Name string `json:"name"`
- Address string `json:"address"`
- Description string `json:"description"`
- Twitter string `json:"twitter"`
- IsOwner bool `json:"is_owner"`
- }
- func SelectUserInfo(db *gorm.DB, id uint64, ownerID uint64) (error, UserProfile) {
- var _up UserProfile
- err := db.Table("user").Select(`
- user.id,
- user_profile.cover_image,
- user_profile.thumbnail_image,
- (select if(artist_profile.id is not null , true, false)) as is_artist,
- user_profile.name,
- user_wallet.address,
- user_profile.description,
- user_profile.twitter,
- IF(user.id = ?, true, false) AS is_owner
- `, ownerID).
- Joins("left join user_profile on user.id = user_profile.user_id ").
- Joins("left join user_wallet on user.id = user_wallet.user_id").
- Joins("left join artist_profile on user.id = artist_profile.user_id").
- Where("user.id = (?)", id).Find(&_up).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- fmt.Println(err.Error())
- return err, UserProfile{}
- }
- return nil, _up
- }
- func SelectLikedTokens(db *gorm.DB, userID uint64, _filter common.Filter) (error, []common.ExpItem) {
- var _items []common.ExpItem
- var tokenIDs []uint64
- err := db.Table("user_like").Select("token_id").Where("user_id = ? and is_like = (?)", userID, true).Group("token_id").Find(&tokenIDs).Error
- 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
- if errors.Is(err, gorm.ErrRecordNotFound) {
- fmt.Println(err.Error())
- return err, []common.ExpItem{}
- }
- return nil, _items
- }
- func SelectUserBidHistory(db *gorm.DB, userID uint64, _filter common.Filter) (error, []common.ExpItem) {
- var _items []common.ExpItem
- var _participatedSaleList []uint64
- err := db.Table("bid_log").
- Select("sale_id").
- Joins("left join user_wallet on bid_log.address = user_wallet.address").
- Where("user_wallet.user_id = (?)", userID).Find(&_participatedSaleList).Error
- err = GetExploreItemQuery(db, _filter, userID).
- Where("sale.id in (?)", _participatedSaleList).Find(&_items).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- fmt.Println(err.Error())
- return err, []common.ExpItem{}
- }
- return nil, _items
- }
- func SelectUserActivities(db *gorm.DB, userID uint64) (error, []common.ActivityItem) {
- var _logs []common.ActivityItem
- err := GetActivityItemQuery(db).Where("log_relation.user_id = (?)", userID).Find(&_logs).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- fmt.Println(err.Error())
- return err, []common.ActivityItem{}
- }
- return nil, _logs
- }
- type SimpleProfile struct {
- SimpleProfile UserBaseData `json:"simple_profile"`
- Setting Settings `json:"settings"`
- }
- type UserBaseData struct {
- UserID uint64 `json:"user_id"`
- Name string `json:"name"`
- Address string `json:"address"`
- Email string `json:"email"`
- IsArtist bool `json:"is_artist"`
- EthBalance float64 `json:"eth_balance"`
- AvailableEthBalance float64 `json:"available_eth_balance"`
- AvailableMfBalance float64 `json:"available_mf_balance"`
- AvailableMrBalance float64 `json:"available_mr_balance"`
- MfBalance float64 `json:"mf_balance"`
- MrBalance float64 `json:"mr_balance"`
- ThumbnailImage string `json:"thumbnail_image"`
- CoverImage string `json:"cover_image"`
- Description string `json:"description"`
- Twitter string `json:"twitter"`
- }
- type Settings struct {
- ServiceCommission float64 `json:"service_commission"`
- NetworkCommission float64 `json:"network_commission"`
- }
- func GetSimpleProfile(db *gorm.DB, userID uint64) *gorm.DB {
- query := db.Select(`user.id AS user_id, user_profile.name, user_profile.description, user_profile.thumbnail_image, user_profile.cover_image,
- user_profile.twitter, user_wallet.address, user_authentication.email,
- IF(artist_profile.id IS NOT NULL, true, false) AS is_artist`).Table("user_profile").
- Joins("INNER JOIN user ON user.id = user_profile.user_id").
- Joins("INNER JOIN user_wallet ON user_wallet.user_id = user.id").
- Joins("INNER JOIN user_authentication ON user_authentication.user_id = user.id").
- Joins("LEFT JOIN artist_profile ON artist_profile.user_id = user.id").
- Where("user.id = ?", userID)
- return query
- }
|