public Task <JsonResponse> ChangeSeen(int userID, int NotiID) { return(Task.Run(() => { using (MIUEntities db = new MIUEntities()) { try { if (NotiID != 0) { NotiVisibility notiVisibility = db.NotiVisibilities.Where(x => x.NotiID == NotiID && x.UserID == userID).SingleOrDefault(); notiVisibility.IsSeen = true; db.SaveChanges(); return new JsonResponse() { Flag = true, Message = "IsSeen changed." }; } else { return new JsonResponse() { Flag = true, Message = "No Changes(Coming soon)" }; } //return true; } catch (Exception ex) { return new JsonResponse() { Flag = true, Message = ex.Message }; } } })); }
public Task <NotiInfo> GetNotificationList(int userID, int currentIndex, int maxRow) { return(Task.Run(() => { using (MIUEntities db = new MIUEntities()) { NotiInfo Notifications = new NotiInfo(); try { User user = db.Users.Where(x => x.ID == userID && x.IsDelete != true).SingleOrDefault(); Notifications.NotiCount = 0; var skip = (currentIndex - 1) * maxRow; List <Notification> NotiList = (from n in db.Notifications where n.NotiTypeId != 1 && (!db.NotiVisibilities.Any(nv => nv.NotiID == n.ID && nv.UserID == userID) || db.NotiVisibilities.Any(nv => nv.NotiID == n.ID && nv.UserID == userID)) select n).ToList(); List <Notification> SubscriptionList = (from n in db.Notifications //join nv in db.NotiVisibilities.Where(nv => nv.UserID != userID) on n.ID equals nv.NotiID into noti from x in noti.DefaultIfEmpty() join s in db.Subscribtions.Where(s => s.UserID == userID) on n.ID equals s.NotiID where (!db.NotiVisibilities.Any(nv => nv.NotiID == n.ID && nv.UserID == userID) || db.NotiVisibilities.Any(nv => nv.NotiID == n.ID && nv.UserID == userID)) && n.NotiTypeId == 1 select n).ToList(); List <NotiVisibility> notiVisibilities = new List <NotiVisibility>(); List <NotiListInfo> NotiListInfo = new List <NotiListInfo>(); foreach (var data in NotiList) { NotiListInfo info = new NotiListInfo(); PropertyCopier <Notification, NotiListInfo> .Copy(data, info); var NotiType = db.NotificationTypes.Where(x => x.ID == data.NotiTypeId).SingleOrDefault(); if (NotiType.ID == 1) { LeadNotification LeadNoti = new LeadNotification(); LeadNoti = db.LeadNotifications.Where(x => x.NotificationId == data.ID).FirstOrDefault(); if (LeadNoti != null) { info.RelatedTableID = LeadNoti.LeadId; info.RelatedTableName = "Lead"; } } else if (NotiType.ID == 2) { NewsNoti NewNoti = new NewsNoti(); NewNoti = db.NewsNotis.Where(x => x.NotiID == data.ID).FirstOrDefault(); if (NewNoti != null) { info.RelatedTableID = NewNoti.NewsID; info.RelatedTableName = "News"; } } //info.RelatedTableID = db. NotiListInfo.Add(info); //Get Notification List NotiVisibility notiVisibility = new NotiVisibility(); notiVisibilities = db.NotiVisibilities.Where(x => x.UserID == userID && x.NotiID == data.ID).ToList(); if (notiVisibilities.Count == 0) { notiVisibility.NotiID = data.ID; notiVisibility.UserID = userID; notiVisibility.IsSeen = false; db.NotiVisibilities.Add(notiVisibility); } else { foreach (var notiVis in notiVisibilities) { foreach (var NotiListing in NotiListInfo) { if (NotiListing.UserId != null && NotiListing.ID == notiVis.NotiID && NotiListing.UserId == notiVis.UserID && notiVis.IsSeen == true) { NotiListing.IsSeen = true; } else if (NotiListing.ID == notiVis.NotiID && notiVis.IsSeen == true) { NotiListing.IsSeen = true; } } } } } foreach (var data in SubscriptionList) { NotiListInfo info = new NotiListInfo(); PropertyCopier <Notification, NotiListInfo> .Copy(data, info); var NotiType = db.NotificationTypes.Where(x => x.ID == data.NotiTypeId).SingleOrDefault(); if (NotiType.ID == 1) { LeadNotification LeadNoti = db.LeadNotifications.Where(x => x.NotificationId == data.ID).FirstOrDefault(); if (LeadNoti != null) { info.RelatedTableID = LeadNoti.LeadId; info.RelatedTableName = "Lead"; } } else if (NotiType.ID == 2) { NewsNoti NewNoti = db.NewsNotis.Where(x => x.NotiID == data.ID).FirstOrDefault(); if (NewNoti != null) { info.RelatedTableID = NewNoti.NewsID; info.RelatedTableName = "News"; } } //info.RelatedTableID = db. NotiListInfo.Add(info); //Get Notification List NotiVisibility notiVisibility = new NotiVisibility(); notiVisibilities = db.NotiVisibilities.Where(x => x.UserID == userID && x.NotiID == data.ID).ToList(); if (notiVisibilities.Count == 0) { notiVisibility.NotiID = data.ID; notiVisibility.UserID = userID; notiVisibility.IsSeen = false; db.NotiVisibilities.Add(notiVisibility); } else { foreach (var notiVis in notiVisibilities) { foreach (var NotiListing in NotiListInfo) { if (NotiListing.ID == notiVis.NotiID && NotiListing.UserId == notiVis.UserID && notiVis.IsSeen == true) { NotiListing.IsSeen = true; //NotiListInfo.Add(NotiListing); } } } } } List <NotiListInfo> WebNotiList = new List <NotiListInfo>(); WebNotiList = GetWebNotification(userID); NotiListInfo.AddRange(WebNotiList); NotiListInfo.ForEach(x => { if (x.UpdatedDate == null) { x.UpdatedDate = x.CreatedDate; x.UpdatedBy = x.CreatedBy; } }); // if updated date is null set updated date value from created date. Notifications.NotiList = NotiListInfo.OrderByDescending(x => x.CreatedDate).Skip(skip).Take(maxRow).ToList(); db.SaveChanges(); //Add to IsSeenNotification Table int TotalNotiCount = db.Notifications.Where(x => x.NotiTypeId != 1).Count(); int IsSeenNotiCount = db.NotiVisibilities.Where(x => x.UserID == userID).Count(); Notifications.NotiCount = 0; } catch (System.Exception ex) { //throw ex; } return Notifications; } })); }