mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15:51:14 +00:00
update character image
This commit is contained in:
@@ -298,6 +298,47 @@ func handleGetCharactersFromVndb(c fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleUpdateCharacterImage(c fiber.Ctx) error {
|
||||||
|
resourceIdStr := c.Params("resourceId")
|
||||||
|
characterIdStr := c.Params("characterId")
|
||||||
|
if resourceIdStr == "" || characterIdStr == "" {
|
||||||
|
return model.NewRequestError("Resource ID and Character ID are required")
|
||||||
|
}
|
||||||
|
resourceId, err := strconv.Atoi(resourceIdStr)
|
||||||
|
if err != nil {
|
||||||
|
return model.NewRequestError("Invalid resource ID")
|
||||||
|
}
|
||||||
|
characterId, err := strconv.Atoi(characterIdStr)
|
||||||
|
if err != nil {
|
||||||
|
return model.NewRequestError("Invalid character ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
var params struct {
|
||||||
|
ImageID uint `json:"image_id"`
|
||||||
|
}
|
||||||
|
body := c.Body()
|
||||||
|
err = json.Unmarshal(body, ¶ms)
|
||||||
|
if err != nil {
|
||||||
|
return model.NewRequestError("Invalid request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
uid, ok := c.Locals("uid").(uint)
|
||||||
|
if !ok {
|
||||||
|
return model.NewUnAuthorizedError("You must be logged in to update a character")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = service.UpdateCharacterImage(uid, uint(resourceId), uint(characterId), params.ImageID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(model.Response[any]{
|
||||||
|
Success: true,
|
||||||
|
Data: nil,
|
||||||
|
Message: "Character image updated successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func AddResourceRoutes(api fiber.Router) {
|
func AddResourceRoutes(api fiber.Router) {
|
||||||
resource := api.Group("/resource")
|
resource := api.Group("/resource")
|
||||||
{
|
{
|
||||||
@@ -312,5 +353,6 @@ func AddResourceRoutes(api fiber.Router) {
|
|||||||
resource.Get("/tag/:tag", handleListResourcesWithTag)
|
resource.Get("/tag/:tag", handleListResourcesWithTag)
|
||||||
resource.Get("/user/:username", handleGetResourcesWithUser)
|
resource.Get("/user/:username", handleGetResourcesWithUser)
|
||||||
resource.Post("/:id", handleUpdateResource)
|
resource.Post("/:id", handleUpdateResource)
|
||||||
|
resource.Put("/:resourceId/character/:characterId/image", handleUpdateCharacterImage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -517,3 +517,15 @@ func CountResources() (int64, error) {
|
|||||||
}
|
}
|
||||||
return count, nil
|
return count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateCharacterImage 更新角色的图片ID
|
||||||
|
func UpdateCharacterImage(characterID, imageID uint) error {
|
||||||
|
result := db.Model(&model.Character{}).Where("id = ?", characterID).Update("image_id", imageID)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
if result.RowsAffected == 0 {
|
||||||
|
return model.NewNotFoundError("Character not found")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -763,3 +763,33 @@ func downloadAndCreateImage(imageURL string) (uint, error) {
|
|||||||
|
|
||||||
return imageID, nil
|
return imageID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateCharacterImage 更新角色的图片ID
|
||||||
|
func UpdateCharacterImage(uid, resourceID, characterID, imageID uint) error {
|
||||||
|
// 检查资源是否存在并且用户有权限修改
|
||||||
|
resource, err := dao.GetResourceByID(resourceID)
|
||||||
|
if err != nil {
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
return model.NewNotFoundError("Resource not found")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
isAdmin, err := CheckUserIsAdmin(uid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查用户是否有权限修改这个资源
|
||||||
|
if resource.UserID != uid && !isAdmin {
|
||||||
|
return model.NewUnAuthorizedError("You don't have permission to modify this resource")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新角色图片
|
||||||
|
err = dao.UpdateCharacterImage(characterID, imageID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user