Audit Service

This commit is contained in:
Andre Beging
2022-05-23 10:29:10 +02:00
parent 5961c06004
commit 5d713db83f
14 changed files with 552 additions and 49 deletions

View File

@@ -1,6 +1,5 @@
using FoodsharingSiegen.Contracts.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace FoodsharingSiegen.Server.Data
{
@@ -26,6 +25,11 @@ namespace FoodsharingSiegen.Server.Data
/// Gets or sets the value of the users (ab)
/// </summary>
public DbSet<User>? Users { get; set; }
/// <summary>
/// Gets or sets the value of the audits (ab)
/// </summary>
public DbSet<Audit>? Audits { get; set; }
#endregion

View File

@@ -0,0 +1,95 @@
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
namespace FoodsharingSiegen.Server.Data.Service
{
/// <summary>
/// The audit service class (a. beging, 23.05.2022)
/// </summary>
/// <seealso cref="ServiceBase"/>
public class AuditService : ServiceBase
{
#region Setup/Teardown
/// <summary>
/// Initializes a new instance of the <see cref="AuditService"/> class
/// </summary>
/// <param name="context">The context (ab)</param>
/// <param name="authService"></param>
public AuditService(FsContext context, AuthService authService) : base(context, authService) { }
#endregion
#region Public Method Insert
/// <summary>
/// Inserts the type (a. beging, 23.05.2022)
/// </summary>
/// <param name="type">The type</param>
/// <param name="data1">The data</param>
/// <param name="data2">The data</param>
/// <returns>A task containing an operation result of audit</returns>
public async Task<OperationResult<Audit>> Insert(AuditType type, string? data1 = null, string? data2 = null)
{
try
{
var audit = new Audit
{
Type = type,
UserID = CurrentUser?.Id,
Data1 = data1,
Data2 = data2
};
Context.Audits?.Add(audit);
var saveR = await Context.SaveChangesAsync();
if (saveR > 0)
return new OperationResult<Audit>(audit);
return new OperationResult<Audit>(new Exception("Couldn't add audit"));
}
catch (Exception e)
{
return new OperationResult<Audit>(e);
}
}
#endregion
#region Public Method Load
/// <summary>
/// Loads the count (a. beging, 23.05.2022)
/// </summary>
/// <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)
{
try
{
var query = Context.Audits?.OrderBy(x => x.Created).AsQueryable();
if (count > 0)
query = query?.Take(count);
if (type != null)
query = query?.Where(x => x.Type == type);
var mat = query?.ToList();
if (mat != null) return new OperationResult<List<Audit>>(mat);
return new OperationResult<List<Audit>>(new Exception("Couldn't load audits"));
}
catch (Exception e)
{
return new OperationResult<List<Audit>>(e);
}
}
#endregion
}
}

View File

@@ -1,5 +1,6 @@
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
@@ -10,13 +11,17 @@ namespace FoodsharingSiegen.Server.Data.Service
/// <seealso cref="ServiceBase"/>
public class ProspectService : ServiceBase
{
public AuditService AuditService { get; }
#region Setup/Teardown
/// <summary>
/// Initializes a new instance of the <see cref="ProspectService"/> class
/// </summary>
/// <param name="context">The context</param>
public ProspectService(FsContext context) : base(context) { }
/// <param name="authService"></param>
/// <param name="auditService"></param>
public ProspectService(FsContext context, AuthService authService, AuditService auditService) : base(context, authService) => AuditService = auditService;
#endregion
@@ -75,7 +80,10 @@ namespace FoodsharingSiegen.Server.Data.Service
var saveR = await Context.SaveChangesAsync();
if (saveR > 0)
{
await AuditService.Insert(AuditType.CreateProspect, prospect.Name, prospect.FsId.ToString());
return new OperationResult<Prospect>(prospect);
}
return new OperationResult<Prospect>(new Exception("Couldn't add prospect"));
}

View File

@@ -1,21 +1,50 @@
namespace FoodsharingSiegen.Server.Data.Service
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
namespace FoodsharingSiegen.Server.Data.Service
{
/// <summary>
/// The service base class (a. beging, 23.05.2022)
/// </summary>
public class ServiceBase
{
#region Public Properties
/// <summary>
/// Gets the value of the auth service (ab)
/// </summary>
public AuthService AuthService { get; }
#endregion
#region Setup/Teardown
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Specialised constructor for use only by derived class. </summary>
///
/// <remarks> A Beging, 20.10.2021. </remarks>
///
/// <param name="context"> The context. </param>
/// <param name="authService"></param>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected ServiceBase(FsContext context, AuthService authService)
{
Context = context;
AuthService = authService;
}
#endregion
/// <summary>
/// Gets the value of the current user (ab)
/// </summary>
protected User? CurrentUser => AuthService.User;
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Database Context </summary>
///
/// <value> The context. </value>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected FsContext Context { get; }
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Specialised constructor for use only by derived class. </summary>
///
/// <remarks> A Beging, 20.10.2021. </remarks>
///
/// <param name="context"> The context. </param>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected ServiceBase(FsContext context) => Context = context;
}
}

View File

@@ -1,5 +1,6 @@
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
@@ -16,10 +17,8 @@ namespace FoodsharingSiegen.Server.Data.Service
/// Initializes a new instance of the <see cref="UserService"/> class
/// </summary>
/// <param name="context">The context</param>
public UserService(FsContext context) : base(context)
{
}
/// <param name="authService"></param>
public UserService(FsContext context, AuthService authService) : base(context, authService) { }
#endregion
@@ -58,28 +57,6 @@ namespace FoodsharingSiegen.Server.Data.Service
#endregion
#region Public Method CheckForceLogout
/// <summary>
/// Checks the force logout using the specified user (a. beging, 11.04.2022)
/// </summary>
/// <param name="user">The user</param>
/// <returns>A task containing an operation result of bool</returns>
public async Task<OperationResult<bool>> CheckForceLogout(User user)
{
try
{
var anyR = await Context.Users.AnyAsync(x => x.Id == user.Id && x.ForceLogout);
return new OperationResult<bool>(anyR);
}
catch (Exception e)
{
return new OperationResult<bool>(e);
}
}
#endregion
#region Public Method GetUsersAsync
////////////////////////////////////////////////////////////////////////////////////////////////////