public JsonResult DeleteChat(string fileName) { List <UserMessage> listMessages = new List <UserMessage>(); WriteReadJsonFile.WriteList(listMessages, fileName); return(Json(listMessages, JsonRequestBehavior.AllowGet)); }
public JsonResult GetMessages(string fileName, int userToId) { if (Session["Id"] != null && Session["UserRank"].ToString() == "User") { int userFromId = Convert.ToInt32(Session["Id"]); List <Message> listMessages = new List <Message>(); User userFrom = _context.Users.FirstOrDefault(u => u.Id == userFromId); User userTo = _context.Users.FirstOrDefault(u => u.Id == userToId); string fileName1 = userFrom.Email.Substring(0, userFrom.Email.IndexOf("@")) + userTo.Email.Substring(0, userTo.Email.IndexOf("@")) + ".json"; string fileName2 = userTo.Email.Substring(0, userTo.Email.IndexOf("@")) + userFrom.Email.Substring(0, userFrom.Email.IndexOf("@")) + ".json"; if (fileName1 == fileName) { WriteReadJsonFile.ReadList(ref listMessages, fileName); if (listMessages != null) { foreach (var m in listMessages) { m.UserFrom = _context.Users.Single(u => u.Id == m.UserFromId); m.UserTo = _context.Users.Single(u => u.Id == m.UserToId); if (m.UserToId == userFromId) { m.ReadMessage = true; } } WriteReadJsonFile.WriteList(listMessages, fileName1, fileName2); } return(Json(listMessages, JsonRequestBehavior.AllowGet)); } return(Json("", JsonRequestBehavior.AllowGet)); } return (Json("", JsonRequestBehavior.AllowGet)); }
public ActionResult SendMessage(UserMessage userMessage) { List <UserMessage> listMessages = new List <UserMessage>(); userMessage.DateTimeSend = DateTime.Now; userMessage.ReadMessage = false; var userFrom = _context.Users.Single(u => u.Id == userMessage.UserFromId); var userTo = _context.Users.Single(u => u.Id == userMessage.UserToId); string fileName = ""; if (userFrom.UserRank == "Admin" && userTo.UserRank == "Customer") { fileName = userFrom.Username + userTo.Username + ".json"; } else if (userFrom.UserRank == "Customer" && userTo.UserRank == "Admin") { fileName = userTo.Username + userFrom.Username + ".json"; } WriteReadJsonFile.ReadList <UserMessage>(ref listMessages, fileName); if (listMessages == null) { listMessages = new List <UserMessage>(); } listMessages.Add(userMessage); WriteReadJsonFile.WriteList <UserMessage>(listMessages, fileName); if (listMessages != null) { foreach (var m in listMessages) { m.UserFrom = _context.Users.Single(u => u.Id == m.UserFromId); m.UserTo = _context.Users.Single(u => u.Id == m.UserToId); } } var viewModel = new NewChatViewModel { UserMessage = new UserMessage { UserFrom = userFrom, UserTo = userTo, UserToId = userTo.Id, UserFromId = userFrom.Id }, UserMessages = listMessages, Filename = fileName }; return(View("UserChat", viewModel)); }
public ActionResult ChatPage(int userToId) { if (Session["Id"] != null && Session["UserRank"].ToString() == "User") { int userFromId = Convert.ToInt32(Session["Id"]); if (userFromId == userToId) { return(HttpNotFound()); } User userFrom = _context.Users.FirstOrDefault(u => u.Id == userFromId); User userTo = _context.Users.FirstOrDefault(u => u.Id == userToId); string fileName = userFrom.Email.Substring(0, userFrom.Email.IndexOf("@")) + userTo.Email.Substring(0, userTo.Email.IndexOf("@")) + ".json"; List <Message> listMessages = new List <Message>(); WriteReadJsonFile.ReadList <Message>(ref listMessages, fileName); if (listMessages != null) { foreach (var m in listMessages) { m.UserFrom = _context.Users.Single(u => u.Id == m.UserFromId); m.UserTo = _context.Users.Single(u => u.Id == m.UserToId); if (m.UserToId == userFromId) { m.ReadMessage = true; } } } string fileName1 = fileName; string fileName2 = userTo.Email.Substring(0, userTo.Email.IndexOf("@")) + userFrom.Email.Substring(0, userFrom.Email.IndexOf("@")) + ".json"; WriteReadJsonFile.WriteList(listMessages, fileName1, fileName2); var viewModel = new NewRandomViewModel { Message = new Message { UserFromId = userFromId, UserFrom = userFrom, UserToId = userToId, UserTo = userTo }, UserMessages = listMessages, Filename = fileName }; return(View(viewModel)); } return(HttpNotFound()); }
public ActionResult SendMessage(Message userMessage) { if (Session["Id"] != null && Session["UserRank"].ToString() == "User") { List <Message> listMessages = new List <Message>(); userMessage.DateTimeSend = DateTime.Now; userMessage.ReadMessage = false; var userFrom = _context.Users.Single(u => u.Id == userMessage.UserFromId); var userTo = _context.Users.Single(u => u.Id == userMessage.UserToId); string fileName1 = userFrom.Email.Substring(0, userFrom.Email.IndexOf("@")) + userTo.Email.Substring(0, userTo.Email.IndexOf("@")) + ".json"; string fileName2 = userTo.Email.Substring(0, userTo.Email.IndexOf("@")) + userFrom.Email.Substring(0, userFrom.Email.IndexOf("@")) + ".json";; WriteReadJsonFile.ReadList <Message>(ref listMessages, fileName1); if (listMessages == null) { listMessages = new List <Message>(); } listMessages.Add(userMessage); WriteReadJsonFile.WriteList <Message>(listMessages, fileName1, fileName2); if (listMessages != null) { foreach (var m in listMessages) { m.UserFrom = _context.Users.Single(u => u.Id == m.UserFromId); m.UserTo = _context.Users.Single(u => u.Id == m.UserToId); } } var viewModel = new NewRandomViewModel { Message = new Message { UserFrom = userFrom, UserTo = userTo, UserToId = userTo.Id, UserFromId = userFrom.Id }, UserMessages = listMessages, Filename = fileName1 }; return(View("ChatPage", viewModel)); } return(HttpNotFound()); }
public ActionResult UserChat(int id) { int userId = Convert.ToInt32(Session["Id"]); var userFrom = _context.Users.Single(u => u.Id == userId); var userTo = _context.Users.Single(u => u.Id == id); List <UserMessage> listMessages = new List <UserMessage>(); if (userFrom.UserRank == "Admin" && userTo.UserRank == "Customer") { string fileName = userFrom.Username + userTo.Username + ".json"; WriteReadJsonFile.ReadList <UserMessage>(ref listMessages, fileName); if (listMessages != null) { foreach (var m in listMessages) { m.UserFrom = _context.Users.Single(u => u.Id == m.UserFromId); m.UserTo = _context.Users.Single(u => u.Id == m.UserToId); if (m.UserToId == userId) { m.ReadMessage = true; } } } WriteReadJsonFile.WriteList(listMessages, fileName); var viewModel = new NewChatViewModel { UserMessage = new UserMessage { UserTo = userTo, UserFrom = userFrom, UserToId = userTo.Id, UserFromId = userFrom.Id }, UserMessages = listMessages, Filename = fileName }; return(View(viewModel)); } else if (userFrom.UserRank == "Customer" && userTo.UserRank == "Admin") { string fileName = userTo.Username + userFrom.Username + ".json"; WriteReadJsonFile.ReadList <UserMessage>(ref listMessages, fileName); if (listMessages != null) { foreach (var m in listMessages) { m.UserFrom = _context.Users.Single(u => u.Id == m.UserFromId); m.UserTo = _context.Users.Single(u => u.Id == m.UserToId); if (m.UserToId == userId) { m.ReadMessage = true; } } } WriteReadJsonFile.WriteList(listMessages, fileName); var viewModel = new NewChatViewModel { UserMessage = new UserMessage { UserTo = userTo, UserFrom = userFrom, UserToId = userTo.Id, UserFromId = userFrom.Id }, UserMessages = listMessages, Filename = fileName }; return(View(viewModel)); } return(HttpNotFound()); }