public void Albums_Update(Album item) { using (var context = new ChinookContext()) { var existing = context.Albums.Find(item.AlbumId); if (existing == null) { throw new Exception("Album no long on file."); } //any business rules //any data refinements //review of using iif //composer can be a null string //we do not wish to store an empty string item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null : item.ReleaseLabel; //update the existing instance of trackinfo on the database context.Entry(item).State = System.Data.Entity.EntityState.Modified; //update command if updating selected fields //context.Entry(instancevariablename).Property(y => y.columnname).IsModified = true; context.SaveChanges(); } }
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { Playlist exists = (from x in context.Playlists where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Playlist has been removed from the database."); } else { var trackskept = exists.PlaylistTracks.Where(tr => !trackstodelete.Any(tod => tod == tr.TrackId)).Select(tr => tr); PlaylistTrack item = null; foreach (int dtrack in trackstodelete) { item = exists.PlaylistTracks.Where(dx => dx.TrackId == dtrack).FirstOrDefault(); exists.PlaylistTracks.Remove(item); } int newrownumber = 1; foreach (var trackkept in trackskept) { trackkept.TrackNumber = newrownumber; context.Entry(trackkept).Property(y => y.TrackNumber).IsModified = true; newrownumber++; } context.SaveChanges(); } } }//eom
public int Artist_Update(Artist artist) { using (var context = new ChinookContext()) { context.Entry(artist).State = System.Data.Entity.EntityState.Modified; //staged return(context.SaveChanges()); //committed } }
public int Album_Update(Album item) { using (var context = new ChinookContext()) { context.Entry(item).State = System.Data.Entity.EntityState.Modified; return(context.SaveChanges()); } }
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { var existingTrack = ( from x in context.Playlists where x.Name.Equals(playlistname) && x.UserName.Equals(username) select x) .FirstOrDefault(); if (existingTrack == null) { throw new Exception("Playlist not found on file."); } else { // Track physical order may not adhere to the logical order (track numbering) var tracksToKeep = existingTrack.PlaylistTracks .Where(t => !trackstodelete.Any( d => d == t.TrackId)) // Look for an item in list A that is also in list B .OrderBy(t => t.TrackNumber) // Maintains current track order .Select(t => t); // Delete tracks PlaylistTrack item = null; foreach (var trackid in trackstodelete) { item = existingTrack.PlaylistTracks .Where(t => t.TrackId == trackid) .FirstOrDefault(); if (item != null) { existingTrack.PlaylistTracks.Remove(item); } } // Renumber tracks (if any remain) int number = 1; foreach (var track in tracksToKeep) { track.TrackNumber = number; number++; context.Entry(track).Property(y => y.TrackNumber).IsModified = true; } // Commit changes context.SaveChanges(); } } }//eom
public int Album_Update(Album item) { using (var context = new ChinookContext()) { item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null : item.ReleaseLabel; context.Entry(item).State = System.Data.Entity.EntityState.Modified; return(context.SaveChanges()); } }
public void Albums_Update(Album item) { using (var context = new ChinookContext()) { item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null : item.ReleaseLabel; //this will take the item, find item, and change it context.Entry(item).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public int Album_Update(Album item) { using (var context = new ChinookContext()) { item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null : item.ReleaseLabel; //if it contains an empry string then I set it to null else I set it the value inside the field context.Entry(item).State = System.Data.Entity.EntityState.Modified; //stages the update return(context.SaveChanges()); //sends the number of rows that were affected } }
}//eom public List <UserPlaylistTrack> DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { //code to go here //firstor default it takes the first occurence in collection, if there is nothing there then itll give you a null value Playlist exists = (from x in context.Playlists where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Playlist has been removed from the databse."); } else { //the tracks that are to be kept //take the track off the current place is what teh code below does //when you do anyhting in lamda, the first thing is the object Name or row (tr) //come over to trackstodelete to do the following work which is the Any test //so what are the condition of the any test? //you have to refer to an object in the second list which is the tdo, compare the tr instance to the tdo instance //what you are doing is comparing the tdo which is an int and is it equal to the track id, tdo ==tr.trackid var trackskept = exists.PlaylistTracks.Where(tr => !trackstodelete.Any(tdo => tdo == tr.TrackId)).Select(tr => tr); //Remove the tracks in the trackstodelete list PlaylistTrack item = null; foreach (var deletetrack in trackstodelete) { item = exists.PlaylistTracks .Where(dx => dx.TrackId == deletetrack).FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } //renumber the kept tracks so that the tracknumber //is sequential as is expected by all other operations //in our database there is no holes in the numeric //sequence int number = 1; foreach (var trackkept in trackskept) { trackkept.TrackNumber = number; context.Entry(trackkept).Property(y => y.TrackNumber).IsModified = true; number++; } context.SaveChanges(); return(List_TracksForPlaylist(playlistname, username)); } } }//eom
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { //Note: if we remove the items from the list, their id will still be not in order since we have deleted something in between // So, we can first put those items in a temporary list, clear up (stage remove) the items from the real list // And then, repopulate everything using the temporary lits we made and renumber them at the same time //playlist exists? var exists = (from x in context.Playlists where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase) select x).FirstOrDefault(); if (exists == null) { //no: message throw new Exception("Play list has been removed from the system"); } else { // yes: Create a list of playlisttracks that are to be kept List <PlaylistTrack> trackskept = exists.PlaylistTracks .Where(tr => !trackstodelete.Any(tod => tr.TrackId == tod)) //Compare each and every from one table to all of other table (So, with this query, each and every of outer list are compared to the ones in inner list, so we select records that don't match) .Select(tr => tr) .ToList(); // stage the removal of tracks PlaylistTrack item = null; foreach (var dtrackid in trackstodelete) { item = exists.PlaylistTracks .Where(tr => tr.TrackId == dtrackid) .FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } // renumbering of kept tracks and stage update int number = 1; trackskept.Sort((x, y) => x.TrackId.CompareTo(y.TrackNumber)); foreach (var tkept in trackskept) { tkept.TrackNumber = number; context.Entry(tkept).Property(y => y.TrackNumber).IsModified = true; number++; } // Commit context.SaveChanges(); } } }//eom
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { //code to go here var exists = (from x in context.Playlists where x.Name.Equals(playlistname) && x.UserName.Equals(username) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Play list has been removed from the files."); } else { //get a list of tracks that will be kept IN ORDER of TRACKNUMBER //you do NOT know if the physical order is the same as the // logical TrackNumber order //.Any() allows you to search for an item in a list using a condition, returns true if found //looking an item in ListA is inside ListB //in this example we DO NOT want to find it thus ! var trackskept = exists.PlaylistTracks. Where(tr => !trackstodelete.Any(tod => tod == tr.TrackId)). OrderBy(tr => tr.TrackNumber). Select(tr => tr); //delete tracks PlaylistTrack item = null; foreach (var deletetrackid in trackstodelete) { item = exists.PlaylistTracks. Where(tr => tr.TrackId == deletetrackid). FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } //renumber remaining tracks (tracks that were kept) int number = 1; foreach (var tkept in trackskept) { tkept.TrackNumber = number; number++; context.Entry(tkept).Property(y => y.TrackNumber).IsModified = true; } //commit work context.SaveChanges(); } } }//eom
public void Album_Update(Album _album) { using (ChinookContext context = new ChinookContext()) { _album.ReleaseLabel = string.IsNullOrEmpty(_album.ReleaseLabel) ? null : _album.ReleaseLabel; // Flag record as being modified context.Entry(_album).State = EntityState.Modified; // Commit the changes to the database context.SaveChanges(); } }
public void Albums_Update(Album item) { using (var context = new ChinookContext()) { context.Albums.Attach(item); item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null : item.ReleaseLabel; context.Entry(item).State = System.Data.Entity.EntityState.Modified; //Use this to update the command for selected fields. //context.Entry(instacevariablename).Property(y=>y.columnname) context.SaveChanges(); } }
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { var exists = (from x in context.Playlists where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase) select x).FirstOrDefault(); if (exists == null) { // no: message throw new Exception("Play list has been removed from the system"); } else { // yes: create a list of playlistTrackIds that are to be kept. List <PlaylistTrack> trackskept = exists.PlaylistTracks .Where(tr => !trackstodelete.Any(tod => tr.TrackId == tod)) .Select(tr => tr) .ToList(); // stage the removal of all the other tracks PlaylistTrack item = null; foreach (var dtrackid in trackstodelete) { item = exists.PlaylistTracks .Where(tr => tr.TrackId == dtrackid) .FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } // renumber the kept tracks // and stage the update int number = 1; trackskept.Sort((x, y) => x.TrackNumber.CompareTo(y.TrackNumber)); foreach (var tKept in trackskept) { tKept.TrackNumber = number; context.Entry(tKept).Property(y => y.TrackNumber).IsModified = true; number++; } // commit context.SaveChanges(); } } }//eom
public void MoveTrack(string playlistname, int trackid, int tracknumber, int customerid, string updown) { using (var context = new ChinookContext()) { Playlist existing = (from x in context.PlayLists where x.Name.Equals(playlistname) && x.CustomerId == customerid select x).FirstOrDefault(); PlaylistTrack movetrack = (from x in context.PlaylistTracks where x.PlaylistId == existing.PlaylistId && x.TrackId == trackid select x).FirstOrDefault(); PlaylistTrack othertrack = null; if (updown.Equals("up")) { othertrack = (from x in context.PlaylistTracks where x.PlaylistId == existing.PlaylistId && x.TrackNumber == tracknumber - 1 select x).FirstOrDefault(); movetrack.TrackNumber -= 1; othertrack.TrackNumber += 1; } else { othertrack = (from x in context.PlaylistTracks where x.PlaylistId == existing.PlaylistId && x.TrackNumber == tracknumber + 1 select x).FirstOrDefault(); movetrack.TrackNumber += 1; othertrack.TrackNumber -= 1; } context.Entry(movetrack).Property(y => y.TrackNumber).IsModified = true; context.Entry(othertrack).Property(y => y.TrackNumber).IsModified = true; context.SaveChanges(); } }
public int Album_Update(Album album) { using (var context = new ChinookContext()) { if (CheckReleaseYear(album)) { context.Entry(album).State = System.Data.Entity.EntityState.Modified; //staged return(context.SaveChanges()); //committed } else { throw new BusinessRuleException("Validation error: ", reasons); } } }
public int Album_Update(Album item) { using (var context = new ChinookContext()) if (CheckReleaseYear(item)) { context.Entry(item).State = System.Data.Entity.EntityState.Modified; return(context.SaveChanges()); } else { throw new BusinessRuleException("Validation Error", reasons); } }
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { //code to go here var exists = (from x in context.Playlists where x.UserName.Equals(username) && x.Name.Equals(playlistname) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Play list has been removed from the system."); } else { //find the songs to keep var tracksKept = exists.PlaylistTracks .Where(tr => !trackstodelete.Any(tod => tod == tr.TrackId)) .OrderBy(tr => tr.TrackNumber) .Select(tr => tr); //remove the tracks to delete PlaylistTrack item = null; foreach (var dtrackid in trackstodelete) { item = exists.PlaylistTracks .Where(tr => tr.TrackId == dtrackid) .Select(tr => tr) .FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } //resequence kept tracks int number = 1; foreach (var tKept in tracksKept) { tKept.TrackNumber = number; context.Entry(tKept).Property("TrackNumber").IsModified = true; number++; } context.SaveChanges(); } } }//eom
public void Update_Track(Track trackinfo) { using (var context = new ChinookContext()) { // any business rules // Any data refinements // review of iif //composer can be a null string, we do not wish to store an empty string trackinfo.Composer = string.IsNullOrEmpty(trackinfo.Composer) ? null : trackinfo.Composer; // update the instance of track info to the database context.Entry(trackinfo).State = System.Data.Entity.EntityState.Modified; // commit of the update context.SaveChanges(); } }
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { //check for playlist var exists = (from x in context.Playlists where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Playlist removed from system."); } else { //make list of playlisttracks for keeping List <PlaylistTrack> toKeep = exists.PlaylistTracks .Where(track => !trackstodelete.Any(ttd => track.TrackId == ttd)) .Select(tr => tr).ToList(); //stage track remove PlaylistTrack item = null; foreach (var trackID in trackstodelete) { item = exists.PlaylistTracks .Where(tr => tr.TrackId == trackID) .FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } //renumber kept tracks int number = 1; toKeep.Sort((x, y) => x.TrackNumber.CompareTo(y.TrackNumber)); foreach (var track in toKeep) { track.TrackNumber = number; //stage update context.Entry(track).Property(y => y.TrackNumber).IsModified = true; number -= -1; } //commit context.SaveChanges(); } } }//eom
}//eom public List <UserPlaylistTrack> DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { //find the parent, then the kids Playlist exists = (from x in context.Playlists where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase) select x).FirstOrDefault(); //FirstOrDefault() brings in a collection and takes the first one, or default which is null if (exists == null) { throw new Exception("Playlist has been removed"); } else { //the tracks that are to be kept var tracksKept = exists.PlaylistTracks.Where(tr => !trackstodelete.Any(tdo => tdo == tr.TrackId)).Select(tr => tr); //remove the tracks in the tracksToDelete list PlaylistTrack item = null; foreach (var deleteTrack in trackstodelete) { item = exists.PlaylistTracks.Where(dx => dx.TrackId == deleteTrack).FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } //renumber the tracks so that the track number is sequential, as is expected by all other operations //in our DB there is NO holes in the numeric sequence int number = 1; foreach (var trackKept in tracksKept) { trackKept.TrackNumber = number; context.Entry(trackKept).Property(y => y.TrackNumber).IsModified = true; number++; } context.SaveChanges(); return(List_TracksForPlaylist(playlistname, username)); } } }//eom
}//eom public List <UserPlaylistTrack> DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { //code to go here Playlist exists = (from x in context.Playlists where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Playlist has been removed from our site."); } else { //the tracks that are to be kept var trackskept = exists.PlaylistTracks.Where(tr => !trackstodelete.Any(tod => tod == tr.TrackId)).Select(tr => tr); //remove the tracks in the trackstodelete list PlaylistTrack item = null; foreach (var deletetrack in trackstodelete) { item = exists.PlaylistTracks .Where(dx => dx.TrackId == deletetrack) .FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } //renumber the kept tracks so that the tracknumber //is sequential as expected by akk other operations // In our database there is NO holes in the numeric sequence. int number = 1; foreach (var trackkept in trackskept) { trackkept.TrackNumber = number; context.Entry(trackkept).Property(y => y.TrackNumber).IsModified = true; number++; } context.SaveChanges(); return(List_TracksForPlaylist(playlistname, username)); } } }//eom
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { var exists = (from x in context.Playlists where x.UserName.Equals(username) && x.Name.Equals(playlistname) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Playlist has been removed from the system."); } else { //get a list of every track that we DONT want to delete (keep them) var trackKept = exists.PlaylistTracks .Where(tr => !trackstodelete.Any(tod => tod == tr.TrackId)) //This gets items that are in list but not in our tracks to delete. .OrderBy(tr => tr.TrackNumber) .Select(tr => tr); //remove unwanted tracks PlaylistTrack item = null; foreach (var dtrackid in trackstodelete) { item = (exists.PlaylistTracks .Where(tr => tr.TrackId == dtrackid) .Select(tr => tr)).FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } int number = 1; foreach (var tKept in trackKept) { tKept.TrackNumber = number; context.Entry(tKept).Property(y => y.TrackNumber).IsModified = true; number++; } context.SaveChanges(); } } }//eom
}//eom public List <UserPlaylistTrack> DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { //code to go here //conerned of casing "****, StringComaprison.OrdinalIgnoreCase Playlist exists = (from x in context.Playlists where x.UserName.Equals(username) && x.Name.Equals(playlistname) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Playlist has been removed from the site"); } else { //the tracks that aren't to be deleted var safetracks = exists.PlaylistTracks .Where(tr => !trackstodelete.Any(ttd => ttd == tr.TrackId)) .Select(tr => tr); //remove unwanted tracks PlaylistTrack item = null; foreach (var deletetrack in trackstodelete) { item = exists.PlaylistTracks .Where(dx => dx.TrackId == deletetrack) .FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } //renumber remaining tracks //in our database there are no holes in the numeric sequence int counter = 1; foreach (var track in safetracks) { track.TrackNumber = counter; context.Entry(track).Property(y => y.TrackNumber).IsModified = true; counter++; } context.SaveChanges(); return(List_TracksForPlaylist(playlistname, username)); } } }//eom
public int Album_Update(Album item) { using (var context = new ChinookContext()) { //additional logic if (CheckReleaseYear(item)) { item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null : item.ReleaseLabel; context.Entry(item).State = System.Data.Entity.EntityState.Modified; //staging return(context.SaveChanges()); //actual commit to database and return rowsaffected } else { throw new BusinessRuleException("Validation error", _reasons); } } }
public void UpdateTrack(Track trackinfo) { using (var context = new ChinookContext()) { //any business rules //and data refinements //review of using immediate if (iif) //comoser can be a null string, we do not want to store an empt string trackinfo.Composer = string.IsNullOrEmpty(trackinfo.Composer) ? null : trackinfo.Composer; //update the existing instance of trackinfo on the database context.Entry(trackinfo).State = System.Data.Entity.EntityState.Modified; //commit update transaction context.SaveChanges(); } }
public void UpdateTrack(Track trackInfo) { using (var context = new ChinookContext()) { //Any buisness rules //Any data refinements //Review of using iif (Imediate if) //Composer can be a null string, we do not wish to store an empty string trackInfo.Composer = string.IsNullOrEmpty(trackInfo.Composer) ? null : trackInfo.Composer; //Update the existing instance on database context.Entry(trackInfo).State = System.Data.Entity.EntityState.Modified; //Commit transaction context.SaveChanges(); } }
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookContext()) { var exists = (from x in context.Playlists where x.UserName.Equals(username) && x.Name.Equals(playlistname) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Play List has been removed from the file"); } else { //find tracks that will be kept var tracksKept = exists.PlaylistTracks.Where(tr => !trackstodelete.Any(tod => tod == tr.TrackId)).Select(tr => tr); //remove unwangted tracks PlaylistTrack item = null; foreach (var dtrackid in trackstodelete) { item = exists.PlaylistTracks.Where(tr => tr.TrackId == dtrackid).FirstOrDefault(); exists.PlaylistTracks.Remove(item); } //renumber remaining (Kept) list int number = 1; foreach (var tKept in tracksKept) { tKept.TrackNumber = number; context.Entry(tKept).Property(y => y.TrackNumber).IsModified = true; number++; } context.SaveChanges(); } } }//eom
public int Album_Update(Album item) { using (var context = new ChinookContext()) { //addition logic - treat this as validation if (CheckReleaseYear(item)) { item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null : item.ReleaseLabel; context.Entry(item).State = System.Data.Entity.EntityState.Modified; //Staging all our information is just sitting in memory return(context.SaveChanges()); //actual commit to the database } else { throw new BusinessRuleException("Validation error", reasons); } } }
public void RemovePlaylistTrack(string playlistname, int trackid, int tracknumber, int customerid) { using (var context = new ChinookContext()) { Playlist existing = (from x in context.PlayLists where x.Name.Equals(playlistname) && x.CustomerId == customerid select x).FirstOrDefault(); var tracktoremove = context.PlaylistTracks.Find(existing.PlaylistId, trackid); List <PlaylistTrack> trackskept = (from x in existing.PlaylistTracks where x.TrackNumber > tracknumber orderby x.TrackNumber select x).ToList(); context.PlaylistTracks.Remove(tracktoremove); foreach (var track in trackskept) { track.TrackNumber -= 1; context.Entry(track).Property("TrackNumber").IsModified = true; } context.SaveChanges(); } }