Files
raven/server/AyaNova/biz/EventLogProcessor.cs
2020-01-27 18:16:16 +00:00

125 lines
4.1 KiB
C#

using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using AyaNova.Models;
namespace AyaNova.Biz
{
internal static class EventLogProcessor
{
/// <summary>
/// Add an entry to the log
///
/// </summary>
/// <param name="newEvent"></param>
/// <param name="ct"></param>
/// <returns></returns>
internal static async Task LogEventToDatabaseAsync(Event newEvent, AyContext ct)
{
await ct.Event.AddAsync(newEvent);
await ct.SaveChangesAsync();
}
/// <summary>
/// Handle delete
/// remove all prior entries for object, add one deleted entry
/// </summary>
/// <param name="userId"></param>
/// <param name="ayType"></param>
/// <param name="ayId"></param>
/// <param name="textra"></param>
/// <param name="ct"></param>
internal static async Task DeleteObjectLogAsync(long userId, AyaType ayType, long ayId, string textra, AyContext ct)
{
await ct.Database.ExecuteSqlInterpolatedAsync($"delete from aevent where aytype = {ayType} and ayid={ayId}");
await ct.Event.AddAsync(new Event(userId, ayId, ayType, AyaEvent.Deleted, textra));
await ct.SaveChangesAsync();
}
/// <summary>
/// Get the event log for a specified object
/// Presentation is the client's responsibility (localization internationalization etc)
/// </summary>
internal static async Task<AyaNova.Api.Controllers.EventLogController.ObjectEventLogItem[]> GetLogForObjectAsync(AyaNova.Api.Controllers.EventLogController.EventLogOptions opt, AyContext ct)
{
//This is also an example of conditional where statements
//Set up the query
var q = ct.Event.Select(m => m).AsNoTracking();
q = q.Where(m => m.AyId == opt.AyId);
if (opt.StartDate != null)
q = q.Where(m => m.Created > opt.StartDate);
if (opt.EndDate != null)
q = q.Where(m => m.Created < opt.EndDate);
q = q.OrderBy(m => m.Created);
//Execute the query
var EventItems = await q.ToArrayAsync();
//convert the Event array to the correct return type array
var ret = EventItems.Select(m => new AyaNova.Api.Controllers.EventLogController.ObjectEventLogItem()
{
Date = m.Created,
UserId = m.UserId,
Event = m.AyEvent,
Textra = m.Textra
}).ToArray();
return ret;
}
/// <summary>
/// Get the event log for a specified User
/// Presentation is the client's responsibility (localization internationalization etc)
/// </summary>
internal static async Task<AyaNova.Api.Controllers.EventLogController.UserEventLogItem[]> GetLogForUserAsync(AyaNova.Api.Controllers.EventLogController.EventLogOptions opt, AyContext ct)
{
//Set up the query
var q = ct.Event.Select(m => m);
q = q.Where(m => m.UserId == opt.AyId);
if (opt.StartDate != null)
q = q.Where(m => m.Created > opt.StartDate);
if (opt.EndDate != null)
q = q.Where(m => m.Created < opt.EndDate);
q = q.OrderBy(m => m.Created);
//Execute the query
var EventItems = await q.ToArrayAsync();
//convert the Event array to the correct return type array
var ret = EventItems.Select(m => new AyaNova.Api.Controllers.EventLogController.UserEventLogItem()
{
Date = m.Created,
ObjectType = m.AyType,
ObjectId = m.AyId,
Event = m.AyEvent,
Textra = m.Textra
}).ToArray();
return ret;
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons