public async Task NewGroupMessage(string userId, int chatId, string content, string msgType = "")
        {
            AppUser sender = userManager.Users
                             .FirstOrDefault(u => u.Id == userId);
            string senderPhoto = (sender.ProfilePhotoUrl != null) ?
                                 $"/UsersData/{sender.ProfilePhotoUrl}" : "/defaultAvatar.png";
            string senderName = (sender.FirstName == null && sender.LastName == null) ?
                                sender.UserName : $"{sender.FirstName} {sender.LastName}";

            GroupChat groupChat = repository.GroupChats.FirstOrDefault(c => c.ChatId == chatId);
            string    chatPhoto = (groupChat.ChatPhotoPath != null) ?
                                  $"/UsersData/{groupChat.ChatPhotoPath}" : "/defaultGroup.png";

            if (groupChat != null)
            {
                GroupChatMessage newMsg = new GroupChatMessage
                {
                    GroupChatId    = groupChat.ChatId,
                    UserId         = sender.Id,
                    MessageContent = content,
                    MessageType    = msgType
                };

                repository.NewGroupChatMessage(newMsg, groupChat);

                await Clients.All.SendAsync("PostNewGroupMessage", newMsg.MessageId, groupChat.ChatId,
                                            sender.Id, senderName, DateTime.Now.ToShortTimeString(), content, senderPhoto, msgType);

                foreach (GroupChatUsers chatUser in repository.GroupChatUsers
                         .Where(g => g.GroupChatId == groupChat.ChatId))
                {
                    int userUnseenMsgs = 0;
                    foreach (GroupChatMessage msg in repository.GroupChatMessages
                             .Where(m => m.GroupChatId == groupChat.ChatId))
                    {
                        if (msg.UserId != chatUser.UserId)
                        {
                            if (repository.GroupMessageViews.Any(m => m.MessageId == msg.MessageId &&
                                                                 m.UserId == chatUser.UserId) == false)
                            {
                                userUnseenMsgs++;
                            }
                        }
                    }

                    await Clients.All.SendAsync("UserHeaderNewGroupMsg", sender.Id, chatUser.UserId,
                                                groupChat.ChatId, senderName, DateTime.Now.ToShortTimeString(), content,
                                                userUnseenMsgs, chatPhoto, groupChat.Title);

                    await Clients.All.SendAsync("ChatIndexNewGroupMsg", sender.Id, chatUser.UserId,
                                                groupChat.ChatId, senderName, DateTime.Now.ToShortTimeString(), content,
                                                userUnseenMsgs, chatPhoto, groupChat.Title);
                }
            }
        }
        public async Task <IActionResult> NewGroupStep3(NewGroupStep3ViewModel model)
        {
            ViewData["Id"] = model.UserId;

            AppUser user = await userManager.FindByIdAsync(model.UserId);

            if (user != null)
            {
                //group
                Group group = repository.Groups
                              .FirstOrDefault(g => g.GroupId == model.GroupId);

                if (model.CoursesIdString != null)
                {
                    //list of users id
                    string courseIdList = model.CoursesIdString.Trim();
                    while (courseIdList.Trim().Length > 0)
                    {
                        int index = courseIdList.IndexOf(";");
                        if (index != -1)
                        {
                            string newCourseId = courseIdList.Substring(0, index);

                            int newCourseIdInt = Int32.Parse(newCourseId.Trim());

                            repository.AddCourseToGroup(newCourseIdInt, group.GroupId);

                            courseIdList = courseIdList.Replace(newCourseId + ";", "");
                        }
                    }
                }

                //send msg
                if (group.GroupChatId != 0)
                {
                    GroupChat groupChat = repository.GroupChats
                                          .FirstOrDefault(g => g.ChatId == group.GroupChatId);

                    string userName = (user.FirstName == null && user.LastName == null) ?
                                      user.UserName : user.FirstName + user.LastName;
                    string           content = $"{userName} created a group chat";
                    GroupChatMessage newMsg  = new GroupChatMessage
                    {
                        GroupChatId    = group.GroupChatId,
                        UserId         = user.Id,
                        MessageContent = content,
                        MessageType    = "SystemMsg"
                    };

                    repository.NewGroupChatMessage(newMsg, groupChat);
                }

                //new notification
                if (group.Status == "Public")
                {
                    repository.NewNotification(user.Id, "AddUserToGroup", "Group", group.GroupId);
                }

                return(RedirectToAction("GroupDetails", new
                {
                    id = group.CreatedBy.Id,
                    groupId = group.GroupId
                }));
            }

            return(RedirectToAction("Error", "UserNotFound"));
        }