Skip to content

Commit

Permalink
feat: add more apis for batch query (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Nov 16, 2023
1 parent 39cdae4 commit 6431b56
Show file tree
Hide file tree
Showing 26 changed files with 2,624 additions and 12 deletions.
32 changes: 32 additions & 0 deletions dao/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type ItemDao interface {
GetByGroupId(context context.Context, groupId int64, includeAll bool) (database.Item, error)
GetByBucketId(context context.Context, bucketId int64, includeAll bool) (database.Item, error)
GetByObjectId(context context.Context, objectId int64, includeAll bool) (database.Item, error)
GetByBucketIds(context context.Context, bucketIds []int64, includeAll bool) ([]database.Item, error)
GetByObjectIds(context context.Context, objectIds []int64, includeAll bool) ([]database.Item, error)
Batch(context context.Context, ids []int64, includeAll bool) ([]database.Item, error)
Search(context context.Context, categoryId int64, address, keyword string, includeAll bool, sort string, offset, limit int) (int64, []*database.Item, error)
}
Expand Down Expand Up @@ -130,6 +132,36 @@ func (dao *dbItemDao) GetByObjectId(context context.Context, objectId int64, inc
return item, nil
}

func (dao *dbItemDao) GetByBucketIds(context context.Context, bucketIds []int64, includeAll bool) ([]database.Item, error) {
items := make([]database.Item, 0)
if includeAll {
if err := dao.db.Preload("Stats").Where("resource_id in ? and type = ?", bucketIds, database.COLLECTION).Find(&items).Error; err != nil {
return items, err
}
} else {
if err := dao.db.Preload("Stats").Where("resource_id in ? and type = ? and status <> ? and status <> ? and status <> ?",
bucketIds, database.COLLECTION, database.ItemBlocked, database.ItemDelisted, database.ItemPending).Find(&items).Error; err != nil {
return items, err
}
}
return items, nil
}

func (dao *dbItemDao) GetByObjectIds(context context.Context, objectIds []int64, includeAll bool) ([]database.Item, error) {
items := make([]database.Item, 0)
if includeAll {
if err := dao.db.Preload("Stats").Where("resource_id in ? and type = ?", objectIds, database.OBJECT).Find(&items).Error; err != nil {
return items, err
}
} else {
if err := dao.db.Preload("Stats").Where("resource_id in ? and type = ? and status <> ? and status <> ? and status <> ?",
objectIds, database.OBJECT, database.ItemBlocked, database.ItemDelisted, database.ItemPending).Find(&items).Error; err != nil {
return items, err
}
}
return items, nil
}

func (dao *dbItemDao) Batch(context context.Context, ids []int64, includeAll bool) ([]database.Item, error) {
items := make([]database.Item, 0)
if includeAll {
Expand Down
76 changes: 76 additions & 0 deletions dao/purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type PurchaseDao interface {
Update(context context.Context, purchase *database.Purchase) error
Get(context context.Context, id int64) (database.Purchase, error)
Search(context context.Context, itemId int64, address string, sort string, offset, limit int) (int64, []*database.Purchase, error)
Query(context context.Context, itemIds []int64, bucketIds []int64, objectIds []int64, address string, sort string, offset, limit int) (int64, []*database.Purchase, error)
}

type dbPurchaseDao struct {
Expand Down Expand Up @@ -122,3 +123,78 @@ func (dao *dbPurchaseDao) Search(context context.Context, itemId int64, address

return
}

func (dao *dbPurchaseDao) Query(context context.Context, itemIds []int64, bucketIds []int64, objectIds []int64, address string, sort string, offset, limit int) (total int64, purchases []*database.Purchase, err error) {
rawSql := fmt.Sprintf(" inner join items i on p.item_id = i.id where i.status = %d", database.ItemListed)
parameters := make([]interface{}, 0)

if len(itemIds) > 0 {
rawSql = rawSql + ` and item_id in ?`
parameters = append(parameters, itemIds)
} else if len(bucketIds) > 0 {
rawSql = rawSql + ` and resource_id in ? and type = ?`
parameters = append(parameters, bucketIds)
parameters = append(parameters, database.COLLECTION)
} else if len(objectIds) > 0 {
rawSql = rawSql + ` and resource_id in ? and type = ?`
parameters = append(parameters, objectIds)
parameters = append(parameters, database.OBJECT)
}

if len(address) > 0 {
rawSql = rawSql + ` and buyer_address = ?`
parameters = append(parameters, address)
}

countSql := "select count(1) from purchases p " + rawSql

err = dao.db.Raw(countSql, parameters...).Scan(&total).Error
if err != nil {
return 0, nil, err
}
if total == 0 {
return
}

dataSql := "select * from purchases p " + rawSql
dataSql = dataSql + " order by "
switch sort {
case PurchaseSortCreationAsc:
dataSql = dataSql + "p.id asc "
case PurchaseSortCreationDesc:
dataSql = dataSql + "p.id desc "
case PurchaseSortPriceAsc:
dataSql = dataSql + "p.price asc "
case PurchaseSortPriceDesc:
dataSql = dataSql + "p.price desc "
default:
return 0, nil, fmt.Errorf("unsupported sort string: %s", sort)
}
dataSql = dataSql + fmt.Sprintf("limit %d, %d", offset, limit)

err = dao.db.Preload("Item").Raw(dataSql, parameters...).Scan(&purchases).Error
if err != nil {
return 0, nil, err
}

itemIds = []int64{}
for _, purchase := range purchases {
itemIds = append(itemIds, purchase.ItemId)
}

var items []*database.Item
err = dao.db.Preload("Stats").Where("id in ?", itemIds).Find(&items).Error
if err != nil {
return 0, nil, err
}

for _, purchase := range purchases {
for _, item := range items {
if purchase.ItemId == item.Id {
purchase.Item = item
}
}
}

return
}
82 changes: 82 additions & 0 deletions models/item_by_buckets_request.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions models/item_by_objects_request.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6431b56

Please sign in to comment.