public PlayerProfile CreatePlayerProfile(string userName)
        {
            PlayerProfile playerProfile = new PlayerProfile()
            {
                Pseudo = userName.Split('@')[0]
            };

            _context.PlayerProfiles.Add(playerProfile);

            var user = _context.Users.SingleOrDefault(u => u.UserName.Equals(userName));

            user.PlayerProfile = playerProfile;

            Title       title       = _context.Titles.FirstOrDefault(t => t.Text.Equals("Noob"));
            PlayerTitle PlayerTitle = new PlayerTitle()
            {
                Player = playerProfile, Title = title, IsSelected = true
            };

            _context.PlayerTitles.Add(PlayerTitle);

            playerProfile.AvailableTitles.Add(PlayerTitle);

            Project project = GenerateDemoProject();

            _context.Add(project);

            PlayerProject playerProject = new PlayerProject()
            {
                Project = project, Player = playerProfile
            };

            _context.PlayerProjects.Add(playerProject);

            playerProfile.PlayerProjects.Add(playerProject);

            _context.SaveChanges();
            return(playerProfile);
        }
        public async Task <IActionResult> Edit(int id, ProjectCreateEditViewModel editProject)
        {
            if (id != editProject.Project.ProjectId)
            {
                return(NotFound());
            }

            ModelState.Remove("Project.User");
            ModelState.Remove("Project.UserId");
            if (ModelState.IsValid)
            {
                // Delete the joiner tables before the edit so that we can add the new instances without duplicates
                var playerProjects = await _context.PlayerProject.Where(pp => pp.ProjectId == id).ToListAsync();

                if (playerProjects != null)
                {
                    foreach (PlayerProject pp in playerProjects)
                    {
                        _context.PlayerProject.Remove(pp);
                    }
                }
                var artistProjects = await _context.ArtistProject.Where(ap => ap.ProjectId == id).ToListAsync();

                if (artistProjects != null)
                {
                    foreach (ArtistProject ap in artistProjects)
                    {
                        _context.ArtistProject.Remove(ap);
                    }
                }
                try
                {
                    editProject.Project.User = await GetCurrentUserAsync();

                    editProject.Project.UserId = editProject.Project.User.Id;
                    // Add the new joiner tables to the database if there are any
                    if (editProject.SelectedPlayers != null)
                    {
                        foreach (int playerId in editProject.SelectedPlayers)
                        {
                            PlayerProject newPP = new PlayerProject()
                            {
                                PlayerId  = playerId,
                                ProjectId = editProject.Project.ProjectId
                            };
                            _context.Add(newPP);
                        }
                    }
                    if (editProject.SelectedArtists != null)
                    {
                        foreach (int artistId in editProject.SelectedArtists)
                        {
                            ArtistProject newAP = new ArtistProject()
                            {
                                ArtistId  = artistId,
                                ProjectId = editProject.Project.ProjectId
                            };
                            _context.Add(newAP);
                        }
                    }
                    // Update the database with the new edited project
                    _context.Update(editProject.Project);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProjectExists(editProject.Project.ProjectId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("ProjectsofStatus", new { id = 3 }));
                // Add logic to get the dropdown items and multiselect items. Not Sure if I need this.
            }
            return(View(editProject));
        }
        public async Task <IActionResult> Create(ProjectCreateEditViewModel createProject)
        {
            // Must remove the User and User Id from the Project for the Model State to be valid (these may have been placeholder values)
            ModelState.Remove("Project.User");
            ModelState.Remove("Project.UserId");
            if (ModelState.IsValid)
            {
                // Add the current User and UserId to the project
                createProject.Project.User = await GetCurrentUserAsync();

                createProject.Project.UserId = createProject.Project.User.Id;
                // Add the newly created project to the database
                _context.Add(createProject.Project);
                // A foreach looping through the SelectedPlayers to create the instances of the PlayerProjects for every one in the list. Then adding them to the database. The foreach works because each entry in the list is an int representing the playerId then it takes the project Id from the created project in the model to make the PlayerProject. Needs to be in an if statement so a person can create a project and not assign players to a project. Then this same thing is done for the SelectedArtists
                if (createProject.SelectedPlayers != null)
                {
                    foreach (int playerId in createProject.SelectedPlayers)
                    {
                        PlayerProject newPP = new PlayerProject()
                        {
                            PlayerId  = playerId,
                            ProjectId = createProject.Project.ProjectId
                        };
                        // Add each individual Player Project to the database
                        _context.Add(newPP);
                    }
                }
                if (createProject.SelectedArtists != null)
                {
                    foreach (int artistId in createProject.SelectedArtists)
                    {
                        ArtistProject newAP = new ArtistProject()
                        {
                            ArtistId  = artistId,
                            ProjectId = createProject.Project.ProjectId
                        };
                        _context.Add(newAP);
                    }
                }
                // Save all the changes to the database. The additions to the database will not completed until they are saved. This is similar to git like adding changes to staging before making a commit.
                await _context.SaveChangesAsync();

                // Redirect to the projects being filtered to show only the the Current project.
                return(RedirectToAction("ProjectsofStatus", new { id = 3 }));
            }

            // All of this logic is adding the list options back to the create view model so that if the model state is not valid it will return to the create view and maintain the lists. If this is not done when it returns to the view the lists will be empty because the lists are not being held. This logic comes from the get portion of the create.
            var user = await GetCurrentUserAsync();

            var projectTypes = await _context.ProjectType.ToListAsync();

            var statusTypes = await _context.StatusType.ToListAsync();

            var clients = await _context.Client.Where(c => c.User == user).ToListAsync();

            var players = await _context.Player.Where(c => c.User == user).ToListAsync();

            var artists = await _context.Artist.Where(c => c.User == user).ToListAsync();

            var projectTypeListOptions = new List <SelectListItem>();
            var statusTypeListOptions  = new List <SelectListItem>();
            var clientListOptions      = new List <SelectListItem>();
            var playerListOptions      = new List <SelectListItem>();
            var artistListOptions      = new List <SelectListItem>();

            foreach (ProjectType pt in projectTypes)
            {
                projectTypeListOptions.Add(new SelectListItem
                {
                    Value = pt.ProjectTypeId.ToString(),
                    Text  = pt.Type
                });
            }

            foreach (StatusType st in statusTypes)
            {
                statusTypeListOptions.Add(new SelectListItem
                {
                    Value = st.StatusTypeId.ToString(),
                    Text  = st.Type
                });
            }

            foreach (Client c in clients)
            {
                clientListOptions.Add(new SelectListItem
                {
                    Value = c.ClientId.ToString(),
                    Text  = c.Name
                });
            }

            foreach (Player p in players)
            {
                playerListOptions.Add(new SelectListItem
                {
                    Value = p.PlayerId.ToString(),
                    Text  = p.FirstName + " " + p.LastName
                });
            }

            foreach (Artist a in artists)
            {
                artistListOptions.Add(new SelectListItem
                {
                    Value = a.ArtistId.ToString(),
                    Text  = a.Name
                });
            }

            projectTypeListOptions.Insert(0, new SelectListItem
            {
                Text  = "Choose a Project Type",
                Value = "0"
            });

            statusTypeListOptions.Insert(0, new SelectListItem
            {
                Text  = "Choose a Status",
                Value = "0"
            });

            clientListOptions.Insert(0, new SelectListItem
            {
                Text  = "Choose a Client",
                Value = "0"
            });

            createProject.ProjectTypes     = projectTypeListOptions;
            createProject.StatusTypes      = statusTypeListOptions;
            createProject.Clients          = clientListOptions;
            createProject.AvailablePlayers = playerListOptions;
            createProject.AvailableArtists = artistListOptions;

            return(View(createProject));
        }