63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
using audit_logs.Repositories;
|
|
using MongoDB.Driver;
|
|
|
|
namespace audit_logs.Services;
|
|
using audit_logs.Utils;
|
|
using audit.Model;
|
|
using audit.Repositories;
|
|
using audit.Repositories.Interfaces;
|
|
using Serilog;
|
|
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
public class AuditService
|
|
{
|
|
|
|
private readonly IAuditRecordRepository _auditLogRepository;
|
|
|
|
public AuditService(IAuditRecordRepository auditLogRepository)
|
|
{
|
|
_auditLogRepository = auditLogRepository;
|
|
}
|
|
|
|
public AuditService(IMongoDatabase database)
|
|
{
|
|
_auditLogRepository = new AuditRecordRepository(database);
|
|
|
|
}
|
|
public IAuditRecordRepository AuditLogRepository => _auditLogRepository;
|
|
|
|
public async Task RecordAuditAsync(string entityType, string recordId, string userId, string actionType, string reason,DateTime actionTime, List<AuditRecord.Change> changes)
|
|
{
|
|
if (entityType == null) throw new ArgumentNullException(nameof(entityType));
|
|
if (recordId == null) throw new ArgumentNullException(nameof(recordId));
|
|
if (userId == null) throw new ArgumentNullException(nameof(userId));
|
|
if (actionType == null) throw new ArgumentNullException(nameof(actionType));
|
|
if (changes == null) throw new ArgumentNullException(nameof(changes));
|
|
|
|
var auditRecord = new AuditRecord
|
|
{
|
|
EntityType = entityType,
|
|
RecordId = recordId,
|
|
UserId = userId,
|
|
ActionType = actionType,
|
|
ActionTime = actionTime,
|
|
Changes = changes,
|
|
Reason = reason
|
|
};
|
|
|
|
await _auditLogRepository.InsertOneAsync(auditRecord);
|
|
}
|
|
|
|
public async Task CreateAuditLogAsync(string entityType, string recordId, string userId, string actionType,string reason,DateTime actionTime,string originalJson, string modifiedJson)
|
|
{
|
|
JsonComparer jsonComparer = new();
|
|
List<AuditRecord.Change> changes = jsonComparer.GetDifferences(originalJson, modifiedJson);
|
|
RecordAuditAsync(entityType,recordId,userId,actionType,reason,actionTime,changes).Wait();
|
|
string s = "";
|
|
|
|
}
|
|
|
|
} |