diff --git a/frontend/src/network/network.ts b/frontend/src/network/network.ts index 0bd8695..6696052 100644 --- a/frontend/src/network/network.ts +++ b/frontend/src/network/network.ts @@ -730,10 +730,10 @@ class Network { ); } - async listUserCollections(page: number = 1): Promise> { + async listUserCollections(username: string, page: number = 1): Promise> { 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> { return this._callApi(() => axios.get(`${this.apiBaseUrl}/collection/search`, { - params: { keyword }, + params: { username, keyword }, }), ); } diff --git a/server/api/collection.go b/server/api/collection.go index e749f9b..d0ace90 100644 --- a/server/api/collection.go +++ b/server/api/collection.go @@ -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 } diff --git a/server/service/collection.go b/server/service/collection.go index 391ce49..e8b7edf 100644 --- a/server/service/collection.go +++ b/server/service/collection.go @@ -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