Add excludedRID param to collection search api.

This commit is contained in:
2025-07-31 10:49:18 +08:00
parent 12237ee0a4
commit 470324221d
4 changed files with 30 additions and 9 deletions

View File

@@ -202,7 +202,14 @@ func handleSearchUserCollections(c fiber.Ctx) error {
if username == "" {
return model.NewRequestError("username is required")
}
cols, err := service.SearchUserCollections(username, keyword)
excludedRIDStr := c.Query("excludedRID", "")
var excludedRID uint = 0
if excludedRIDStr != "" {
if rid, err := strconv.Atoi(excludedRIDStr); err == nil && rid > 0 {
excludedRID = uint(rid)
}
}
cols, err := service.SearchUserCollections(username, keyword, excludedRID)
if err != nil {
return err
}

View File

@@ -176,14 +176,23 @@ func ListCollectionResources(collectionID uint, page int, pageSize int) ([]*mode
}
// SearchUserCollections searches for collections by user ID and keyword limited to 10 results.
func SearchUserCollections(uid uint, keyword string) ([]*model.Collection, error) {
// excludedRID: if >0, only return collections not containing this resource.
func SearchUserCollections(uid uint, keyword string, excludedRID uint) ([]*model.Collection, error) {
var collections []*model.Collection
if err := db.
Model(&model.Collection{}).
query := db.Model(&model.Collection{}).
Where("user_id = ? AND title LIKE ?", uid, "%"+keyword+"%")
if excludedRID > 0 {
// Use LEFT JOIN with IS NULL for better performance
query = query.
Joins("LEFT JOIN collection_resources cr ON collections.id = cr.collection_id AND cr.resource_id = ?", excludedRID).
Where("cr.collection_id IS NULL")
}
if err := query.
Preload("Images").
Preload("Resources").
Where("user_id = ? AND title LIKE ?", uid, "%"+keyword+"%").
Limit(10).
Find(&collections).Error; err != nil {
return nil, err

View File

@@ -134,7 +134,8 @@ func ListCollectionResources(collectionID uint, page int) ([]*model.ResourceView
}
// Search user collections by keyword, limited to 10 results.
func SearchUserCollections(username string, keyword string) ([]*model.CollectionView, error) {
// excludedRID: if >0, only return collections not containing this resource.
func SearchUserCollections(username string, keyword string, excludedRID uint) ([]*model.CollectionView, error) {
if username == "" || keyword == "" {
return nil, errors.New("invalid parameters")
}
@@ -143,7 +144,7 @@ func SearchUserCollections(username string, keyword string) ([]*model.Collection
return nil, err
}
uid := user.ID
collections, err := dao.SearchUserCollections(uid, keyword)
collections, err := dao.SearchUserCollections(uid, keyword, excludedRID)
if err != nil {
return nil, err
}