Exemplo n.º 1
0
        public async Task <NotificationSummaryViewModel> GetSummaryNotifications(ClaimsPrincipal user)
        {
            var userId   = _userManager.GetUserId(user);
            var cacheKey = GetNotificationsCacheId(userId);

            return(await _memoryCache.GetOrCreateAsync(cacheKey, async entry =>
            {
                var resp = await GetNotifications(new NotificationsQuery()
                {
                    Seen = false, Skip = 0, Take = 5, UserId = userId
                });
                entry.SetAbsoluteExpiration(TimeSpan.FromMilliseconds(_cacheExpiryMs));
                var res = new NotificationSummaryViewModel()
                {
                    Last5 = resp.Items, UnseenCount = resp.Count
                };
                entry.Value = res;
                return res;
            }));
        }
Exemplo n.º 2
0
        private async Task <NotificationSummaryViewModel> FetchNotificationsFromDb(string userId)
        {
            var resp = new NotificationSummaryViewModel();

            using (var _db = _factory.CreateContext())
            {
                resp.UnseenCount = _db.Notifications
                                   .Where(a => a.ApplicationUserId == userId && !a.Seen)
                                   .Count();

                if (resp.UnseenCount > 0)
                {
                    try
                    {
                        resp.Last5 = (await _db.Notifications
                                      .Where(a => a.ApplicationUserId == userId && !a.Seen)
                                      .OrderByDescending(a => a.Created)
                                      .Take(5)
                                      .ToListAsync())
                                     .Select(a => ToViewModel(a))
                                     .ToList();
                    }
                    catch (System.IO.InvalidDataException)
                    {
                        // invalid notifications that are not pkuzipable, burn them all
                        var notif = _db.Notifications.Where(a => a.ApplicationUserId == userId);
                        _db.Notifications.RemoveRange(notif);
                        _db.SaveChanges();

                        resp.UnseenCount = 0;
                        resp.Last5       = new List <NotificationViewModel>();
                    }
                }
                else
                {
                    resp.Last5 = new List <NotificationViewModel>();
                }
            }

            return(resp);
        }