using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using AyaNova.Models; namespace AyaNova.Biz { internal static class EventLogProcessor { private const int DEFAULT_EVENT_LIMIT = 20; /// /// Add an entry to the log /// /// /// /// /// internal static async Task LogEventToDatabaseAsync(Event newEvent, AyContext ct) { //System.Diagnostics.Debug.WriteLine($"Event log event for {newEvent.AyId}:{newEvent.AyType} {newEvent.AyEvent} {newEvent.Created}"); await ct.Event.AddAsync(newEvent); await ct.SaveChangesAsync(); } /// /// Handle delete /// remove all prior entries for object, add one deleted entry /// /// /// /// /// /// 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(); } /// /// Get the event log for a specified object /// Presentation is the client's responsibility (localization internationalization etc) /// internal static async Task GetLogForObjectAsync(AyaNova.Api.Controllers.EventLogController.EventLogOptions opt, AyContext ct) { AyaNova.Api.Controllers.EventLogController.ObjectEventLog ret = new Api.Controllers.EventLogController.ObjectEventLog(); var limit = opt.Limit ?? DEFAULT_EVENT_LIMIT; var offset = opt.Offset ?? 0; //Set up the query var q = ct.Event.Select(m => m).Skip(offset).Take(limit).AsNoTracking(); q = q.Where(m => m.AyId == opt.AyId && m.AyType== opt.AyType); q = q.OrderByDescending(m => m.Created); q = q.Skip(offset).Take(limit); //Execute the query var EventItems = await q.ToArrayAsync(); //convert the Event array to the correct return type array using (var command = ct.Database.GetDbConnection().CreateCommand()) { ct.Database.OpenConnection(); ret.Events = EventItems.Select(m => new AyaNova.Api.Controllers.EventLogController.ObjectEventLogItem() { Date = m.Created, UserId = m.UserId, Event = m.AyEvent, Textra = m.Textra, Name = BizObjectNameFetcherDirect.Name(AyaType.User, m.UserId, command) }).ToArray(); ret.Name = BizObjectNameFetcherDirect.Name(opt.AyType, opt.AyId, command); return ret; } } /// /// Get the event log for a specified User /// Presentation is the client's responsibility (localization internationalization etc) /// internal static async Task GetLogForUserAsync(AyaNova.Api.Controllers.EventLogController.UserEventLogOptions opt, AyContext ct) { AyaNova.Api.Controllers.EventLogController.UserEventLog ret = new Api.Controllers.EventLogController.UserEventLog(); var limit = opt.Limit ?? DEFAULT_EVENT_LIMIT; var offset = opt.Offset ?? 0; //Set up the query var q = ct.Event.Select(m => m).Skip(offset).Take(limit).AsNoTracking(); q = q.Where(m => m.UserId == opt.UserId); q = q.OrderByDescending(m => m.Created); //Execute the query var EventItems = await q.ToArrayAsync(); using (var command = ct.Database.GetDbConnection().CreateCommand()) { ct.Database.OpenConnection(); //convert the Event array to the correct return type array ret.Events = EventItems.Select(m => new AyaNova.Api.Controllers.EventLogController.UserEventLogItem() { Date = m.Created, ObjectType = m.AyType, ObjectId = m.AyId, Event = m.AyEvent, Textra = m.Textra, Name = BizObjectNameFetcherDirect.Name(m.AyType, m.AyId, command) }).ToArray(); ret.Name = BizObjectNameFetcherDirect.Name(AyaType.User, opt.UserId, command); return ret; } } ///////////////////////////////////////////////////////////////////// }//eoc }//eons