Exemple #1
0
        public ActionResult Add(AddChatModel model)
        {
            Account account = GetAccount();

            model.Contacts = contactRepository.GetAllContactsForAccount(account.AccountId);

            if (ModelState.IsValid)                 // Check for model errors
            {
                if (model.SelectedContact.HasValue) // Check if the selected contact has an account (otherwise you can't chat)
                {
                    int receiverId = (int)model.SelectedContact;
                    int senderId   = account.AccountId;

                    if (!chatRepository.CheckForDuplicateChats(receiverId, senderId)) // Check if the chat doesn't exist yet (no duplicate chats)
                    {
                        Chat chat = new Chat();
                        chat.ReceiverId = receiverId;
                        chat.SenderId   = senderId;

                        chatRepository.AddChat(chat);                               // Add chat to DB
                        chatLogic.AddMessage(model.InitialMessage, senderId, chat); // Add initial message

                        return(RedirectToAction("ViewChat", "Chat", new { chatId = chat.ChatId }));
                    }
                    else
                    {
                        ModelState.AddModelError("", "You already have a chat with this contact. Try chatting in your existing chatroom.");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The selected contact doesn't have an account yet. Try another contact!");
                }
            }
            return(View(model));
        }