public IActionResult Chat(int?id) { if (User.Identity.IsAuthenticated && User.IsInRole("moderator")) { if (id == null || !_context.Orders.Any(o => o.Id == id)) { return(PartialView("ErrorPage")); } if (!_context.Chats.Any(c => c.OrderId == id)) { Chat newChat = new Chat { OrderId = (int)id, To = _context.Orders.Where(o => o.Id == id).Include(o => o.Advertisement).FirstOrDefault().Advertisement.ApplicationUserId, }; _context.Chats.Add(newChat); _context.SaveChanges(); } ViewBag.Moderator = true; ChatModeratorVM vM = new ChatModeratorVM { ApplicationUser = _context.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault(), ChatMessages = _context.ChatMessages.Where(cm => cm.Chat.OrderId == id).Include(cm => cm.Chat).Include(cm => cm.Message).Include(cm => cm.Chat.Order).OrderBy(cm => cm.Message.Date) }; return(View(vM)); } ViewBag.IsHeaderNonVisible = true; return(Redirect("/account/login")); }
public IActionResult Chat(int?id) { if (User.Identity.IsAuthenticated && User.IsInRole("member")) { if (id == null || !_context.Orders.Any(o => o.Id == id && o.Chat.To == _context.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault().Id)) { return(PartialView("ErrorPage")); } ChatModeratorVM vM = new ChatModeratorVM { ApplicationUser = _context.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault(), ChatMessages = _context.ChatMessages.Where(cm => cm.Chat.OrderId == id && cm.Chat.To == _context.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault().Id).Include(cm => cm.Chat).Include(cm => cm.Message).Include(cm => cm.Chat.Order).OrderBy(cm => cm.Message.Date) }; return(View(vM)); } ViewBag.IsHeaderNonVisible = true; return(Redirect("/account/login")); }