public ActionResult MarkItemsAsRead(string timeFrame) { IQueryable<SubscriptionPost> subscriptionPostsUnread = _dbContext.SubscriptionPosts .Where(x => x.Subscription.UserSubscriptions .Any(y => y.User.Email == WebSecurity.CurrentUserName) && !x.PostsRead.Any(y => y.UserSubscription.User.Email == WebSecurity.CurrentUserName && y.SubscriptionPost.Id == x.Id)); switch (timeFrame) { case "DAY": subscriptionPostsUnread = subscriptionPostsUnread .Where(x => x.PublishDateUTC <= EntityFunctions.AddDays(DateTime.UtcNow, -1)); break; case "WEEK": subscriptionPostsUnread = subscriptionPostsUnread .Where(x => x.PublishDateUTC <= EntityFunctions.AddDays(DateTime.UtcNow, -7)); break; case "MONTH": subscriptionPostsUnread = subscriptionPostsUnread .Where(x => x.PublishDateUTC <= EntityFunctions.AddMonths(DateTime.UtcNow, -1)); break; } foreach (var subscriptionPost in subscriptionPostsUnread) { var userSubscription = subscriptionPost.Subscription.UserSubscriptions .Single(x => x.User.Email == WebSecurity.CurrentUserName); userSubscription.UnreadItems--; var postRead = new PostRead { ReadOnUTC = DateTime.UtcNow, SubscriptionPost = subscriptionPost, UserSubscription = userSubscription }; _dbContext.PostsRead.Add(postRead); } _dbContext.SaveChanges(); return Content("MARKED"); }
public ActionResult MarkAsRead(int id) { var alreadyMarked = _dbContext.PostsRead.Any(x => x.SubscriptionPost.Id == id && x.UserSubscription.User.Email == WebSecurity.CurrentUserName); if (alreadyMarked) { return Content("ALREADY_MARKED"); } var subscriptionPost = _dbContext.SubscriptionPosts.FirstOrDefault(x => x.Id == id); var userSubscription = subscriptionPost.Subscription.UserSubscriptions .FirstOrDefault(x => x.User.Email == WebSecurity.CurrentUserName); userSubscription.UnreadItems--; var postRead = new PostRead { ReadOnUTC = DateTime.UtcNow, SubscriptionPost = subscriptionPost, UserSubscription = userSubscription, }; _dbContext.PostsRead.Add(postRead); _dbContext.SaveChanges(); return Content("MARKED"); }