Uploading a character image

This commit is contained in:
2025-12-29 13:37:55 +01:00
parent b41b06763c
commit 3a12b613d9
7 changed files with 586 additions and 2 deletions
+45
View File
@@ -0,0 +1,45 @@
package character
import (
"bamort/database"
"bamort/logger"
"bamort/models"
"net/http"
"github.com/gin-gonic/gin"
)
type ImageUpdateRequest struct {
Image string `json:"image" binding:"required"`
}
func UpdateCharacterImage(c *gin.Context) {
id := c.Param("id")
logger.Debug("UpdateCharacterImage called for character ID: %s", id)
var character models.Char
err := character.FirstID(id)
if err != nil {
logger.Error("Character not found: %s", err.Error())
respondWithError(c, http.StatusNotFound, "Character not found")
return
}
var request ImageUpdateRequest
if err := c.ShouldBindJSON(&request); err != nil {
logger.Error("Invalid request data: %s", err.Error())
respondWithError(c, http.StatusBadRequest, "Invalid request data")
return
}
character.Image = request.Image
if err := database.DB.Save(&character).Error; err != nil {
logger.Error("Failed to update character image: %s", err.Error())
respondWithError(c, http.StatusInternalServerError, "Failed to update character image")
return
}
logger.Info("Character image updated successfully for ID: %s", id)
c.JSON(http.StatusOK, gin.H{"message": "Image updated successfully", "image": character.Image})
}