Files
bamort/backend/bmrt/character/audit_log_handlers.go
T
Bardioc26 042a1d4773 Learncost frontend (#42)
* introduced central package  registry by package init function
* dynamic registration of routes, model, migrations and initializers.
* setting a docker compose project name to prevent shutdown of other containers with the same (composer)name
* ai documentation
* app template
* Create tests for ALL API entpoints in ALL packages Based on current data. Ensure that all API endpoints used in frontend are tested. These tests are crucial for the next refactoring tasks.
* adopting agent instructions for a more consistent coding style
* added desired module layout and debugging information
* Fix All Failing tests All failing tests are fixed now that makes the refactoring more easy since all tests must pass
* restored routes for maintenance
* added common translations
* added new tests for API Endpoint
* Merge branch 'separate_business_logic'
* added lern and skill improvement cost editing
* Set Docker image tag when building to prevent rebuild when nothing has changed
* add and remove PP for Weaponskill fixed
* add and remove PP for same named skills fixed
* add new task
2026-05-01 18:15:31 +02:00

106 lines
2.5 KiB
Go

package character
import (
"bamort/bmrt/models"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
// GetCharacterAuditLog gibt alle Audit-Log-Einträge für einen Charakter zurück
func GetCharacterAuditLog(c *gin.Context) {
charID := c.Param("id")
// Konvertiere String zu uint
id, err := strconv.ParseUint(charID, 10, 32)
if err != nil {
respondWithError(c, http.StatusBadRequest, "Invalid character ID")
return
}
// Filter für spezifisches Feld (optional)
fieldName := c.Query("field")
var entries []models.AuditLogEntry
if fieldName != "" {
entries, err = GetAuditLogForField(uint(id), fieldName)
} else {
entries, err = GetAuditLogForCharacter(uint(id))
}
if err != nil {
respondWithError(c, http.StatusInternalServerError, "Failed to retrieve audit log")
return
}
c.JSON(http.StatusOK, gin.H{
"character_id": uint(id),
"entries": entries,
})
}
// GetAuditLogStats gibt Statistiken über Änderungen zurück
func GetAuditLogStats(c *gin.Context) {
charID := c.Param("id")
// Konvertiere String zu uint
id, err := strconv.ParseUint(charID, 10, 32)
if err != nil {
respondWithError(c, http.StatusBadRequest, "Invalid character ID")
return
}
// Lade alle Einträge
entries, err := GetAuditLogForCharacter(uint(id))
if err != nil {
respondWithError(c, http.StatusInternalServerError, "Failed to retrieve audit log")
return
}
// Berechne Statistiken
stats := map[string]interface{}{
"total_changes": len(entries),
"by_field": make(map[string]int),
"by_reason": make(map[string]int),
"total_ep_spent": 0,
"total_ep_gained": 0,
"total_gold_spent": 0,
"total_gold_gained": 0,
}
fieldStats := stats["by_field"].(map[string]int)
reasonStats := stats["by_reason"].(map[string]int)
for _, entry := range entries {
// Zähle nach Feld
fieldStats[entry.FieldName]++
// Zähle nach Grund
reasonStats[entry.Reason]++
// Summen für EP und Gold
if entry.FieldName == "experience_points" {
if entry.Difference > 0 {
stats["total_ep_gained"] = stats["total_ep_gained"].(int) + entry.Difference
} else {
stats["total_ep_spent"] = stats["total_ep_spent"].(int) + (-entry.Difference)
}
}
if entry.FieldName == "gold" {
if entry.Difference > 0 {
stats["total_gold_gained"] = stats["total_gold_gained"].(int) + entry.Difference
} else {
stats["total_gold_spent"] = stats["total_gold_spent"].(int) + (-entry.Difference)
}
}
}
c.JSON(http.StatusOK, gin.H{
"character_id": uint(id),
"stats": stats,
})
}