public void Delete(Guid pieceId, Guid genreId)
        {
            PieceGenre pieceGenre = new PieceGenre
            {
                GenreId = genreId,
                PieceId = pieceId
            };

            pieceGenre.Delete();
        }
Exemplo n.º 2
0
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if ((string)btnSave.Content == "Add Piece")
                {
                    //Add the piece

                    //Make sure that the fields are filled out that cannot be null
                    if (string.IsNullOrEmpty(txtName.Text))
                    {
                        throw new Exception("Piece must have a name");
                    }

                    //Create and set values on a Piece
                    Piece piece = new Piece();

                    piece.Name = txtName.Text;

                    if (!string.IsNullOrEmpty(txtGradeLevel.Text))
                    {
                        piece.GradeLevel = txtGradeLevel.Text;
                    }
                    else
                    {
                        piece.GradeLevel = string.Empty;
                    }


                    if (!string.IsNullOrEmpty(txtPerformanceNotes.Text))
                    {
                        piece.PerformanceNotes = txtPerformanceNotes.Text;
                    }
                    else
                    {
                        piece.PerformanceNotes = string.Empty;
                    }

                    if (!string.IsNullOrEmpty(txtYearWritten.Text))
                    {
                        int yearWritten;
                        int.TryParse(txtYearWritten.Text, out yearWritten);
                        piece.YearWritten = yearWritten;
                    }

                    //Send it to the API
                    HttpClient client          = InitializeClient();
                    string     serializedPiece = JsonConvert.SerializeObject(piece);
                    var        content         = new StringContent(serializedPiece);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = client.PostAsync("Piece", content).Result;

                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        //Get the new piece Id
                        response = client.GetAsync("Piece?name=" + piece.Name).Result;
                        string result         = response.Content.ReadAsStringAsync().Result;
                        Piece  retrievedPiece = JsonConvert.DeserializeObject <Piece>(result);

                        //Save the Id so that we can update it.
                        piece.Id = retrievedPiece.Id;

                        foreach (Composer composer in lstComposer.SelectedItems)
                        {
                            PieceWriter pieceWriter = new PieceWriter();
                            pieceWriter.ComposerId     = composer.Id;
                            pieceWriter.PieceId        = retrievedPiece.Id;
                            pieceWriter.ComposerTypeId = composerTypes.FirstOrDefault(ct => ct.Description == "Composer").Id;

                            //Add to the piece
                            piece.PieceWriters.Add(pieceWriter);

                            //Send it to the API
                            string serializedPieceWriter = JsonConvert.SerializeObject(pieceWriter);
                            content = new StringContent(serializedPieceWriter);
                            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                            response = client.PostAsync("PieceWriter", content).Result;
                        }

                        foreach (Composer arranger in lstArranger.SelectedItems)
                        {
                            PieceWriter pieceWriter = new PieceWriter();
                            pieceWriter.ComposerId     = arranger.Id;
                            pieceWriter.PieceId        = retrievedPiece.Id;
                            pieceWriter.ComposerTypeId = composerTypes.FirstOrDefault(ct => ct.Description == "Arranger").Id;

                            //Add to the piece
                            piece.PieceWriters.Add(pieceWriter);

                            //Send it to the API
                            string serializedPieceWriter = JsonConvert.SerializeObject(pieceWriter);
                            content = new StringContent(serializedPieceWriter);
                            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                            response = client.PostAsync("PieceWriter", content).Result;
                        }

                        foreach (Genre genre in lstGenre.SelectedItems)
                        {
                            PieceGenre pieceGenre = new PieceGenre
                            {
                                GenreId = genre.Id,
                                PieceId = retrievedPiece.Id
                            };

                            //Add to the piece
                            piece.Genres.Add(genre);

                            //Send it to the API
                            string serializedPieceGenre = JsonConvert.SerializeObject(pieceGenre);
                            content = new StringContent(serializedPieceGenre);
                            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                            response = client.PostAsync("PieceGenre", content).Result;
                        }

                        pieces.Add(piece);
                        Rebind();


                        //Select the inserted piece
                        cboPiece.SelectedIndex = pieces.FindIndex(p => p == piece);
                    }
                    else
                    {
                        throw new Exception("Piece could not be inserted");
                    }
                }
                else
                {
                    //Update the piece

                    //Make sure that the fields are filled out that cannot be null
                    if (string.IsNullOrEmpty(txtName.Text))
                    {
                        throw new Exception("Piece must have a name");
                    }

                    //Create and set values on a Piece
                    Piece piece = new Piece();

                    piece = pieces[cboPiece.SelectedIndex];

                    piece.Name = txtName.Text;

                    if (!string.IsNullOrEmpty(txtGradeLevel.Text))
                    {
                        piece.GradeLevel = txtGradeLevel.Text;
                    }
                    else
                    {
                        piece.GradeLevel = string.Empty;
                    }


                    if (!string.IsNullOrEmpty(txtPerformanceNotes.Text))
                    {
                        piece.PerformanceNotes = txtPerformanceNotes.Text;
                    }
                    else
                    {
                        piece.PerformanceNotes = string.Empty;
                    }

                    if (!string.IsNullOrEmpty(txtYearWritten.Text))
                    {
                        int yearWritten;
                        int.TryParse(txtYearWritten.Text, out yearWritten);
                        piece.YearWritten = yearWritten;
                    }

                    //Send it to the API
                    HttpClient client          = InitializeClient();
                    string     serializedPiece = JsonConvert.SerializeObject(piece);
                    var        content         = new StringContent(serializedPiece);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = client.PutAsync("Piece/" + piece.Id, content).Result;

                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        Guid        composerTypeId = composerTypes.FirstOrDefault(ct => ct.Description == "Composer").Id;
                        List <Guid> oldComposers   = new List <Guid>();
                        oldComposers = piece.PieceWriters.Where(pw => pw.ComposerTypeId == composerTypeId).Select(pw => pw.ComposerId).ToList();

                        List <Guid> selectedComposers = new List <Guid>();

                        //Add new composers
                        foreach (Composer composer in lstComposer.SelectedItems)
                        {
                            //If they are not in the old composers list, add them.
                            if (!oldComposers.Contains(composer.Id))
                            {
                                PieceWriter pieceWriter = new PieceWriter();
                                pieceWriter.ComposerId     = composer.Id;
                                pieceWriter.PieceId        = piece.Id;
                                pieceWriter.ComposerTypeId = composerTypeId;

                                //Add to the piece
                                piece.PieceWriters.Add(pieceWriter);

                                //Send it to the API
                                string serializedPieceWriter = JsonConvert.SerializeObject(pieceWriter);
                                content = new StringContent(serializedPieceWriter);
                                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                                response = client.PostAsync("PieceWriter", content).Result;
                            }

                            //Add to the selected composer list
                            selectedComposers.Add(composer.Id);
                        }

                        //Delete the composer peice writers that are no longer selected
                        foreach (Guid composerId in oldComposers.Except(selectedComposers))
                        {
                            PieceWriter pieceWriter = new PieceWriter();
                            pieceWriter = piece.PieceWriters.FirstOrDefault(pw => pw.ComposerId == composerId && pw.ComposerTypeId == composerTypeId);

                            response = client.DeleteAsync("PieceWriter/" + pieceWriter.Id).Result;

                            piece.PieceWriters.Remove(pieceWriter);
                        }


                        //Setup arrangers
                        Guid        arrangerTypeId = composerTypes.FirstOrDefault(ct => ct.Description == "Arranger").Id;
                        List <Guid> oldArrangers   = new List <Guid>();
                        oldArrangers = piece.PieceWriters.Where(pw => pw.ComposerTypeId == arrangerTypeId).Select(pw => pw.ComposerId).ToList();

                        List <Guid> selectedArrangers = new List <Guid>();

                        //Add new arrangers
                        foreach (Composer arranger in lstArranger.SelectedItems)
                        {
                            //If they are not in the old arranger list, add them.
                            if (!oldArrangers.Contains(arranger.Id))
                            {
                                PieceWriter pieceWriter = new PieceWriter();
                                pieceWriter.ComposerId     = arranger.Id;
                                pieceWriter.PieceId        = piece.Id;
                                pieceWriter.ComposerTypeId = arrangerTypeId;

                                //Add to the piece
                                piece.PieceWriters.Add(pieceWriter);

                                //Send it to the API
                                string serializedPieceWriter = JsonConvert.SerializeObject(pieceWriter);
                                content = new StringContent(serializedPieceWriter);
                                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                                response = client.PostAsync("PieceWriter", content).Result;
                            }

                            //Add to the selected arranger list
                            selectedArrangers.Add(arranger.Id);
                        }

                        //Delete the arranger peice writers that are no longer selected
                        foreach (Guid composerId in oldArrangers.Except(selectedArrangers))
                        {
                            PieceWriter pieceWriter = new PieceWriter();
                            pieceWriter = piece.PieceWriters.FirstOrDefault(pw => pw.ComposerId == composerId && pw.ComposerTypeId == arrangerTypeId);

                            response = client.DeleteAsync("PieceWriter/" + pieceWriter.Id).Result;

                            piece.PieceWriters.Remove(pieceWriter);
                        }



                        //Setup genres
                        List <Guid> oldGenres = new List <Guid>();
                        oldGenres = piece.Genres.Select(g => g.Id).ToList();

                        List <Guid> selectedGenres = new List <Guid>();

                        //Add new arrangers
                        foreach (Genre genre in lstGenre.SelectedItems)
                        {
                            //If they are not in the old genre list, add them.
                            if (!oldGenres.Contains(genre.Id))
                            {
                                PieceGenre pieceGenre = new PieceGenre();
                                pieceGenre.PieceId = piece.Id;
                                pieceGenre.GenreId = genre.Id;

                                //Send it to the API
                                string serializedPieceGenre = JsonConvert.SerializeObject(pieceGenre);
                                content = new StringContent(serializedPieceGenre);
                                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                                response = client.PostAsync("PieceGenre", content).Result;

                                piece.Genres.Add(genre);
                            }

                            //Add to the selected gener list
                            selectedGenres.Add(genre.Id);
                        }

                        //Delete the genres that are no longer selected
                        foreach (Guid genreId in oldGenres.Except(selectedGenres))
                        {
                            response = client.DeleteAsync("PieceGenre?pieceId=" + piece.Id + "&genreId=" + genreId).Result;

                            Genre genreToRemove = piece.Genres.FirstOrDefault(g => g.Id == genreId);
                            piece.Genres.Remove(genreToRemove);
                        }


                        //Save index, refresh screen, and reselect where we are.
                        var index = cboPiece.SelectedIndex;
                        Rebind();
                        cboPiece.SelectedIndex = index;


                        MessageBox.Show("Piece has been saved!", "Success");
                    }
                    else
                    {
                        throw new Exception("Piece could not be updated");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 public void Post(PieceGenre pieceGenre)
 {
     pieceGenre.Add();
 }