public async Task <IActionResult> Edit(int id, ClientCreateEditViewModel editClient)
        {
            if (id != editClient.Client.ClientId)
            {
                return(NotFound());
            }

            // Remove the User and UserId so the ModelState can be Valid
            ModelState.Remove("Client.User");
            ModelState.Remove("Client.UserId");
            if (ModelState.IsValid)
            {
                // Delete the joiner tables associated with the client. Retrieve all of the ClientArtists from the database then loop over them and remove each one. This is so you can add the joiner tables from the edited client and not have duplicates.
                var clientArtists = await _context.ClientArtist.Where(ca => ca.ClientId == id).ToListAsync();

                if (clientArtists != null)
                {
                    foreach (ClientArtist ca in clientArtists)
                    {
                        _context.ClientArtist.Remove(ca);
                    }
                }
                try
                {
                    // Add the current user
                    editClient.Client.User = await GetCurrentUserAsync();

                    editClient.Client.UserId = editClient.Client.User.Id;
                    // Add the new joiner tables if their are any from the edited client. Foreach over the selected artists and add the ClientArtists to the database
                    if (editClient.SelectedArtists != null)
                    {
                        foreach (int artistId in editClient.SelectedArtists)
                        {
                            ClientArtist newCA = new ClientArtist()
                            {
                                ClientId = editClient.Client.ClientId,
                                ArtistId = artistId
                            };
                            _context.Add(newCA);
                        }
                    }
                    // Update the database with the edited client and save the changes.
                    _context.Update(editClient.Client);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ClientExists(editClient.Client.ClientId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(editClient));
        }
        public async Task <IActionResult> Create(ArtistCreateEditViewModel createArtist)
        {
            // Remove the User and UserId so the ModelState can be valid
            ModelState.Remove("Artist.User");
            ModelState.Remove("Artist.UserId");
            if (ModelState.IsValid)
            {
                // Attach the current user to the created artist and add them to the database
                createArtist.Artist.User = await GetCurrentUserAsync();

                createArtist.Artist.UserId = createArtist.Artist.User.Id;
                _context.Add(createArtist.Artist);
                // If the created artist has any SelectedClients foreach over it to create the individual ClientArtist and add it to the database.
                if (createArtist.SelectedClients != null)
                {
                    foreach (int clientId in createArtist.SelectedClients)
                    {
                        ClientArtist newCA = new ClientArtist()
                        {
                            ClientId = clientId,
                            ArtistId = createArtist.Artist.ArtistId
                        };
                        _context.Add(newCA);
                    }
                }
                // Once everything has been added save the changes to the database.
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(createArtist));
        }
        public async Task <IActionResult> Create(ClientCreateEditViewModel createClient)
        {
            // Remove the User and UserId so the ModelState can be valid
            ModelState.Remove("Client.User");
            ModelState.Remove("Client.UserId");
            if (ModelState.IsValid)
            {
                // Add the current user to the created client. Then add that client to the database.
                createClient.Client.User = await GetCurrentUserAsync();

                createClient.Client.UserId = createClient.Client.User.Id;
                _context.Add(createClient.Client);
                // If the created client has any selected artists. Then loop over them and create a ClientArtist for each one as well as add them to the database.
                if (createClient.SelectedArtists != null)
                {
                    foreach (int artistId in createClient.SelectedArtists)
                    {
                        ClientArtist newCA = new ClientArtist()
                        {
                            ClientId = createClient.Client.ClientId,
                            ArtistId = artistId
                        };
                        _context.Add(newCA);
                    }
                }
                // Save all the changes made to the database
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(createClient));
        }
        public async Task <IActionResult> Edit(int id, ArtistCreateEditViewModel editArtist)
        {
            if (id != editArtist.Artist.ArtistId)
            {
                return(NotFound());
            }

            // Remove the User and UserId so the ModelState can be valid
            ModelState.Remove("Artist.User");
            ModelState.Remove("Artist.UserId");
            if (ModelState.IsValid)
            {
                // Delete joiner tables associated with the Artist before the edit so that there will not be duplicate tables after the edit is submitted.
                // First retrieve the ClientArtists from the database then loop over them to remove each one.
                var clientArtists = await _context.ClientArtist.Where(ca => ca.ArtistId == id).ToListAsync();

                if (clientArtists != null)
                {
                    foreach (ClientArtist ca in clientArtists)
                    {
                        _context.ClientArtist.Remove(ca);
                    }
                }
                try
                {
                    // Add the current user to the edited artist
                    editArtist.Artist.User = await GetCurrentUserAsync();

                    editArtist.Artist.UserId = editArtist.Artist.User.Id;
                    // Add the new joiner tables to the database if there are any. If there areany SelectedClients loop over them to create the ClientArtist and add it to the database.
                    if (editArtist.SelectedClients != null)
                    {
                        foreach (int clientId in editArtist.SelectedClients)
                        {
                            ClientArtist newCA = new ClientArtist()
                            {
                                ClientId = clientId,
                                ArtistId = editArtist.Artist.ArtistId
                            };
                            _context.Add(newCA);
                        }
                    }
                    // Update the database with the new artist and save the changes
                    _context.Update(editArtist.Artist);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ArtistExists(editArtist.Artist.ArtistId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(editArtist));
        }