69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
// internal/services/audit_service.go
|
|
package services
|
|
|
|
import (
|
|
"lost-and-found/internal/models"
|
|
"lost-and-found/internal/repositories"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AuditService struct {
|
|
auditLogRepo *repositories.AuditLogRepository
|
|
}
|
|
|
|
func NewAuditService(db *gorm.DB) *AuditService {
|
|
return &AuditService{
|
|
auditLogRepo: repositories.NewAuditLogRepository(db),
|
|
}
|
|
}
|
|
|
|
// GetAllAuditLogs gets all audit logs
|
|
func (s *AuditService) GetAllAuditLogs(page, limit int, action, entityType string, userID *uint) ([]models.AuditLogResponse, int64, error) {
|
|
logs, total, err := s.auditLogRepo.FindAll(page, limit, action, entityType, userID)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var responses []models.AuditLogResponse
|
|
for _, log := range logs {
|
|
responses = append(responses, log.ToResponse())
|
|
}
|
|
|
|
return responses, total, nil
|
|
}
|
|
|
|
// GetAuditLogsByUser gets audit logs by user
|
|
func (s *AuditService) GetAuditLogsByUser(userID uint, page, limit int) ([]models.AuditLogResponse, int64, error) {
|
|
logs, total, err := s.auditLogRepo.FindByUser(userID, page, limit)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var responses []models.AuditLogResponse
|
|
for _, log := range logs {
|
|
responses = append(responses, log.ToResponse())
|
|
}
|
|
|
|
return responses, total, nil
|
|
}
|
|
|
|
// GetAuditLogsByEntity gets audit logs by entity
|
|
func (s *AuditService) GetAuditLogsByEntity(entityType string, entityID uint, page, limit int) ([]models.AuditLogResponse, int64, error) {
|
|
logs, total, err := s.auditLogRepo.FindByEntity(entityType, entityID, page, limit)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var responses []models.AuditLogResponse
|
|
for _, log := range logs {
|
|
responses = append(responses, log.ToResponse())
|
|
}
|
|
|
|
return responses, total, nil
|
|
}
|
|
|
|
// LogAction creates a new audit log entry
|
|
func (s *AuditService) LogAction(userID *uint, action, entityType string, entityID *uint, details, ipAddress, userAgent string) error {
|
|
return s.auditLogRepo.Log(userID, action, entityType, entityID, details, ipAddress, userAgent)
|
|
} |