mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
refactor: update user collections handling to use username.
This commit is contained in:
@@ -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(() =>
|
return this._callApi(() =>
|
||||||
axios.get(`${this.apiBaseUrl}/collection/list`, {
|
axios.get(`${this.apiBaseUrl}/collection/list`, {
|
||||||
params: { page },
|
params: { username, page },
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -774,11 +774,12 @@ class Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async searchUserCollections(
|
async searchUserCollections(
|
||||||
|
username: string,
|
||||||
keyword: string,
|
keyword: string,
|
||||||
): 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: { keyword },
|
params: { username, keyword },
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -95,16 +95,16 @@ func handleGetCollection(c fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleListUserCollections(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")
|
pageStr := c.Query("page", "1")
|
||||||
page, err := strconv.Atoi(pageStr)
|
page, err := strconv.Atoi(pageStr)
|
||||||
if err != nil || page < 1 {
|
if err != nil || page < 1 {
|
||||||
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -194,15 +194,15 @@ func handleRemoveResourceFromCollection(c fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleSearchUserCollections(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", "")
|
keyword := c.Query("keyword", "")
|
||||||
if keyword == "" {
|
if keyword == "" {
|
||||||
return model.NewRequestError("keyword is required")
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -96,10 +96,15 @@ func GetCollectionByID(id uint) (*model.CollectionView, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List collections of a user with pagination.
|
// List collections of a user with pagination.
|
||||||
func ListUserCollections(uid uint, page int) ([]*model.CollectionView, int64, error) {
|
func ListUserCollections(username string, page int) ([]*model.CollectionView, int64, error) {
|
||||||
if uid == 0 || page < 1 {
|
if username == "" || page < 1 {
|
||||||
return nil, 0, errors.New("invalid parameters")
|
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)
|
collections, total, err := dao.ListUserCollections(uid, page, pageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
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.
|
// Search user collections by keyword, limited to 10 results.
|
||||||
func SearchUserCollections(uid uint, keyword string) ([]*model.CollectionView, error) {
|
func SearchUserCollections(username string, keyword string) ([]*model.CollectionView, error) {
|
||||||
if uid == 0 || keyword == "" {
|
if username == "" || keyword == "" {
|
||||||
return nil, errors.New("invalid parameters")
|
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)
|
collections, err := dao.SearchUserCollections(uid, keyword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Reference in New Issue
Block a user