Files
bamort/backend/bmrt/character/audit_log.go
T

60 lines
1.9 KiB
Go
Raw Normal View History

2025-07-24 07:39:43 +02:00
package character
import (
"bamort/database"
2026-05-01 18:15:31 +02:00
"bamort/bmrt/models"
2025-07-24 07:39:43 +02:00
)
// AuditLogReason definiert Standard-Gründe für Änderungen
type AuditLogReason string
const (
ReasonManual AuditLogReason = "manual"
ReasonSkillLearning AuditLogReason = "skill_learning"
ReasonSkillImprovement AuditLogReason = "skill_improvement"
ReasonSpellLearning AuditLogReason = "spell_learning"
ReasonEquipment AuditLogReason = "equipment"
ReasonReward AuditLogReason = "reward"
ReasonCorrection AuditLogReason = "correction"
ReasonImport AuditLogReason = "import"
)
// CreateAuditLogEntry erstellt einen neuen Audit-Log-Eintrag
func CreateAuditLogEntry(characterID uint, fieldName string, oldValue, newValue int, reason AuditLogReason, userID uint, notes string) error {
2025-08-05 21:09:48 +02:00
entry := models.AuditLogEntry{
2025-07-24 07:39:43 +02:00
CharacterID: characterID,
FieldName: fieldName,
OldValue: oldValue,
NewValue: newValue,
Difference: newValue - oldValue,
Reason: string(reason),
UserID: userID,
Notes: notes,
}
return database.DB.Create(&entry).Error
}
// GetAuditLogForCharacter holt alle Audit-Log-Einträge für einen Charakter
2025-08-05 21:09:48 +02:00
func GetAuditLogForCharacter(characterID uint) ([]models.AuditLogEntry, error) {
var entries []models.AuditLogEntry
2025-07-24 07:39:43 +02:00
err := database.DB.Where("character_id = ?", characterID).
Order("timestamp DESC").
Find(&entries).Error
return entries, err
}
// GetAuditLogForField holt Audit-Log-Einträge für ein spezifisches Feld
2025-08-05 21:09:48 +02:00
func GetAuditLogForField(characterID uint, fieldName string) ([]models.AuditLogEntry, error) {
var entries []models.AuditLogEntry
2025-07-24 07:39:43 +02:00
err := database.DB.Where("character_id = ? AND field_name = ?", characterID, fieldName).
Order("timestamp DESC").
Find(&entries).Error
return entries, err
}
// MigrateAuditLog führt die Migration für die Audit-Log-Tabelle durch
func MigrateAuditLog() error {
2025-08-05 21:09:48 +02:00
return database.DB.AutoMigrate(&models.AuditLogEntry{})
2025-07-24 07:39:43 +02:00
}