From 40fb3a93b6949890a43c0e8f4cc59a824bfac4fb Mon Sep 17 00:00:00 2001 From: nyne Date: Thu, 31 Jul 2025 11:01:49 +0800 Subject: [PATCH] refactor: improve search user collections logic to handle optional keyword --- server/api/collection.go | 6 +++--- server/dao/collection.go | 6 +++++- server/service/collection.go | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/server/api/collection.go b/server/api/collection.go index 0746d63..52701d1 100644 --- a/server/api/collection.go +++ b/server/api/collection.go @@ -195,9 +195,9 @@ func handleRemoveResourceFromCollection(c fiber.Ctx) error { func handleSearchUserCollections(c fiber.Ctx) error { keyword := c.Query("keyword", "") - if keyword == "" { - return model.NewRequestError("keyword is required") - } + // if keyword == "" { + // return model.NewRequestError("keyword is required") + // } username := c.Query("username", "") if username == "" { return model.NewRequestError("username is required") diff --git a/server/dao/collection.go b/server/dao/collection.go index a4cc48e..087c50c 100644 --- a/server/dao/collection.go +++ b/server/dao/collection.go @@ -181,7 +181,11 @@ func SearchUserCollections(uid uint, keyword string, excludedRID uint) ([]*model var collections []*model.Collection query := db.Model(&model.Collection{}). - Where("user_id = ? AND title LIKE ?", uid, "%"+keyword+"%") + Where("user_id = ?", uid) + + if keyword != "" { + query = query.Where("title LIKE ?", "%"+keyword+"%") + } if excludedRID > 0 { // Use LEFT JOIN with IS NULL for better performance diff --git a/server/service/collection.go b/server/service/collection.go index 9cf4f0f..2674948 100644 --- a/server/service/collection.go +++ b/server/service/collection.go @@ -136,7 +136,7 @@ func ListCollectionResources(collectionID uint, page int) ([]*model.ResourceView // Search user collections by keyword, limited to 10 results. // excludedRID: if >0, only return collections not containing this resource. func SearchUserCollections(username string, keyword string, excludedRID uint) ([]*model.CollectionView, error) { - if username == "" || keyword == "" { + if username == "" { return nil, errors.New("invalid parameters") } user, err := dao.GetUserByUsername(username)