Create Audits on Actions

This commit is contained in:
Andre Beging
2022-05-23 11:07:52 +02:00
parent 5d713db83f
commit b16951a1a6
11 changed files with 358 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
{
@@ -36,6 +37,7 @@ namespace FoodsharingSiegen.Server.Data.Service
{
var audit = new Audit
{
Created = DateTime.Now,
Type = type,
UserID = CurrentUser?.Id,
Data1 = data1,
@@ -66,11 +68,11 @@ namespace FoodsharingSiegen.Server.Data.Service
/// <param name="count">The count</param>
/// <param name="type">The type</param>
/// <returns>A task containing an operation result of list audit</returns>
public async Task<OperationResult<List<Audit>>> Load(int count, AuditType? type)
public async Task<OperationResult<List<Audit>>> Load(int count, AuditType? type = null)
{
try
{
var query = Context.Audits?.OrderBy(x => x.Created).AsQueryable();
var query = Context.Audits?.Include(x => x.User).OrderByDescending(x => x.Created).AsQueryable();
if (count > 0)
query = query?.Take(count);

View File

@@ -46,6 +46,8 @@ namespace FoodsharingSiegen.Server.Data.Service
await Context.SaveChangesAsync();
await AuditService.Insert(AuditType.AddInteraction, targetProspect.Name, interaction.Type.ToString());
// Detatch entities
Context.Entry(targetProspect).State = EntityState.Detached;
Context.Entry(interaction).State = EntityState.Detached;
@@ -130,6 +132,8 @@ namespace FoodsharingSiegen.Server.Data.Service
Context.Interactions.Remove(new Interaction { Id = interactionId });
await Context.SaveChangesAsync();
await AuditService.Insert(AuditType.RemoveInteraction, "?");
return new OperationResult();
}
catch (Exception e)
@@ -161,6 +165,8 @@ namespace FoodsharingSiegen.Server.Data.Service
var saveR = await Context.SaveChangesAsync();
if(saveR < 1) return new OperationResult(new Exception("Fehler beim speichern"));
await AuditService.Insert(AuditType.EditProspect, prospect.Name);
return new OperationResult();
}
catch (Exception e)