コード例 #1
0
        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"));
        }