public static async Task <bool> SendNotiRole(ApplicationDbContext db, string UserId, DateTime?date = null) { if (date == null) { date = DateTime.Now; } var prev_noti_role = await db.NotiRole.FirstOrDefaultAsync(n => n.UserID == UserId); if (prev_noti_role != null) { return(false); } var user = await db.User.FirstOrDefaultAsync(u => u.Id == UserId); if (user == null) { return(false); } var noti_role = new NotiRole() { Message = "The Customer " + user.Name + " wants to become in to a seller", NotiDate = (DateTime)date, Seen = false, UserID = UserId, User = user, SendToUser = "******", }; await db.NotiRole.AddAsync(noti_role); await db.SaveChangesAsync(); return(true); }
public async Task <IActionResult> Index(string noti_type) { var claimsIdentity = (ClaimsIdentity)this.User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); bool is_admin = this.User.IsInRole(SD.ManagerUser); NotificationViewModel nvm = new NotificationViewModel(); if (!this.User.Identity.IsAuthenticated) { return(NotFound()); } if (noti_type == "NotiRole" || noti_type == null) { var notifications = await _db.NotiRole .Where(n => n.SendToUser == claim.Value || (n.SendToUser == "All_A" && is_admin) ) .Include(nr => nr.User) .ToListAsync(); foreach (var n in notifications) { NotiRole nr = new NotiRole() { Id = n.Id, Message = n.Message, User = n.User, UserID = n.UserID, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser, }; nvm.NotiRole.Add(nr); n.Seen = true; } _db.UpdateRange(notifications); } if (noti_type == "NotiBuy" || noti_type == null) { var notifications = await _db.NotiBuy .Where(n => n.SendToUser == claim.Value ) .Include(n => n.OrderHeader) .Include(n => n.OrderHeader.User) .ToListAsync(); foreach (var n in notifications) { n.OrderDetails = await _db.OrderDetails.Where(o => o.OrderId == n.OrderHeaderID).ToListAsync(); NotiBuy nr = new NotiBuy() { Id = n.Id, Message = n.Message, OrderHeaderID = n.OrderHeaderID, OrderHeader = n.OrderHeader, OrderDetails = n.OrderDetails, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser, }; nvm.NotiBuy.Add(nr); n.Seen = true; } _db.UpdateRange(notifications); } if (noti_type == "NotiSell" || noti_type == null) { var notifications = await _db.NotiSell .Where(n => n.SendToUser == claim.Value ) .Include(n => n.OrderDetails) .ToListAsync(); foreach (var n in notifications) { NotiSell ns = new NotiSell() { Id = n.Id, Message = n.Message, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser, OrderDetails = n.OrderDetails, }; nvm.NotiSell.Add(ns); n.Seen = true; } _db.UpdateRange(notifications); } if (noti_type == "NotiGeneral" || noti_type == null) { var notifications = await _db.Notification.OfType <Notification>().Where(n => (n.SendToUser == claim.Value)).ToListAsync(); foreach (var n in notifications) { if (n.GetType() == typeof(Notification)) { Notification not = new Notification() { Id = n.Id, Message = n.Message, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser }; nvm.NotiGeneral.Add(not); n.Seen = true; } } _db.UpdateRange(notifications); } if (noti_type == "NotiAuction" || noti_type == null) { var notifications = await _db.NotiAuction .Where(n => n.SendToUser == claim.Value ) .Include(n => n.AuctionHeader) .ToListAsync(); foreach (var n in notifications) { var products = await _db.AuctionProduct.Where(a => a.AuctionId == n.AuctionHeaderID).Include(a => a.Product).ToListAsync(); NotiAuction na = new NotiAuction() { Id = n.Id, Message = n.Message, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser, AuctionHeader = n.AuctionHeader, AuctionHeaderID = n.AuctionHeaderID, AuctionProduct = products, }; var auction = await _db.AuctionHeader.FirstOrDefaultAsync(a => a.Id == n.AuctionHeaderID); string status = SD.ActiveStatus; if (DateTime.Compare(DateTime.Now, auction.EndDate) >= 0) { status = SD.PastStatus; } na.Status = status; nvm.NotiAuction.Add(na); n.Seen = true; } _db.UpdateRange(notifications); } await _db.SaveChangesAsync(); nvm.Type = noti_type; return(View(nvm)); }