mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 20:27:23 +00:00
fix collection api.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
)
|
||||
@@ -9,7 +8,7 @@ import (
|
||||
// Create a new collection.
|
||||
func CreateCollection(uid uint, title, article string, host string) (*model.CollectionView, error) {
|
||||
if uid == 0 || title == "" || article == "" {
|
||||
return nil, errors.New("invalid parameters")
|
||||
return nil, model.NewRequestError("invalid parameters")
|
||||
}
|
||||
c, err := dao.CreateCollection(uid, title, article, findImagesInContent(article, host))
|
||||
if err != nil {
|
||||
@@ -22,14 +21,14 @@ func CreateCollection(uid uint, title, article string, host string) (*model.Coll
|
||||
// Update an existing collection with user validation.
|
||||
func UpdateCollection(uid, id uint, title, article string, host string) error {
|
||||
if uid == 0 || id == 0 || title == "" || article == "" {
|
||||
return errors.New("invalid parameters")
|
||||
return model.NewRequestError("invalid parameters")
|
||||
}
|
||||
collection, err := dao.GetCollectionByID(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if collection.UserID != uid {
|
||||
return errors.New("user does not have permission to update this collection")
|
||||
return model.NewUnAuthorizedError("user does not have permission to update this collection")
|
||||
}
|
||||
return dao.UpdateCollection(id, title, article, findImagesInContent(article, host))
|
||||
}
|
||||
@@ -47,7 +46,7 @@ func DeleteCollection(uint, id uint) error {
|
||||
}
|
||||
|
||||
if user.ID != collection.UserID && !user.IsAdmin {
|
||||
return errors.New("user does not have permission to delete this collection")
|
||||
return model.NewUnAuthorizedError("user does not have permission to delete this collection")
|
||||
}
|
||||
|
||||
return dao.DeleteCollection(id)
|
||||
@@ -56,14 +55,14 @@ func DeleteCollection(uint, id uint) error {
|
||||
// Add a resource to a collection with user validation.
|
||||
func AddResourceToCollection(uid, collectionID, resourceID uint) error {
|
||||
if uid == 0 || collectionID == 0 || resourceID == 0 {
|
||||
return errors.New("invalid parameters")
|
||||
return model.NewRequestError("invalid parameters")
|
||||
}
|
||||
collection, err := dao.GetCollectionByID(collectionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if collection.UserID != uid {
|
||||
return errors.New("user does not have permission to modify this collection")
|
||||
return model.NewUnAuthorizedError("user does not have permission to modify this collection")
|
||||
}
|
||||
return dao.AddResourceToCollection(collectionID, resourceID)
|
||||
}
|
||||
@@ -71,14 +70,14 @@ func AddResourceToCollection(uid, collectionID, resourceID uint) error {
|
||||
// Remove a resource from a collection with user validation.
|
||||
func RemoveResourceFromCollection(uid, collectionID, resourceID uint) error {
|
||||
if uid == 0 || collectionID == 0 || resourceID == 0 {
|
||||
return errors.New("invalid parameters")
|
||||
return model.NewRequestError("invalid parameters")
|
||||
}
|
||||
collection, err := dao.GetCollectionByID(collectionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if collection.UserID != uid {
|
||||
return errors.New("user does not have permission to modify this collection")
|
||||
return model.NewUnAuthorizedError("user does not have permission to modify this collection")
|
||||
}
|
||||
return dao.RemoveResourceFromCollection(collectionID, resourceID)
|
||||
}
|
||||
@@ -86,7 +85,7 @@ func RemoveResourceFromCollection(uid, collectionID, resourceID uint) error {
|
||||
// Get a collection by ID.
|
||||
func GetCollectionByID(id uint) (*model.CollectionView, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("invalid collection id")
|
||||
return nil, model.NewRequestError("invalid collection id")
|
||||
}
|
||||
c, err := dao.GetCollectionByID(id)
|
||||
if err != nil {
|
||||
@@ -98,7 +97,7 @@ func GetCollectionByID(id uint) (*model.CollectionView, error) {
|
||||
// List collections of a user with pagination.
|
||||
func ListUserCollections(username string, page int) ([]*model.CollectionView, int64, error) {
|
||||
if username == "" || page < 1 {
|
||||
return nil, 0, errors.New("invalid parameters")
|
||||
return nil, 0, model.NewRequestError("invalid parameters")
|
||||
}
|
||||
user, err := dao.GetUserByUsername(username)
|
||||
if err != nil {
|
||||
@@ -119,7 +118,7 @@ func ListUserCollections(username string, page int) ([]*model.CollectionView, in
|
||||
// List resources in a collection with pagination.
|
||||
func ListCollectionResources(collectionID uint, page int) ([]*model.ResourceView, int64, error) {
|
||||
if collectionID == 0 || page < 1 {
|
||||
return nil, 0, errors.New("invalid parameters")
|
||||
return nil, 0, model.NewRequestError("invalid parameters")
|
||||
}
|
||||
resources, total, err := dao.ListCollectionResources(collectionID, page, pageSize)
|
||||
if err != nil {
|
||||
@@ -137,7 +136,7 @@ func ListCollectionResources(collectionID uint, page int) ([]*model.ResourceView
|
||||
// excludedRID: if >0, only return collections not containing this resource.
|
||||
func SearchUserCollections(username string, keyword string, excludedRID uint) ([]*model.CollectionView, error) {
|
||||
if username == "" {
|
||||
return nil, errors.New("invalid parameters")
|
||||
return nil, model.NewRequestError("invalid parameters")
|
||||
}
|
||||
user, err := dao.GetUserByUsername(username)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user