mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add excludedRID param to collection search api.
This commit is contained in:
@@ -730,7 +730,10 @@ class Network {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async listUserCollections(username: string, page: number = 1): Promise<PageResponse<Collection>> {
|
async listUserCollections(
|
||||||
|
username: string,
|
||||||
|
page: number = 1,
|
||||||
|
): Promise<PageResponse<Collection>> {
|
||||||
return this._callApi(() =>
|
return this._callApi(() =>
|
||||||
axios.get(`${this.apiBaseUrl}/collection/list`, {
|
axios.get(`${this.apiBaseUrl}/collection/list`, {
|
||||||
params: { username, page },
|
params: { username, page },
|
||||||
@@ -776,10 +779,11 @@ class Network {
|
|||||||
async searchUserCollections(
|
async searchUserCollections(
|
||||||
username: string,
|
username: string,
|
||||||
keyword: string,
|
keyword: string,
|
||||||
|
excludedRID?: number,
|
||||||
): Promise<Response<Collection[]>> {
|
): Promise<Response<Collection[]>> {
|
||||||
return this._callApi(() =>
|
return this._callApi(() =>
|
||||||
axios.get(`${this.apiBaseUrl}/collection/search`, {
|
axios.get(`${this.apiBaseUrl}/collection/search`, {
|
||||||
params: { username, keyword },
|
params: { username, keyword, excludedRID },
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -202,7 +202,14 @@ func handleSearchUserCollections(c fiber.Ctx) error {
|
|||||||
if username == "" {
|
if username == "" {
|
||||||
return model.NewRequestError("username is required")
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -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.
|
// 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
|
var collections []*model.Collection
|
||||||
|
|
||||||
if err := db.
|
query := db.Model(&model.Collection{}).
|
||||||
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("Images").
|
||||||
Preload("Resources").
|
Preload("Resources").
|
||||||
Where("user_id = ? AND title LIKE ?", uid, "%"+keyword+"%").
|
|
||||||
Limit(10).
|
Limit(10).
|
||||||
Find(&collections).Error; err != nil {
|
Find(&collections).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -134,7 +134,8 @@ func ListCollectionResources(collectionID uint, page int) ([]*model.ResourceView
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Search user collections by keyword, limited to 10 results.
|
// 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 == "" {
|
if username == "" || keyword == "" {
|
||||||
return nil, errors.New("invalid parameters")
|
return nil, errors.New("invalid parameters")
|
||||||
}
|
}
|
||||||
@@ -143,7 +144,7 @@ func SearchUserCollections(username string, keyword string) ([]*model.Collection
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
uid := user.ID
|
uid := user.ID
|
||||||
collections, err := dao.SearchUserCollections(uid, keyword)
|
collections, err := dao.SearchUserCollections(uid, keyword, excludedRID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user