refactor: update user collections handling to use username.

This commit is contained in:
2025-07-30 16:47:28 +08:00
parent 17b40f2214
commit a56eb559ba
3 changed files with 28 additions and 17 deletions

View File

@@ -730,10 +730,10 @@ class Network {
);
}
async listUserCollections(page: number = 1): Promise<PageResponse<Collection>> {
async listUserCollections(username: string, page: number = 1): Promise<PageResponse<Collection>> {
return this._callApi(() =>
axios.get(`${this.apiBaseUrl}/collection/list`, {
params: { page },
params: { username, page },
}),
);
}
@@ -774,11 +774,12 @@ class Network {
}
async searchUserCollections(
username: string,
keyword: string,
): Promise<Response<Collection[]>> {
return this._callApi(() =>
axios.get(`${this.apiBaseUrl}/collection/search`, {
params: { keyword },
params: { username, keyword },
}),
);
}

View File

@@ -95,16 +95,16 @@ func handleGetCollection(c fiber.Ctx) error {
}
func handleListUserCollections(c fiber.Ctx) error {
uid, ok := c.Locals("uid").(uint)
if !ok {
return model.NewUnAuthorizedError("Unauthorized")
}
pageStr := c.Query("page", "1")
page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
page = 1
}
cols, total, err := service.ListUserCollections(uid, page)
username := c.Query("username", "")
if username == "" {
return model.NewRequestError("Username is required")
}
cols, total, err := service.ListUserCollections(username, page)
if err != nil {
return err
}
@@ -194,15 +194,15 @@ func handleRemoveResourceFromCollection(c fiber.Ctx) error {
}
func handleSearchUserCollections(c fiber.Ctx) error {
uid, ok := c.Locals("uid").(uint)
if !ok {
return model.NewUnAuthorizedError("Unauthorized")
}
keyword := c.Query("keyword", "")
if keyword == "" {
return model.NewRequestError("keyword is required")
}
cols, err := service.SearchUserCollections(uid, keyword)
username := c.Query("username", "")
if username == "" {
return model.NewRequestError("username is required")
}
cols, err := service.SearchUserCollections(username, keyword)
if err != nil {
return err
}

View File

@@ -96,10 +96,15 @@ func GetCollectionByID(id uint) (*model.CollectionView, error) {
}
// List collections of a user with pagination.
func ListUserCollections(uid uint, page int) ([]*model.CollectionView, int64, error) {
if uid == 0 || page < 1 {
func ListUserCollections(username string, page int) ([]*model.CollectionView, int64, error) {
if username == "" || page < 1 {
return nil, 0, errors.New("invalid parameters")
}
user, err := dao.GetUserByUsername(username)
if err != nil {
return nil, 0, err
}
uid := user.ID
collections, total, err := dao.ListUserCollections(uid, page, pageSize)
if err != nil {
return nil, 0, err
@@ -129,10 +134,15 @@ func ListCollectionResources(collectionID uint, page int) ([]*model.ResourceView
}
// Search user collections by keyword, limited to 10 results.
func SearchUserCollections(uid uint, keyword string) ([]*model.CollectionView, error) {
if uid == 0 || keyword == "" {
func SearchUserCollections(username string, keyword string) ([]*model.CollectionView, error) {
if username == "" || keyword == "" {
return nil, errors.New("invalid parameters")
}
user, err := dao.GetUserByUsername(username)
if err != nil {
return nil, err
}
uid := user.ID
collections, err := dao.SearchUserCollections(uid, keyword)
if err != nil {
return nil, err