public async Task <IActionResult> Apply(ApplyFormInputModel input)
        {
            if (!ModelState.IsValid)
            {
                return(this.Redirect(this.Request.GetDisplayUrl()));
            }
            var chatId = await this.chatService.CreateChatAsync(input);

            return(this.RedirectToAction("ChatById", "Chat", new { id = chatId }));
        }
Exemple #2
0
        public async Task <int> CreateChatAsync(ApplyFormInputModel input)
        {
            var groupName      = this.Hash(input.TaskCreatorUsername + input.OthersideUsername + input.About);
            var existingChatId = this.chatRepo.AllAsNoTracking()
                                 .Where(c => c.ChatGroupName == groupName)
                                 .Select(c => c.Id)
                                 .FirstOrDefault();

            if (existingChatId != 0)
            {
                return(existingChatId);
            }

            var chat = new Chat
            {
                HelpTaskId          = input.HelpTaskId,
                About               = input.About,
                TaskCreatorUsername = input.TaskCreatorUsername,
                OthersideUsername   = input.OthersideUsername,
                ChatGroupName       = groupName,
            };

            chat.Messages.Add(new Message
            {
                SenderUsername = chat.OthersideUsername,
                Text           = input.MessageText,
            });
            await this.chatRepo.AddAsync(chat);

            await this.chatRepo.SaveChangesAsync();

            chat = this.chatRepo.AllAsNoTracking().OrderBy(x => x.CreatedOn).LastOrDefault();

            var bothUsersInChat = this.userRepo.All()
                                  .Where(x => x.UserName == input.OthersideUsername || x.UserName == input.TaskCreatorUsername)
                                  .ToList();

            foreach (var user in bothUsersInChat)
            {
                var userChat = new UserChat
                {
                    UserId = user.Id,
                    ChatId = chat.Id,
                };
                chat.Users.Add(userChat);
                user.Chats.Add(userChat);
            }
            await this.userRepo.SaveChangesAsync();

            await this.chatRepo.SaveChangesAsync();

            return(chat.Id);
        }
        public IActionResult Apply(int id)
        {
            var titleAndCreatorUsernameDTO = this.helpTaskService.GetTitleAndCreatorUsernameById(id);
            var viewModel = new ApplyFormInputModel
            {
                HelpTaskId          = id,
                About               = titleAndCreatorUsernameDTO.Title,
                TaskCreatorUsername = titleAndCreatorUsernameDTO.Username,
                OthersideUsername   = this.User.Identity.Name,
            };

            return(this.View(viewModel));
        }