mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-28 04:27:24 +00:00
fix collection api.
This commit is contained in:
@@ -66,6 +66,7 @@ func ErrorHandler(c fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Error("Internal Server Error: ", err)
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(model.Response[any]{
|
return c.Status(fiber.StatusInternalServerError).JSON(model.Response[any]{
|
||||||
Success: false,
|
Success: false,
|
||||||
Data: nil,
|
Data: nil,
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"nysoure/server/dao"
|
"nysoure/server/dao"
|
||||||
"nysoure/server/model"
|
"nysoure/server/model"
|
||||||
)
|
)
|
||||||
@@ -9,7 +8,7 @@ import (
|
|||||||
// Create a new collection.
|
// Create a new collection.
|
||||||
func CreateCollection(uid uint, title, article string, host string) (*model.CollectionView, error) {
|
func CreateCollection(uid uint, title, article string, host string) (*model.CollectionView, error) {
|
||||||
if uid == 0 || title == "" || article == "" {
|
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))
|
c, err := dao.CreateCollection(uid, title, article, findImagesInContent(article, host))
|
||||||
if err != nil {
|
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.
|
// Update an existing collection with user validation.
|
||||||
func UpdateCollection(uid, id uint, title, article string, host string) error {
|
func UpdateCollection(uid, id uint, title, article string, host string) error {
|
||||||
if uid == 0 || id == 0 || title == "" || article == "" {
|
if uid == 0 || id == 0 || title == "" || article == "" {
|
||||||
return errors.New("invalid parameters")
|
return model.NewRequestError("invalid parameters")
|
||||||
}
|
}
|
||||||
collection, err := dao.GetCollectionByID(id)
|
collection, err := dao.GetCollectionByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if collection.UserID != uid {
|
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))
|
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 {
|
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)
|
return dao.DeleteCollection(id)
|
||||||
@@ -56,14 +55,14 @@ func DeleteCollection(uint, id uint) error {
|
|||||||
// Add a resource to a collection with user validation.
|
// Add a resource to a collection with user validation.
|
||||||
func AddResourceToCollection(uid, collectionID, resourceID uint) error {
|
func AddResourceToCollection(uid, collectionID, resourceID uint) error {
|
||||||
if uid == 0 || collectionID == 0 || resourceID == 0 {
|
if uid == 0 || collectionID == 0 || resourceID == 0 {
|
||||||
return errors.New("invalid parameters")
|
return model.NewRequestError("invalid parameters")
|
||||||
}
|
}
|
||||||
collection, err := dao.GetCollectionByID(collectionID)
|
collection, err := dao.GetCollectionByID(collectionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if collection.UserID != uid {
|
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)
|
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.
|
// Remove a resource from a collection with user validation.
|
||||||
func RemoveResourceFromCollection(uid, collectionID, resourceID uint) error {
|
func RemoveResourceFromCollection(uid, collectionID, resourceID uint) error {
|
||||||
if uid == 0 || collectionID == 0 || resourceID == 0 {
|
if uid == 0 || collectionID == 0 || resourceID == 0 {
|
||||||
return errors.New("invalid parameters")
|
return model.NewRequestError("invalid parameters")
|
||||||
}
|
}
|
||||||
collection, err := dao.GetCollectionByID(collectionID)
|
collection, err := dao.GetCollectionByID(collectionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if collection.UserID != uid {
|
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)
|
return dao.RemoveResourceFromCollection(collectionID, resourceID)
|
||||||
}
|
}
|
||||||
@@ -86,7 +85,7 @@ func RemoveResourceFromCollection(uid, collectionID, resourceID uint) error {
|
|||||||
// Get a collection by ID.
|
// Get a collection by ID.
|
||||||
func GetCollectionByID(id uint) (*model.CollectionView, error) {
|
func GetCollectionByID(id uint) (*model.CollectionView, error) {
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
return nil, errors.New("invalid collection id")
|
return nil, model.NewRequestError("invalid collection id")
|
||||||
}
|
}
|
||||||
c, err := dao.GetCollectionByID(id)
|
c, err := dao.GetCollectionByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -98,7 +97,7 @@ func GetCollectionByID(id uint) (*model.CollectionView, error) {
|
|||||||
// List collections of a user with pagination.
|
// List collections of a user with pagination.
|
||||||
func ListUserCollections(username string, page int) ([]*model.CollectionView, int64, error) {
|
func ListUserCollections(username string, page int) ([]*model.CollectionView, int64, error) {
|
||||||
if username == "" || page < 1 {
|
if username == "" || page < 1 {
|
||||||
return nil, 0, errors.New("invalid parameters")
|
return nil, 0, model.NewRequestError("invalid parameters")
|
||||||
}
|
}
|
||||||
user, err := dao.GetUserByUsername(username)
|
user, err := dao.GetUserByUsername(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -119,7 +118,7 @@ func ListUserCollections(username string, page int) ([]*model.CollectionView, in
|
|||||||
// List resources in a collection with pagination.
|
// List resources in a collection with pagination.
|
||||||
func ListCollectionResources(collectionID uint, page int) ([]*model.ResourceView, int64, error) {
|
func ListCollectionResources(collectionID uint, page int) ([]*model.ResourceView, int64, error) {
|
||||||
if collectionID == 0 || page < 1 {
|
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)
|
resources, total, err := dao.ListCollectionResources(collectionID, page, pageSize)
|
||||||
if err != nil {
|
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.
|
// excludedRID: if >0, only return collections not containing this resource.
|
||||||
func SearchUserCollections(username string, keyword string, excludedRID uint) ([]*model.CollectionView, error) {
|
func SearchUserCollections(username string, keyword string, excludedRID uint) ([]*model.CollectionView, error) {
|
||||||
if username == "" {
|
if username == "" {
|
||||||
return nil, errors.New("invalid parameters")
|
return nil, model.NewRequestError("invalid parameters")
|
||||||
}
|
}
|
||||||
user, err := dao.GetUserByUsername(username)
|
user, err := dao.GetUserByUsername(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user