GetNotificationsQueryable(ApplicationDbContext dbContext, NotificationsQuery query) { var queryable = dbContext.Notifications.AsQueryable(); if (query.Ids?.Any() is true) { queryable = queryable.Where(data => query.Ids.Contains(data.Id)); } if (!string.IsNullOrEmpty(query.UserId)) { queryable = queryable.Where(data => data.ApplicationUserId == query.UserId); } if (query.Seen.HasValue) { queryable = queryable.Where(data => data.Seen == query.Seen); } queryable = queryable.OrderByDescending(a => a.Created); var queryable2 = queryable; if (query.Skip.HasValue) { queryable2 = queryable.Skip(query.Skip.Value); } if (query.Take.HasValue) { queryable2 = queryable.Take(query.Take.Value); } return(queryable, queryable2); }
public async Task Remove(NotificationsQuery notificationsQuery) { await using var dbContext = _factory.CreateContext(); var queryables = GetNotificationsQueryable(dbContext, notificationsQuery); dbContext.RemoveRange(queryables.withPaging); await dbContext.SaveChangesAsync(); if (!string.IsNullOrEmpty(notificationsQuery.UserId)) { InvalidateNotificationCache(notificationsQuery.UserId); } }
public async Task <List <NotificationViewModel> > ToggleSeen(NotificationsQuery notificationsQuery, bool?setSeen) { await using var dbContext = _factory.CreateContext(); var queryables = GetNotificationsQueryable(dbContext, notificationsQuery); var items = await queryables.withPaging.ToListAsync(); var userIds = items.Select(data => data.ApplicationUserId).Distinct(); foreach (var notificationData in items) { notificationData.Seen = setSeen.GetValueOrDefault(!notificationData.Seen); } await dbContext.SaveChangesAsync(); InvalidateNotificationCache(userIds.ToArray()); return(items.Select(ToViewModel).ToList()); }
public async Task <(List <NotificationViewModel> Items, int Count)> GetNotifications(NotificationsQuery query) { await using var dbContext = _factory.CreateContext(); var queryables = GetNotificationsQueryable(dbContext, query); return(Items : (await queryables.withPaging.ToListAsync()).Select(ToViewModel).ToList(), Count : await queryables.withoutPaging.CountAsync()); }
public async Task <(List <NotificationViewModel> Items, int?Count)> GetNotifications(NotificationsQuery query) { await using var dbContext = _factory.CreateContext(); var queryables = GetNotificationsQueryable(dbContext, query); var items = (await queryables.withPaging.ToListAsync()).Select(ToViewModel).Where(model => model != null).ToList(); int?count = null; if (query.Seen is false) { // Unseen notifications aren't likely to be too huge, so count should be fast count = await queryables.withoutPaging.CountAsync(); } return(Items : items, Count : count); }