public void deleteItem(object item) { if (item == null) { return; } tbl_collection collection = item as tbl_collection; if (collection == null) { return; } using (var db = new RadioPlayer()) { foreach (var map in db.tbl_collectionmap.Where(m => m.collection_id == collection.collection_id)) { db.tbl_collectionmap.Remove(map); } db.tbl_collection.Remove(db.tbl_collection.Where(g => g.collection_id == collection.collection_id).First()); db.SaveChanges(); } }
public void saveChanges() { using (var db = new RadioPlayer()) { tbl_collection collection = null; if (!this._isCreateMode) { var id = Convert.ToInt32(this.txtID.Text); collection = db.tbl_collection.Where(c => c.collection_id == id).FirstOrDefault(); if (collection == null) { this._mainInterface.statusText = $"ERROR: ID '{this.txtID.Text}' does not exist."; return; } } else { collection = new tbl_collection(); } collection.title = this.txtTitle.Text; // Any values left in mapList need to be removed. var mapList = collection.tbl_collectionmap.ToList(); var trackList = this.listTracks.items.Select(i => i as tbl_track).ToList(); for (int index = 0; index < trackList.Count(); index++) { var track = trackList[index]; var map = mapList.Where(m => m.track_id == track.track_id).FirstOrDefault(); if (map == null) { db.tbl_collectionmap.Add(new tbl_collectionmap() { tbl_collection = collection, track_id = track.track_id, sequence_index = index }); continue; } map.sequence_index = index; mapList.Remove(map); } foreach (var map in mapList) { db.tbl_collectionmap.Remove(map); } if (this._isCreateMode) { db.tbl_collection.Add(collection); } db.SaveChanges(); } }
public void saveChanges() { using (var db = new RadioPlayer()) { var track = new tbl_track(); track.title = _tag.Title; track.subtitle = "N/A"; track.artists = String.Join(",", _tag.AlbumArtists); track.composers = String.Join(",", _tag.Composers); track.bitrate = Convert.ToString(_format.AverageBytesPerSecond * 8); track.publisher = "N/A"; track.parental_advisory = false; track.folder_path = Path.GetDirectoryName(_fileName).Replace(Config.PathToRoot, "").Replace(Config.ServerName, "").TrimStart('\\', '/'); track.file_name = Path.GetFileName(_fileName); track.duration = _totalSeconds; track.tbl_format = db.tbl_format.First(f => f.description == _extension); track.date_recorded = null; track.date_released = null; track.likes = 0; track.dislikes = 0; track.keywords = "N/A"; var lengthStream = System.IO.File.Open(_fileName, FileMode.Open); track.filesize = lengthStream.Length; lengthStream.Dispose(); if (db.tbl_track.Any(t => t.title == track.title)) { var result = MessageBox.Show( $"A track called '{track.title}' already exists, are you sure you want to" + $" continue with adding this track?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question ); if (result == MessageBoxResult.No) { return; } } db.tbl_track.Add(track); // Auto-add it to a collection. var collection = db.tbl_collection.FirstOrDefault(c => c.title.ToLower() == _tag.Album.ToLower()); if (collection == null && !String.IsNullOrWhiteSpace(_tag.Album)) { var result = MessageBox.Show( $"There is no collection called '{_tag.Album}', would you " + $"like to create one for this track?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question ); if (result == MessageBoxResult.Yes) { collection = new tbl_collection(); collection.title = _tag.Album; db.tbl_collection.Add(collection); } } if (collection != null) { var map = new tbl_collectionmap(); map.tbl_collection = collection; map.tbl_track = track; map.sequence_index = 999; db.tbl_collectionmap.Add(map); } db.SaveChanges(); } }