/// <summary> /// Creates a new song /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { int length = 0; bool result = Int32.TryParse(this.lengthBox.Text.Trim(), out length); if (result && length > 0 && this.artistBox.Text.Trim().Length > 0 && this.albumBox.Text.Trim().Length > 0 && this.titleBox.Text.Trim().Length > 0 && this.genreBox.Text.Trim().Length > 0) { Song newSong = new Song(this.artistBox.Text.Trim(), this.albumBox.Text.Trim(), this.titleBox.Text.Trim(), this.genreBox.Text.Trim(), Convert.ToInt32(this.lengthBox.Text.Trim())); this.projectController.AddSong(newSong); this.DialogResult = DialogResult.OK; this.Dispose(); } else if (!result) { MessageBox.Show("Please enter a valid length (in seconds)"); } else { MessageBox.Show("Invalid information. Please verify all fields are filled out."); } }
/// <summary> /// Removes a song from the database /// </summary> /// <param name="s">The song to remove from the database</param> public void RemoveSong(Song s) { this.Songs.RemoveAll(r => r.Artist == s.Artist && r.Album == s.Album && r.Title == s.Title && r.Genre == s.Genre && r.Length == s.Length); }
/// <summary> /// Adds a song to the database /// </summary> /// <param name="song">The song to add to the database</param> public void AddSong(Song song) { this.Songs.Add(song); }
/// <summary> /// Saves the newly edited song /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { // Create the song that is to be added to the database DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; Song toAdd = new Song(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString(), (int)row.Cells[4].Value); // If the new song is different than the old song, remove the old one and add the new one then refresh if (toAdd.Artist != this.currentEdit.Artist || toAdd.Album != this.currentEdit.Album || toAdd.Title != this.currentEdit.Title || toAdd.Genre != this.currentEdit.Genre || toAdd.Length != this.currentEdit.Length) { projectController.RemoveSong(this.currentEdit); projectController.AddSong(toAdd); dataGridView1.DataSource = projectController.GetSortedSongList(currentSortField, currentSortType); } currentEdit = null; }
/// <summary> /// Removes the selected songs from the database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void deleteButton_Click(object sender, EventArgs e) { // For each selected row, delete the corresponding song from the database if (dataGridView1.SelectedRows.Count > 0) { foreach (DataGridViewRow row in dataGridView1.SelectedRows) { Song toRemove = new Song(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString(), (int)row.Cells[4].Value); projectController.RemoveSong(toRemove); dataGridView1.DataSource = projectController.GetSortedSongList(currentSortField, currentSortType); } } else { MessageBox.Show("No songs are selected."); } }
/// <summary> /// Backs up song at the beginning of a cell edit /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { // When owner begins to edit a cell, backup the song that is being edited so we know what to remove from the database DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; currentEdit = new Song(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString(), (int)row.Cells[4].Value); }
/// <summary> /// Adds a new song to the database /// </summary> /// <param name="song">The song to add to the database</param> public void AddSong(Song song) { dataAccess.AddSong(song); }
/// <summary> /// Sorts an array of songs /// </summary> /// <param name="songsToSort">The songs to sort</param> /// <param name="field">The song field to sort by</param> /// <param name="sortType">The type of sort to perform</param> /// <returns>The sorted list of songs</returns> private Song[] SortSongs(Song[] songsToSort, SongFields field, SortType sortType) { switch (field) { case SongFields.Album: if (sortType == SortType.Ascending) { songsToSort = songsToSort.OrderBy(s => s.Album).ThenBy(a => a.Title).ToArray(); } else { songsToSort = songsToSort.OrderByDescending(s => s.Album).ThenByDescending(a => a.Title).ToArray(); } break; case SongFields.Artist: if (sortType == SortType.Ascending) { songsToSort = songsToSort.OrderBy(s => s.Artist).ThenBy(a => a.Album).ThenBy(a => a.Title).ToArray(); } else { songsToSort = songsToSort.OrderByDescending(s => s.Artist).ThenByDescending(a => a.Album).ThenByDescending(a => a.Title).ToArray(); } break; case SongFields.Genre: if (sortType == SortType.Ascending) { songsToSort = songsToSort.OrderBy(s => s.Genre).ThenBy(a => a.Artist).ThenBy(a => a.Album).ThenBy(a => a.Title).ToArray(); } else { songsToSort = songsToSort.OrderByDescending(s => s.Genre).ThenBy(a => a.Artist).ThenByDescending(a => a.Album).ThenByDescending(a => a.Title).ToArray(); } break; case SongFields.Title: if (sortType == SortType.Ascending) { songsToSort = songsToSort.OrderBy(s => s.Title).ThenBy(a => a.Artist).ToArray(); } else { songsToSort = songsToSort.OrderByDescending(s => s.Title).ThenByDescending(a => a.Artist).ToArray(); } break; } return songsToSort; }
/// <summary> /// Searches an array of songs /// </summary> /// <param name="songsToSearch">The songs to search</param> /// <param name="searchString">The string to searh for</param> /// <param name="searchField">The song field to search in</param> /// <returns>The search results</returns> private Song[] SearchSongs(Song[] songsToSearch, string searchString, SongFields searchField) { switch (searchField) { case SongFields.Album: songsToSearch = songsToSearch.Where(s => s.Album.ToUpper().Contains(searchString.ToUpper())).ToArray(); break; case SongFields.Artist: songsToSearch = songsToSearch.Where(s => s.Artist.ToUpper().Contains(searchString.ToUpper())).ToArray(); break; case SongFields.Genre: songsToSearch = songsToSearch.Where(s => s.Genre.ToUpper().Contains(searchString.ToUpper())).ToArray(); break; case SongFields.Title: songsToSearch = songsToSearch.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper())).ToArray(); break; } return songsToSearch; }
/// <summary> /// Removes the song from the database /// </summary> /// <param name="s">The song to remove from the database</param> public void RemoveSong(Song s) { this.dataAccess.RemoveSong(s); }