Exemplo n.º 1
0
        public ActionResult Add()
        {
            Account account = GetAccount();
            IEnumerable <Contact> contacts = contactRepository.GetAllContactsForAccount(account.AccountId);

            // All available contacts to chat with are loaded in a dropdownlist:
            AddChatModel model = new AddChatModel();

            model.Contacts = contacts;

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <Result> AddChat(AddChatModel model)
        {
            var user = await _userManager.FindByEmailAsync(User.Identity.Name);

            var chat = new Chat()
            {
                Name        = user.GetCompany(ctx).Name + " - " + ctx.Services.Single(s => s.Id == model.ServiceId).Name,
                Description = "—",
                ServiceId   = model.ServiceId,
                CompanyId   = user.GetCompany(ctx).Id
            };

            ctx.Chats.Add(chat);
            ctx.SaveChanges();
            ctx.ChatUsers.Add(
                new ChatUser()
            {
                UserId = user.Id,
                ChatId = chat.Id
            }
                );
            ctx.SaveChanges();
            ctx.Messages.Add(
                new Message()
            {
                Date   = DateTime.Now,
                ChatId = chat.Id,
                Text   = "Conversation was started",
            }
                );
            ctx.SaveChanges();
            return(new Result()
            {
                Successful = true
            });
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
 public AddChatViewModel()
 {
     addChatModel    = new AddChatModel();
     checkCommand    = new RelayCommand(checkExecuteMethod);
     completeCommand = new RelayCommand(completeExecuteMethod);
 }