Files
raven/server/AyaNova/biz/EventLogProcessor.cs
2018-08-28 17:30:56 +00:00

127 lines
3.6 KiB
C#

using System;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using AyaNova.Models;
using AyaNova.Util;
namespace AyaNova.Biz
{
internal static class EventLogProcessor
{
private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("EventLogProcessor");
/// <summary>
/// Add an entry to the log
/// </summary>
/// <param name="newEvent"></param>
/// <param name="ct"></param>
/// <returns></returns>
internal static void AddEntry(Event newEvent, AyContext ct)
{
ct.Event.Add(newEvent);
ct.SaveChanges();
}
/// <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 void DeleteObject(long userId, AyaType ayType, long ayId, string textra, AyContext ct)
{
ct.Database.ExecuteSqlCommand($"delete from aevent where aytype = {ayType} and ayid={ayId}");
ct.Event.Add(new Event(userId, ayId, ayType, AyaEvent.Deleted, textra));
ct.SaveChanges();
}
/// <summary>
/// Get the event log for a specified object
/// </summary>
internal static async Task<AyaNova.Api.Controllers.EventLogController.ObjectEventLogItem[]> GetLogForObject(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);
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.OwnerId,
Event = m.AyEvent,
Textra = m.Textra
}).ToArray();
return ret;
}
internal static Task<string> GetLogForUser(AyaNova.Api.Controllers.EventLogController.EventLogOptions opt, AyContext ct)
{
throw new NotImplementedException();
}
internal static string BuildLogEntry(bool userFormat, Event ev, List<KeyValuePair<string, string>> lt)
{
StringBuilder S = new StringBuilder();
//Object format:
//DateTime, UserId, ActionNumber, Textra
//User format:
//DateTime, ObjectType, ObjectId, ActionNumber, Textra
ev
switch (ev.AyEvent)
{
case AyaEvent.Created:
break;
}
return ":";
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons