Пример #1
0
        public ActionResult WriteMessage()
        {
            WriteMessageModel model = new WriteMessageModel {
                Sender = userManager.Users.FirstOrDefault(x => x.UserName == CurrentUser.Name)
            };

            return(View(model));
        }
Пример #2
0
        public ActionResult WriteMessage(WriteMessageModel model)
        {
            if (ModelState.IsValid)
            {
                AppUser currentUser = userManager.Users.FirstOrDefault(x => x.UserName == CurrentUser.Name);
                AppUser recipient   = userManager.Users.FirstOrDefault(x => x.Id == model.RecipientId);

                ChatModel chat          = currentUser.Dialogs.FirstOrDefault(x => x.Users.Last() == recipient);
                ChatModel chatRecipient = recipient.Dialogs.FirstOrDefault(x => x.Users.Last() == currentUser);
                if (chat == null && chatRecipient == null)
                {
                    chat = new ChatModel
                    {
                        chatID   = GetChatId(),
                        ChatName = recipient.Name + " " + recipient.UserSurname
                    };
                    chat.Users = new List <AppUser>();
                    chat.Users.Add(currentUser);
                    chat.Users.Add(recipient);
                    chat.Messages = new List <ChatMessage>();
                    chat.Messages.Add(new ChatMessage {
                        messageID = GetMessageId(), Date = DateTime.Now, User = currentUser, Text = model.Text
                    });

                    chatRecipient = new ChatModel
                    {
                        chatID   = GetChatId(),
                        ChatName = currentUser.Name + " " + currentUser.UserSurname
                    };
                    chatRecipient.Users = new List <AppUser>();
                    chatRecipient.Users.Add(recipient);
                    chatRecipient.Users.Add(currentUser);
                    chatRecipient.Messages = new List <ChatMessage>();
                    chatRecipient.Messages.Add(new ChatMessage {
                        messageID = GetMessageId(), Date = DateTime.Now, User = currentUser, Text = model.Text
                    });

                    currentUser.Dialogs.Add(chat);
                    recipient.Dialogs.Add(chatRecipient);
                }
                else if (chat == null && chatRecipient != null)
                {
                    chat = new ChatModel
                    {
                        chatID   = GetChatId(),
                        ChatName = recipient.Name + " " + recipient.UserSurname
                    };
                    chat.Users = new List <AppUser>();
                    chat.Users.Add(currentUser);
                    chat.Users.Add(recipient);
                    chat.Messages = new List <ChatMessage>();
                    chat.Messages.Add(new ChatMessage {
                        messageID = GetMessageId(), Date = DateTime.Now, User = currentUser, Text = model.Text
                    });

                    recipient.Dialogs.FirstOrDefault(x => x.Users.Last() == currentUser).Messages.Add(
                        new ChatMessage
                    {
                        messageID = GetMessageId(),
                        Date      = DateTime.Now,
                        User      = currentUser,
                        Text      = model.Text
                    }
                        );

                    currentUser.Dialogs.Add(chat);
                }
                else if (chat != null && chatRecipient == null)
                {
                    currentUser.Dialogs.FirstOrDefault(x => x.Users.Last() == recipient).Messages.Add(
                        new ChatMessage
                    {
                        messageID = GetMessageId(),
                        Date      = DateTime.Now,
                        User      = currentUser,
                        Text      = model.Text
                    }
                        );

                    chatRecipient = new ChatModel
                    {
                        chatID   = GetChatId(),
                        ChatName = currentUser.Name + " " + currentUser.UserSurname
                    };
                    chatRecipient.Users = new List <AppUser>();
                    chatRecipient.Users.Add(recipient);
                    chatRecipient.Users.Add(currentUser);
                    chatRecipient.Messages = new List <ChatMessage>();
                    chatRecipient.Messages.Add(new ChatMessage {
                        messageID = GetMessageId(), Date = DateTime.Now, User = currentUser, Text = model.Text
                    });

                    recipient.Dialogs.Add(chatRecipient);
                }
                else
                {
                    currentUser.Dialogs.FirstOrDefault(x => x.Users.Last() == recipient).Messages.Add(
                        new ChatMessage
                    {
                        messageID = GetMessageId(),
                        Date      = DateTime.Now,
                        User      = currentUser,
                        Text      = model.Text
                    }
                        );

                    recipient.Dialogs.FirstOrDefault(x => x.Users.Last() == currentUser).Messages.Add(
                        new ChatMessage
                    {
                        messageID = GetMessageId(),
                        Date      = DateTime.Now,
                        User      = currentUser,
                        Text      = model.Text
                    }
                        );
                }

                userManager.Update(currentUser);
                userManager.Update(recipient);

                return(RedirectToAction("Messages"));
            }
            else
            {
                ModelState.AddModelError("", "");
                return(View(model));
            }
        }