using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
namespace FoodsharingSiegen.Server.Data.Service
{
///
/// The audit service class (a. beging, 23.05.2022)
///
///
public class AuditService : ServiceBase
{
#region Setup/Teardown
///
/// Initializes a new instance of the class
///
/// The context (ab)
///
public AuditService(FsContext context, AuthService authService) : base(context, authService) { }
#endregion
#region Public Method Insert
///
/// Inserts the type (a. beging, 23.05.2022)
///
/// The type
/// The data
/// The data
/// A task containing an operation result of audit
public async Task> 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);
return new OperationResult(new Exception("Couldn't add audit"));
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
#region Public Method Load
///
/// Loads the count (a. beging, 23.05.2022)
///
/// The count
/// The type
/// A task containing an operation result of list audit
public async Task>> 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>(mat);
return new OperationResult>(new Exception("Couldn't load audits"));
}
catch (Exception e)
{
return new OperationResult>(e);
}
}
#endregion
}
}