Audit Service
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
95
FoodsharingSiegen.Server/Data/Service/AuditService.cs
Normal file
95
FoodsharingSiegen.Server/Data/Service/AuditService.cs
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user