public IActionResult NewGroupChatStep2(NewGroupChatStep2ViewModel model)
        {
            if (model.UsersIds == null || model.UsersIds.Trim() == "")
            {
                return(RedirectToAction("AbortNewGroupChat", new
                {
                    id = model.UserId,
                    groupChatId = model.GroupChatId
                }));
            }
            else
            {
                repository.AddUserToGroupChat(model.GroupChatId, model.UserId);
                //list of users id
                string userIdList = model.UsersIds.Trim();
                while (userIdList.Trim().Length > 0)
                {
                    int index = userIdList.IndexOf(";");
                    if (index != -1)
                    {
                        string newUserId = userIdList.Substring(0, index);

                        repository.AddUserToGroupChat(model.GroupChatId, newUserId);

                        userIdList = userIdList.Replace(newUserId + ";", "");
                    }
                }

                return(RedirectToAction("GroupChat", new
                {
                    id = model.UserId,
                    groupChatId = model.GroupChatId
                }));
            }
        }
        public async Task <IActionResult> NewGroup(NewGroupViewModel model)
        {
            ViewData["Id"] = model.CreatedById;

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

            if (user != null)
            {
                string status = (model.Status == true) ? "Public" : "Private";

                Group newGroup = new Group
                {
                    Name        = model.Name,
                    Description = model.Description,
                    CreatedBy   = user,
                    CompanyName = user.CompanyName,
                    Status      = status
                };

                int groupId = repository.SaveGroup(newGroup);
                repository.AddUserToGroup(user.Id, groupId);

                //if photo exists
                if (model.ProfilePhoto != null)
                {
                    AddGroupPhoto(groupId, model.ProfilePhoto);
                }

                //create chat
                if (model.CreateChat == true)
                {
                    GroupChat groupChat = new GroupChat
                    {
                        Title         = model.Name,
                        Status        = status,
                        CreatedBy     = user.Id,
                        ChatPhotoPath = "Groups/" + repository.Groups
                                        .FirstOrDefault(g => g.GroupId == newGroup.GroupId).GroupPhotoPath
                    };
                    repository.EditGroupChat(groupChat);

                    //add admin to chat
                    repository.AddUserToGroupChat(groupChat.ChatId, user.Id);

                    newGroup.GroupChatId = groupChat.ChatId;
                    repository.SaveGroup(newGroup);
                }

                return(RedirectToAction("NewGroupStep2", new { id = model.CreatedById, groupId }));
            }

            return(RedirectToAction("Error", "UserNotFound"));
        }
        public async Task GroupChatNewUsers(string currentUserId, int chatId, string userIds)
        {
            if (userIds != null && userIds.Trim() != "")
            {
                string userIdList = userIds.Trim();
                while (userIdList.Trim().Length > 0)
                {
                    int index = userIdList.IndexOf(";");
                    if (index != -1)
                    {
                        string newUserId = userIdList.Substring(0, index);

                        repository.AddUserToGroupChat(chatId, newUserId);

                        userIdList = userIdList.Replace(newUserId + ";", "");

                        //create system msg
                        AppUser admin     = userManager.Users.FirstOrDefault(u => u.Id == currentUserId);
                        string  adminName = (admin.FirstName == null || admin.LastName == null) ?
                                            admin.UserName : $"{admin.FirstName} {admin.LastName}";
                        AppUser user     = userManager.Users.FirstOrDefault(u => u.Id == newUserId);
                        string  userName = (user.FirstName == null && user.LastName == null) ?
                                           user.UserName : $"{user.FirstName} {user.LastName}";
                        string userPhoto = (user.ProfilePhotoUrl != null) ?
                                           $"/UsersData/{user.ProfilePhotoUrl}" : "/defaultAvatar.png";
                        string msgContent = $"{admin} added {userName}";
                        await NewGroupMessage(currentUserId, chatId, msgContent, "SystemMsg");

                        await Clients.All.SendAsync("AddedUsersList", chatId, user.Id, userName, userPhoto);
                    }
                }
            }
        }