public IActionResult AddUsers(NewGroupStep2ViewModel model) { //group Group group = repository.Groups .FirstOrDefault(g => g.GroupId == model.GroupId); //Created by AppUser user = userManager.Users.FirstOrDefault(u => u.Id == group.CreatedBy.Id); if (model.UsersIdString != null) { //list of users id string userIdList = model.UsersIdString.Trim(); while (userIdList.Trim().Length > 0) { int index = userIdList.IndexOf(";"); if (index != -1) { string newUserId = userIdList.Substring(0, index); repository.AddUserToGroup(newUserId, group.GroupId); //notification if (group.Status == "Public") { repository.NewNotification(group.CreatedBy.Id, "AddUserToGroup", "Group", group.GroupId, newUserId); } //group chat exists if (group.GroupChatId != 0) { repository.AddUserToGroupChat(group.GroupChatId, newUserId); GroupChat groupChat = repository.GroupChats .FirstOrDefault(g => g.ChatId == group.GroupChatId); AppUser addedUser = userManager.Users.FirstOrDefault(u => u.Id == newUserId); string userName = (user.FirstName == null && user.LastName == null) ? user.UserName : user.FirstName + user.LastName; string content = $"{userName} added {addedUser}"; GroupChatMessage newMsg = new GroupChatMessage { GroupChatId = group.GroupChatId, UserId = user.Id, MessageContent = content, MessageType = "SystemMsg" }; repository.NewGroupChatMessage(newMsg, groupChat); } userIdList = userIdList.Replace(newUserId + ";", ""); } } } return(RedirectToAction("GroupDetails", new { id = group.CreatedBy.Id, groupId = group.GroupId })); }
public async Task <IActionResult> NewGroupStep2(NewGroupStep2ViewModel 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.UsersIdString != null) { //list of users id string userIdList = model.UsersIdString.Trim(); while (userIdList.Trim().Length > 0) { int index = userIdList.IndexOf(";"); if (index != -1) { string newUserId = userIdList.Substring(0, index); repository.AddUserToGroup(newUserId, group.GroupId); //group chat exists if (group.GroupChatId != 0) { repository.AddUserToGroupChat(group.GroupChatId, newUserId); } userIdList = userIdList.Replace(newUserId + ";", ""); } } return(RedirectToAction("NewGroupStep3", new { id = model.UserId, groupId = group.GroupId })); } else { return(RedirectToAction("NewGroupStep3", new { id = model.UserId, groupId = group.GroupId })); } } return(RedirectToAction("Error", "UserNotFound")); }