public async Task<IActionResult> Message(string message, string email) { MessagesModel model = new MessagesModel(); if (message != null) { ApplicationUser user = await GetCurrentUserAsync(); model.Message = message; if (User.IsInRole("Admin")) { model.User = "******"; model.sentTo = email; model.Read = false; model.Date = DateTime.Now; _context.Messages.Add(model); _context.SaveChanges(); return RedirectToAction("Index", "AdminMessages"); } else { model.User = user.Email; model.sentTo = "YR Design"; model.Read = false; model.Date = DateTime.Now; _context.Messages.Add(model); _context.SaveChanges(); return RedirectToAction("Index", "Messages"); } } else { return RedirectToAction("Index"); } }
public async Task<IActionResult> Index() { ApplicationUser user = await GetCurrentUserAsync(); MessagesModel model = new MessagesModel(); if (User.IsInRole("Admin")) { model.messageList = _context.Messages.ToList(); } else { model.messageList = _context.Messages.ToList().Where(x => x.sentTo == user.Email || x.User == user.Email); } if (model.messageList.Any()) { ViewData["LatestMessage"] = model.messageList.Last().Date; } List<MessagesModel> unreadMessagesList = _context.Messages.ToList().Where(x => x.sentTo == user.Email && x.Read == false).ToList(); if (unreadMessagesList.Any()) { for (int i = 0; i < unreadMessagesList.Count(); i++) { unreadMessagesList[i].Read = true; } } _context.SaveChanges(); return View(model); }