Exemplo n.º 1
0
        public async Task <IActionResult> AddMessage(ForumViewModel forumViewModel, int chatRoomID)
        {
            // forum view model will be used to bind data to msg viewmodel
            if (ModelState.IsValid)
            {
                var selectedChatRoom = chatRepo.ChatRoomList
                                       .Find(chat => chat.ChatRoomID == chatRoomID);
                var user = await userManager.GetUserAsync(HttpContext.User);

                var newMsg = new Message()
                {
                    Topic          = selectedChatRoom.ChatName,
                    MessageTitle   = forumViewModel.MsgViewModel.Title,
                    MessageContent = forumViewModel.MsgViewModel.MessageBody,
                    UnixTimeStamp  = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
                    Poster         = user.UserName
                };
                // add to msg, chat, and user repos
                await messageRepo.AddMsgToRepo(newMsg);

                user.AddMessageToHistory(newMsg);
                await chatRepo.AddMsgToChat(chatRoomID, newMsg);

                var result = await userManager.UpdateAsync(user);

                return(RedirectToAction("Forum", new { id = chatRoomID }));
            }
            else
            {
                ModelState.AddModelError(nameof(ForumViewModel.MsgViewModel.Title), "Invalid title or body");
                return(RedirectToAction("Forum", new {
                    chatRoomID = chatRoomID,
                    forumViewModel = forumViewModel
                }));
            }
        }