public async Task DeleteOldLogsAsync(int daysTreshold) { var now = DateTime.Now.Date; var oldDate = now.AddDays(-1 * daysTreshold); using (var context = new AppsEntities()) { var logsTask = context.Logs.Where(l => l.DateCreated < oldDate).ToListAsync(); var screenshotsTask = context.Screenshots.Where(s => s.Date < oldDate).ToListAsync(); var usagesTask = context.Usages.Where(u => u.UsageStart < oldDate && u.IsCurrent == false) .Include(u => u.Logs).ToListAsync(); await Task.WhenAll(logsTask, screenshotsTask, usagesTask); var usages = usagesTask.Result; var usageLogs = usages.SelectMany(u => u.Logs); var logs = logsTask.Result; logs.AddRange(usageLogs); logs = logs.Distinct().ToList(); var screenshotsLogs = logs.SelectMany(l => l.Screenshots); var screenshots = screenshotsTask.Result; screenshots.AddRange(screenshotsLogs); screenshots = screenshots.Distinct().ToList(); context.Screenshots.RemoveRange(screenshots); context.Logs.RemoveRange(logs); context.Usages.RemoveRange(usages); await context.SaveChangesAsync(); } await DeleteEmptyLogsAsync(); }
public async Task SaveNewEntityRangeAsync <T>(IEnumerable <T> items) where T : class { using (var context = new AppsEntities()) { context.Set <T>().AddRange(items); await context.SaveChangesAsync(); } }
public async Task SaveNewEntityAsync <T>(T item) where T : class { using (var context = new AppsEntities()) { context.Set <T>().Add(item); await context.SaveChangesAsync(); } }
public async Task SaveModifiedEntityAsync <T>(T item) where T : class { using (var context = new AppsEntities()) { context.Entry <T>(item).State = EntityState.Modified; await context.SaveChangesAsync(); } }
public async Task DeleteByIdsAsync <T>(IEnumerable <Int32> ids) where T : class, IEntity { using (var context = new AppsEntities()) { var items = context.Set <T>().Where(e => ids.Contains(e.ID)); context.Set <T>().RemoveRange(items); await context.SaveChangesAsync(); } }
public async Task <int> DeleteOldScreenshotsAsync(int daysBackwards) { using (var context = new AppsEntities()) { DateTime dateTreshold = DateTime.Now.AddDays(-1d * daysBackwards); var oldScreenshots = context.Screenshots.Where(s => s.Date < dateTreshold).ToList(); context.Screenshots.RemoveRange(oldScreenshots); return(await context.SaveChangesAsync()); } }
public async Task DeleteScreenshotsById(IEnumerable <Int32> ids) { using (var context = new AppsEntities()) { var screenshots = await context.Set <Screenshot>().Where(s => ids.Contains(s.ID)).ToListAsync(); context.Screenshots.RemoveRange(screenshots); await context.SaveChangesAsync(); } }
public async Task DeleteEntityRangeAsync <T>(IEnumerable <T> range) where T : class { using (var context = new AppsEntities()) { foreach (var item in range) { context.Entry <T>(item).State = EntityState.Deleted; } await context.SaveChangesAsync(); } }
public async Task SaveModifiedEntityRangeAsync <T>(IEnumerable <T> items) where T : class { using (var context = new AppsEntities()) { foreach (var item in items) { context.Entry(item).State = EntityState.Modified; } await context.SaveChangesAsync(); } }
public async Task DeleteScreenshots(IEnumerable <Screenshot> screenshots) { using (var context = new AppsEntities()) { foreach (var shot in screenshots) { context.Screenshots.Attach(shot); } context.Screenshots.RemoveRange(screenshots); await context.SaveChangesAsync(); } }
private async Task DeleteEmptyLogsAsync() { using (var context = new AppsEntities()) { var emptyWindows = await context.Windows.Where(w => w.Logs.Count == 0).ToListAsync(); foreach (var window in emptyWindows) { if (!context.Windows.Local.Any(w => w.ID == window.ID)) { context.Windows.Attach(window); } context.Windows.Remove(window); await context.SaveChangesAsync(); } var appsInCategories = await context.AppCategories.SelectMany(c => c.Applications).ToListAsync(); var appsWithLimits = await context.Applications.Where(a => a.Limits.Count > 0).ToListAsync(); foreach (var app in context.Applications.Where(a => a.Windows.Count == 0)) { if (appsInCategories.Contains(app) || appsWithLimits.Contains(app)) { continue; } if (!context.Applications.Local.Any(a => a.ID == app.ID)) { context.Applications.Attach(app); } context.Applications.Remove(app); await context.SaveChangesAsync(); } } }