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})
}
+99
View File
@@ -0,0 +1,99 @@
package character
import (
"bamort/database"
"bamort/models"
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestUpdateCharacterImage(t *testing.T) {
setupTestEnvironment(t)
database.SetupTestDB()
router := gin.Default()
protected := router.Group("/api")
protected.Use(func(c *gin.Context) {
c.Set("userID", uint(1))
c.Next()
})
RegisterRoutes(protected)
// Get existing character
var char models.Char
err := char.FirstID("18")
assert.NoError(t, err, "Test character 18 should exist")
// Prepare image data
imageData := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
requestBody := map[string]string{
"image": imageData,
}
jsonData, _ := json.Marshal(requestBody)
// Update character image
req, _ := http.NewRequest("PUT", "/api/characters/18/image", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "Should successfully update image")
// Verify image was saved
var updatedChar models.Char
err = updatedChar.FirstID("18")
assert.NoError(t, err)
assert.Equal(t, imageData, updatedChar.Image, "Image should be updated in database")
}
func TestUpdateCharacterImageInvalidID(t *testing.T) {
setupTestEnvironment(t)
database.SetupTestDB()
router := gin.Default()
protected := router.Group("/api")
protected.Use(func(c *gin.Context) {
c.Set("userID", uint(1))
c.Next()
})
RegisterRoutes(protected)
requestBody := map[string]string{
"image": "data:image/png;base64,test",
}
jsonData, _ := json.Marshal(requestBody)
req, _ := http.NewRequest("PUT", "/api/characters/99999/image", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code, "Should return 404 for non-existent character")
}
func TestUpdateCharacterImageInvalidData(t *testing.T) {
setupTestEnvironment(t)
database.SetupTestDB()
router := gin.Default()
protected := router.Group("/api")
protected.Use(func(c *gin.Context) {
c.Set("userID", uint(1))
c.Next()
})
RegisterRoutes(protected)
req, _ := http.NewRequest("PUT", "/api/characters/18/image", bytes.NewBuffer([]byte("invalid json")))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code, "Should return 400 for invalid JSON")
}
+1
View File
@@ -11,6 +11,7 @@ func RegisterRoutes(r *gin.RouterGroup) {
charGrp.GET("/:id", GetCharacter)
charGrp.PUT("/:id", UpdateCharacter)
charGrp.DELETE("/:id", DeleteCharacter)
charGrp.PUT("/:id/image", UpdateCharacterImage)
// Erfahrung und Vermögen
charGrp.GET("/:id/experience-wealth", GetCharacterExperienceAndWealth) // NewSystem