public void Album_Update(AlbumItem item) { using (var context = new ChinookSystemContext()) { //"item" is viewmodel object => to Entity object before update! Album updateitem = new Album { AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseYear = item.ReleaseYear, ReleaseLabel = item.ReleaseLabel }; //staging in local mem //at this point code will NOT have send to Database. context.Entry(updateitem).State = System.Data.Entity.EntityState.Modified; //commit to Database - on this command the item is shipped to entity deffinition validation then to context DB context.SaveChanges(); //entity instance will have new Pkey attached to the object } }
public void Album_Update(AlbumItem item) { using (var context = new ChinookSystemContext()) { //due to the fact we have seperaated handling of our entities //from the data traansfer beteen webapp and class library //using viewmodel classes, we must create an instaance of the entity //and move the data from the viewmodel class to the entity instance Album updateItem = new Album { //Update needs a pkey value AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseLabel = item.ReleaseLabel, ReleaseYear = item.ReleaseYear }; //Staging //Setup in local memory context.Entry(updateItem).State = System.Data.Entity.EntityState.Modified; //commit to database //on this command you // a)executee entity validtion annotation // b)send local memory staging to the database for execution //after successful execution your entity instance will have the new pkey(identity) value context.SaveChanges(); } }
public void Album_Update(AlbumItem item) { using (var context = new ChinookSystemContext()) { //Due to the fact that we have seperated the handling of our entities, from the data transfer between WebApp and Class library // using the ViewModel classes, we MUST create an instance of the entity and move the data from the ViewModel class // to the entity instance. Album updateItem = new Album { //for an update, you need to supply your pkey value AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseYear = item.ReleaseYear, ReleaseLabel = item.ReleaseLabel }; //Staging //Setup in Local Memory context.Entry(updateItem).State = System.Data.Entity.EntityState.Modified; //Commit to database //On this command, you // a) Execute entity validation annotation // b) Send your local memory staging to the database for execution context.SaveChanges(); } }
public void Album_Update(AlbumItem item) { using (var context = new ChinookSystemContext()) { //due to the fact that we have separated the handling of our entities // from the data transfer between web app and class library //using the viewmodel classes, we MUST create an instance //of the entity and move the data from the view model class to the entity instance. Album updateitem = new Album { //for an update, you need to supply a pkey value AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseYear = item.ReleaseYear, ReleaseLabel = item.ReleaseLabel }; //staging //setup in local memory context.Entry(updateitem).State = System.Data.Entity.EntityState.Modified; //commit to database //on this command you //A) execute entity validationm annotation // B) send your local memory staging to the database for execution // after a successful execution your entity instance will have the // new pkey (identity) value context.SaveChanges(); //at this point, your identity instance has the new pkey value } }
public int Album_Update(Album item) { using (var context = new ChinookSystemContext()) { context.Entry(item).State = System.Data.Entity.EntityState.Modified; return(context.SaveChanges()); //returned is number of rows affected } }
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookSystemContext()) { //trx //check to see if playlist exists //no:error msg //yes: //create a list of tracks to kept //remove the tracks in the incoming list //re-sequence the kept tracks //commit List <string> errors = new List <string>(); Playlist exists = (from x in context.Playlists where x.Name.Equals(playlistname) && x.UserName.Equals(username) select x).FirstOrDefault(); if (exists == null) { errors.Add("Play list does not exist."); } else { //find the songs to keep var trackskept = context.PlaylistTracks .Where(tr => tr.Playlist.Name.Equals(playlistname) && tr.Playlist.UserName.Equals(username) && !trackstodelete.Any(tod => tod == tr.TrackId)) .OrderBy(tr => tr.TrackNumber) .Select(tr => tr); //remove the tracks to delete PlaylistTrack item = null; foreach (int deletetrackid in trackstodelete) { item = context.PlaylistTracks .Where(tr => tr.Playlist.Name.Equals(playlistname) && tr.Playlist.UserName.Equals(username) && tr.TrackId == deletetrackid) .Select(tr => tr).FirstOrDefault(); if (item != null) { exists.PlaylistTracks.Remove(item); } } //re-sequence int number = 1; foreach (var track in trackskept) { track.TrackNumber = number; context.Entry(track).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; number++; } //commit context.SaveChanges(); } } }//eom
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookSystemContext()) { //code to go here // find playlist var exists = (from x in context.Playlists where x.UserName.Equals(username) && x.Name.Equals(playlistname) select x).FirstOrDefault(); // check if (exists == null) { //null - msg throw new Exception("Playlist has been removed from the file."); } else { // exists // find the tracks that will be kept // these tracks will be in set A but not in set B var tracksKept = exists.PlaylistTracks .Where(tr => !trackstodelete.Any(td => td == tr.TrackId)); // remove the unwanted tracks from the database PlaylistTrack item = null; foreach (var dtrackid in trackstodelete) { // find track instance to delete item = exists.PlaylistTracks.Where(tr => tr.TrackId == dtrackid).Select(tr => tr).FirstOrDefault(); // check if (item != null) { // if its there then remove exists.PlaylistTracks.Remove(item); } } // renumber the tracks that were kept by updating their // track numbers 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 void Album_Update(AlbumItem item) { using (var context = new ChinookSystemContext()) { Album updateItem = new Album() { AlbumId = item.AlbumId, Title = item.Title, ReleaseYear = item.ReleaseYear, ArtistId = item.ArtistId, ReleaseLabel = item.ReleaseLabel }; context.Entry(updateItem).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void Album_Update(AlbumItem item) { using (var context = new ChinookSystemContext()) { Album newItem = new Album { //pkey is identity, not needed AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseLabel = item.ReleaseLabel, ReleaseYear = item.ReleaseYear }; context.Entry(newItem).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void Albums_Update(AlbumViewModel item) { DataValidation(item); using (var context = new ChinookSystemContext()) { Album info = new Album() { AlbumId = item.AlbumId, Title = item.AlbumTitle, ArtistId = item.ArtistId, ReleaseYear = item.AlbumReleaseYear, ReleaseLabel = item.AlbumReleaseLabel }; context.Entry(info).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void Tracks_Update(TrackViewModel item) { using (var context = new ChinookSystemContext()) { Track info = new Track() { TrackId = item.TrackId, MediaTypeId = item.TrackMediaTypeId, Name = item.TrackName, Composer = item.TrackComposer, Milliseconds = item.TrackMilliseconds, Bytes = item.TrackBytes, UnitPrice = item.TrackUnitPrice }; context.Entry(info).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void Album_Update(AlbumList item) { using (var context = new ChinookSystemContext()) { //moving the data from the external ViewModel instance // into an internal instance of the entity Album updateItem = new Album { AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseYear = item.ReleaseYear, ReleaseLabel = item.ReleaseLabel }; context.Entry(updateItem).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void Album_Update(AlbumList item) { using (var context = new ChinookSystemContext()) { //moving the data from the external viewmodel instance into an internal instance of the entity Album updateItem = new Album { //Remember for an update, I need to supply the primary key AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseYear = item.ReleaseYear, ReleaseLabel = item.ReleaseLabel }; context.Entry(updateItem).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void Album_Update(AlbumItem item) { using (var context = new ChinookSystemContext()) { //move the incoming viewmodel instance data into an instance // of the internal entity Album newItem = new Album { //pkey is needed for update to find the instance // on the database AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseYear = item.ReleaseYear, ReleaseLabel = item.ReleaseLabel }; context.Entry(newItem).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void Albums_Update(AlbumItem item) { using (var context = new ChinookSystemContext()) { //need to move the data from the viewmodel class into // an entity instance BEFORE staging //on update you NEED to supply the table's pkey value Album entityItem = new Album { AlbumId = item.AlbumId, Title = item.Title, ArtistId = item.ArtistId, ReleaseYear = item.ReleaseYear, ReleaseLabel = item.ReleaseLabel }; //stagging is to local memory context.Entry(entityItem).State = System.Data.Entity.EntityState.Modified; //commit is the action of sending your request to // the database for action. context.SaveChanges(); } }
}//eom public void MoveTrack(string username, string playlistname, int trackid, int tracknumber, string direction) { using (var context = new ChinookSystemContext()) { //code to go here // since data can be accessed by multiple individuals at the same time // your BLL method should do validation to ensure the data coming is appropriate var exists = context.Playlists.Where(x => x.UserName.Equals(username) && x.Name.Equals(playlistname)).Select(x => x).FirstOrDefault(); // playlist no longer exists if (exists == null) { throw new Exception("Playlist has been removed from files."); } else { var movetrack = exists.PlaylistTracks.Where(x => x.TrackId == trackid).Select(x => x).FirstOrDefault(); //playlist track no longer exists if (movetrack == null) { throw new Exception("Playlist track has been removed from files - Movetrack."); } else { PlaylistTrack othertrack = null; // determine direction if (direction.Equals("up")) { if (movetrack.TrackNumber == 1) { throw new Exception("Playlist track already at top."); } else { //setup for track movement othertrack = (from x in exists.PlaylistTracks where x.TrackNumber == movetrack.TrackNumber - 1 select x).FirstOrDefault(); if (othertrack == null) { throw new Exception("Playlist tracks have been altered. Unable to Complete move"); } else { movetrack.TrackNumber -= 1; othertrack.TrackNumber += 1; } } } else { if (movetrack.TrackNumber == exists.PlaylistTracks.Count) { throw new Exception("Playlist track already at last."); } else { //setup for track movement othertrack = (from x in exists.PlaylistTracks where x.TrackNumber == movetrack.TrackNumber + 1 select x).FirstOrDefault(); if (othertrack == null) { throw new Exception("Playlist tracks have been altered. Unable to Complete move"); } else { movetrack.TrackNumber += 1; othertrack.TrackNumber -= 1; } } } //staging //update context.Entry(movetrack).Property(y => y.TrackNumber).IsModified = true; context.Entry(othertrack).Property(y => y.TrackNumber).IsModified = true; //commit context.SaveChanges(); } } } }//eom
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { //early var placeholders Playlist playlistExist = null; PlaylistTrack playlistTrackExist = null; int tracknumber = 0; using (var context = new ChinookSystemContext()) { if (string.IsNullOrEmpty(playlistname)) { //no playlist name supplied -error out //BusinessRuleException -- part of freecode -import brokenrules.Add(new BusinessRuleException <string>("playlist name is missing. unable to delete track", nameof(playlistname), playlistname)); //nameof not requires if specific } if (string.IsNullOrEmpty(username)) { //no playlist name supplied -error out //BusinessRuleException -- part of freecode -import brokenrules.Add(new BusinessRuleException <string>("username name is missing. unable to delete track", "username", username)); } if (trackstodelete.Count == 0) { //no playlist name supplied -error out //BusinessRuleException -- part of freecode -import brokenrules.Add(new BusinessRuleException <string>("tracks to delete. unable to delete track", "track count", trackstodelete.Count.ToString())); } playlistExist = (from x in context.Playlists where x.Name == playlistname && x.UserName == username select x).FirstOrDefault(); if (playlistExist == null) { //broken rule brokenrules.Add(new BusinessRuleException <string>("playlist no exist", nameof(playlistname), playlistname)); } else { //good data //list all tracks that are to be kept //compare lists of delete to list of kept var trackskept = from x in context.PlaylistTracks where x.Playlist.Name == playlistname && x.Playlist.UserName == username && !trackstodelete.Any(tod => tod == x.TrackId) orderby x.TrackNumber select x; //remove the desired track refrences PlaylistTrack item = null; foreach (var deleterecord in trackstodelete) { item = (from x in context.PlaylistTracks where x.Playlist.Name == playlistname && x.Playlist.UserName == username && x.TrackId == deleterecord orderby x.TrackNumber select x).FirstOrDefault(); if (item != null) { //delete //stage via parent.nav.remove() playlistExist.PlaylistTracks.Remove(item); } } //resequence the kept tracks // option - use a list and update the records of the list //option - delete all children records and re-add only the new kept records list tracknumber = 1; foreach (var track in trackskept) { track.TrackNumber = tracknumber; //stage context.Entry(track).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; tracknumber++; } //commit if (brokenrules.Count > 0) { throw new BusinessRuleCollectionException("Track removal concerns", brokenrules); } else { context.SaveChanges(); } } } }//eom
}//eom public void MoveTrack(MoveTrackItem movetrack) { using (var context = new ChinookSystemContext()) { //inital value errorchecking if (string.IsNullOrEmpty(movetrack.PlaylistName)) { //no playlist name supplied -error out //BusinessRuleException -- part of freecode -import brokenrules.Add(new BusinessRuleException <string>("playlist name is missing. unable to delete track", nameof(movetrack.PlaylistName), movetrack.PlaylistName)); //nameof not requires if specific } if (string.IsNullOrEmpty(movetrack.UserName)) { //no playlist name supplied -error out //BusinessRuleException -- part of freecode -import brokenrules.Add(new BusinessRuleException <string>("username name is missing. unable to delete track", "username", movetrack.UserName)); } if (movetrack.TrackID <= 0) { brokenrules.Add(new BusinessRuleException <string>("trackID no bueno", "trackID", movetrack.TrackID.ToString())); } if (movetrack.TrackNumber <= 0) { brokenrules.Add(new BusinessRuleException <string>("trackID no bueno", "track Number", movetrack.TrackNumber.ToString())); } Playlist exist = (from x in context.Playlists where x.Name == movetrack.PlaylistName && x.UserName == movetrack.UserName select x).FirstOrDefault(); if (exist == null) { //broken rule brokenrules.Add(new BusinessRuleException <string>("playlist no exist", nameof(movetrack.PlaylistName), movetrack.PlaylistName)); } else { //check if track exists on the database PlaylistTrack trackexists = (from x in context.PlaylistTracks where x.Playlist.Name == movetrack.PlaylistName && x.Playlist.UserName == movetrack.UserName && x.TrackId == movetrack.TrackID select x).FirstOrDefault(); if (trackexists == null) { //broken rule brokenrules.Add(new BusinessRuleException <string>("playlist track no exist", nameof(movetrack.TrackID), movetrack.TrackNumber.ToString())); //should lookup name. } else { //move it up or move it down if (movetrack.Direction == "up") { //up //check to see if at top - != 1 if (trackexists.TrackNumber != 1) { // do the move //get adjacent track PlaylistTrack othertrack = (from x in context.PlaylistTracks where x.Playlist.Name == movetrack.PlaylistName && x.Playlist.UserName == movetrack.UserName && x.TrackNumber == trackexists.TrackNumber - 1 select x).FirstOrDefault(); //check other track exists if (othertrack != null) { //good to swap //swap is a matter of changing the track Number Values trackexists.TrackNumber--; othertrack.TrackNumber++; //stage context.Entry(trackexists).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; context.Entry(othertrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; } else { //broken rule brokenrules.Add(new BusinessRuleException <string>("track to swap is missing", nameof(othertrack.Track.Name), othertrack.Track.Name)); } } else { //broken rule brokenrules.Add(new BusinessRuleException <string>("playlist track already #1, refresh display", nameof(movetrack.PlaylistName), trackexists.Track.Name)); } } else { int trackcountonexist = (from x in context.Playlists where x.Name == movetrack.PlaylistName && x.UserName == movetrack.UserName select x).FirstOrDefault().PlaylistTracks.ToList().Count(); trackcountonexist++; //lazy loader bullshi...... //down //up //check to see if at Bottom - != count of the existing playlist if (trackexists.TrackNumber != trackcountonexist--) { // do the move //get adjacent track PlaylistTrack othertrack = (from x in context.PlaylistTracks where x.Playlist.Name == movetrack.PlaylistName && x.Playlist.UserName == movetrack.UserName && x.TrackNumber == trackexists.TrackNumber + 1 select x).FirstOrDefault(); //check other track exists if (othertrack != null) { //good to swap //swap is a matter of changing the track Number Values trackexists.TrackNumber++; othertrack.TrackNumber--; //stage context.Entry(trackexists).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; context.Entry(othertrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; } else { //broken rule brokenrules.Add(new BusinessRuleException <string>("track to swap is missing", nameof(othertrack.Track.Name), othertrack.Track.Name)); } } else { //broken rule brokenrules.Add(new BusinessRuleException <string>("playlist track already #1, refresh display", nameof(movetrack.PlaylistName), trackexists.Track.Name)); } } } } //resequence the kept tracks // option - use a list and update the records of the list //option - delete all children records and re-add only the new kept records list //commit if (brokenrules.Count > 0) { throw new BusinessRuleCollectionException("Track movement concerns", brokenrules); } else { context.SaveChanges(); } } }//eom
} //eom public void MoveTrack(string username, string playlistname, int trackid, int tracknumber, string direction) { using (var context = new ChinookSystemContext()) { //code to go here //check to see if the playlist exists //no:error exception //yes: ////up:check if on the top /////yes:error exception /////no:find record above(tracknumber -1) /////above record tracknumber modified to tracknumber +1 /////selected tracknumber modified to tracknumber-1 ///////down:check if on the bottom /////yes:error exception /////no:find record below(tracknumber +1) /////below record tracknumber modified to tracknumber -1 /////selected tracknumber modified to tracknumber+1 //stage record update //commit List <string> errors = new List <string>();//use for businessrule exception PlaylistTrack moveTrack = null; PlaylistTrack otherTrack = null; Playlist exists = (from x in context.Playlists where x.Name.Equals(playlistname) && x.UserName.Equals(username) select x).FirstOrDefault(); if (exists == null) { errors.Add("Playlist does not exist."); } else { moveTrack = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) && x.TrackId == trackid select x).FirstOrDefault(); if (moveTrack == null) { errors.Add("Playlist track does not exist"); } else { if (direction.Equals("up")) { //this means the tracknumber of the selected track will decrease //prep for move, check if the track is at the top of the list if (moveTrack.TrackNumber == 1) { errors.Add("Song on play list already at the top."); } else { otherTrack = (from x in context.PlaylistTracks where x.TrackNumber == (moveTrack.TrackNumber - 1) && x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) select x).FirstOrDefault(); if (otherTrack == null) { errors.Add("Missing required other song track record."); } else { moveTrack.TrackNumber -= 1; otherTrack.TrackNumber += 1; } } } else { if (moveTrack.TrackNumber == exists.PlaylistTracks.Count) { errors.Add("Song on play list already at the bottom."); } else {//4=>5 otherTrack = (from x in context.PlaylistTracks where x.TrackNumber == (tracknumber + 1) && x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) select x).FirstOrDefault(); if (otherTrack == null) { errors.Add("Missing required other song track record."); } else { moveTrack.TrackNumber += 1; otherTrack.TrackNumber -= 1; } } } } } if (errors.Count > 0) { throw new BusinessRuleException("Move Track", errors); } else { //stage //1)you can stage an update to alter the entire entity(CRUD) //2)You can stage an update to an entity referencing JUST the property to be modified //in this example (b) will be used context.Entry(moveTrack).Property("TrackNumber").IsModified = true; context.Entry(otherTrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; context.SaveChanges(); } } }//eom
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookSystemContext()) { Playlist playlistExists = null; int tracknumber = 0; if (string.IsNullOrEmpty(playlistname)) { //there is a data error //setting up an error message: brokenRules.Add(new BusinessRuleException <string>("Playlist name is missing. Unable to add track.", nameof(playlistname), playlistname)); } if (string.IsNullOrEmpty(username)) { //there is a data error //setting up an error message: brokenRules.Add(new BusinessRuleException <string>("User name is missing. Unable to remove track(s)", "User Name", username)); } if (trackstodelete.Count == 0) { //there is a data error //setting up an error message: brokenRules.Add(new BusinessRuleException <int>("Playlist name is missing. Unable to remove track(s)", "Track list count", 0)); } playlistExists = (from x in context.Playlists where x.Name.Equals(playlistname) && x.UserName.Equals(username) select x).FirstOrDefault(); if (playlistExists == null) { brokenRules.Add(new BusinessRuleException <string>("Playlist does not exist.", nameof(playlistname), playlistname)); } else { //list of all tracks that are to be kept var trackskept = context.PlaylistTracks .Where(x => x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) && !trackstodelete.Any(tod => tod == x.TrackId)) .OrderBy(x => x.TrackNumber) .Select(x => x); //remove the desired tracks PlaylistTrack item = null; foreach (var deleterecord in trackstodelete) //tracksids to delete { //getting a single record item = context.PlaylistTracks .Where(x => x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) && x.TrackId == deleterecord) .Select(x => x).FirstOrDefault(); //delete //stage (parent.navproperty.Remove() if (item != null) { playlistExists.PlaylistTracks.Remove(item); } } //re-sequence the kept tracks //Option A) use a list and update the records of the list. //Option B) delete all children records and re-add only the necessary kept records. //within this example, you will see how to update specific column(s) of a record. (OptionA) tracknumber = 1; foreach (var track in trackskept) { track.TrackNumber = tracknumber; //Stage the update context.Entry(track).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; tracknumber++; } } //commit? if (brokenRules.Count > 0) { throw new BusinessRuleCollectionException("Track Removal Concerns:", brokenRules); } else { context.SaveChanges(); } } }//eom
}//eom public void MoveTrack(MoveTrackItem movetrack) { int numberoftracks = 0; using (var context = new ChinookSystemContext()) { if (string.IsNullOrEmpty(movetrack.PlaylistName)) { //there is a data error //setting up an error message brokenRules.Add(new BusinessRuleException <string>("Playlist name is missing. Unable to remove track(s)", "Playlist Name", movetrack.PlaylistName)); } if (string.IsNullOrEmpty(movetrack.UserName)) { //there is a data error //setting up an error message brokenRules.Add(new BusinessRuleException <string>("User name is missing. Unable to remove track(s)", "User Name", movetrack.UserName)); } if (movetrack.TrackID <= 0) { brokenRules.Add(new BusinessRuleException <int>("Invalid track identifier. Unable to remove track(s)", "Track Identifier", movetrack.TrackID)); } if (movetrack.TrackNumber <= 0) { brokenRules.Add(new BusinessRuleException <int>("Invalid track number. Unable to remove track(s)", "Track Number", movetrack.TrackNumber)); } Playlist exist = (from x in context.Playlists where x.Name.Equals(movetrack.PlaylistName) && x.UserName.Equals(movetrack.UserName) select x).FirstOrDefault(); if (exist == null) { brokenRules.Add(new BusinessRuleException <string>("Playlist does not exist.", nameof(MoveTrackItem.PlaylistName), movetrack.PlaylistName)); } else { //due to the way that LInq executes in your program as a "lazy loader" //we need to query directly the number of tracks in the playlist numberoftracks = (context.PlaylistTracks .Where(x => x.Playlist.Name.Equals(movetrack.PlaylistName) && x.Playlist.UserName.Equals(movetrack.UserName)) .Select(x => x)).Count(); //check to see if the desired track exists on the database PlaylistTrack trackexist = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(movetrack.PlaylistName) && x.Playlist.UserName.Equals(movetrack.UserName) && x.TrackId == movetrack.TrackID select x).FirstOrDefault(); if (trackexist == null) { brokenRules.Add(new BusinessRuleException <string>("Playlist track does not exist.", nameof(MoveTrackItem.PlaylistName), movetrack.PlaylistName)); } else { //decide the logic depending on direction if (movetrack.Direction.Equals("up")) { //up //not at top if (trackexist.TrackNumber == 1) { brokenRules.Add(new BusinessRuleException <string>("Playlist track already at the top. Refresh your display.", nameof(Track.Name), trackexist.Track.Name)); } else { //do the move //get the adjacent track PlaylistTrack othertrack = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(movetrack.PlaylistName) && x.Playlist.UserName.Equals(movetrack.UserName) && x.TrackNumber == trackexist.TrackNumber - 1 select x).FirstOrDefault(); if (othertrack == null) { brokenRules.Add(new BusinessRuleException <string>("Playlist track to swap seems to be missing. Refresh your display.", nameof(MoveTrackItem.PlaylistName), movetrack.PlaylistName)); } else { //good to swap //the swap is a matter of changing the tracknumber values trackexist.TrackNumber -= 1; othertrack.TrackNumber += 1; //staging context.Entry(trackexist).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; context.Entry(othertrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; } } } else { //down //not at bottom if (trackexist.TrackNumber == numberoftracks) { brokenRules.Add(new BusinessRuleException <string>("Playlist track already at the bottom. Refresh your display.", nameof(Track.Name), trackexist.Track.Name)); } else { //do the move //get the adjacent track PlaylistTrack othertrack = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(movetrack.PlaylistName) && x.Playlist.UserName.Equals(movetrack.UserName) && x.TrackNumber == trackexist.TrackNumber + 1 select x).FirstOrDefault(); if (othertrack == null) { brokenRules.Add(new BusinessRuleException <string>("Playlist track to swap seems to be missing. Refresh your display.", nameof(MoveTrackItem.PlaylistName), movetrack.PlaylistName)); } else { //good to swap //the swap is a matter of changing the tracknumber values trackexist.TrackNumber += 1; othertrack.TrackNumber -= 1; //staging context.Entry(trackexist).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; context.Entry(othertrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; } } } } } //commit? if (brokenRules.Count > 0) { throw new BusinessRuleCollectionException("Track Movement Concerns:", brokenRules); } else { context.SaveChanges(); } } }//eom
} //eom public void MoveTrack(string username, string playlistname, int trackid, int tracknumber, string direction) { using (var context = new ChinookSystemContext()) { //trx //check to see if the playlist exsits //no: error exception //yes: // check to see if song exists // no: error exception // yes: // up // check to see if the song is at the top // yes: error exception // no: // move record 4; above record 3 // find record above (tracknumber -1) // change above tracknumber (increment) // move track tracknumber (decrement) // down // check to see if the song is at the bottom // yes: error exception // no: // move record 4; below record 5 // find record below (tracknumber +1) // change below tracknumber (decrement) // move track tracknumber (increment) //can I stage/commit //no: throw the exception (BRE) //yes: // stage updates // commit PlaylistTrack moveTrack = null; PlaylistTrack otherTrack = null; List <string> errors = new List <string>(); // this is to be used by BusinessRuleException cast //.FirstOrDefault() returns the first instance matching the criteria or null Playlist exists = (from x in context.Playlists where x.Name.Equals(playlistname) && x.UserName.Equals(username) select x).FirstOrDefault(); if (exists == null) { errors.Add("Play list does not exist"); } else { moveTrack = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) && x.TrackId == trackid select x).FirstOrDefault(); if (moveTrack == null) { errors.Add("Play list track does not exist"); } else { if (direction.Equals("up")) { //up //this means the tracknumber on the selected track // will decrease (track 4 -> 3) //prep for move, check if the track is at the top if (moveTrack.TrackNumber == 1) { errors.Add("Play list track already at the top."); } else { //manipulate the actal records otherTrack = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) && x.TrackNumber == (moveTrack.TrackNumber - 1) select x).FirstOrDefault(); if (otherTrack == null) { errors.Add("Missng required othe song track record."); } else { moveTrack.TrackNumber -= 1; otherTrack.TrackNumber += 1; } } } else { //down //this means the tracknumber on the selected track // will increase (track 4 -> 5) //prep for move, check if the track is at the bottom int songcount = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) select x).Count(); if (moveTrack.TrackNumber == songcount) { errors.Add("Play list track already at the bottom."); } else { //manipulate the actal records otherTrack = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) && x.TrackNumber == (moveTrack.TrackNumber + 1) select x).FirstOrDefault(); if (otherTrack == null) { errors.Add("Missng required othe song track record."); } else { moveTrack.TrackNumber += 1; otherTrack.TrackNumber -= 1; } } } } } //check to see if an error has occured, if so throw if (errors.Count > 0) { throw new BusinessRuleException("Move Track", errors); } else { //stage //these are updates //a) you can stage an update to alter the entire entity (CRUD) //b) you can stage an update to an entity referencing JUST the property // to be modified //in this example do stage (b) context.Entry(moveTrack).Property("TrackNumber").IsModified = true; context.Entry(otherTrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; //commit context.SaveChanges(); } } }//eom
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { using (var context = new ChinookSystemContext()) { //trx //check to see if the playlist exists // no: msg // yes: // create a list of tracks to kept (need to know which records to renumber) // remove the tracks in the incoming list (multiple deletes) // re-sequence the kept tracks (update) // commit List <string> errors = new List <string>(); // this is to be used by BusinessRuleException cast //.FirstOrDefault() returns the first instance matching the criteria or null Playlist exists = (from x in context.Playlists where x.Name.Equals(playlistname) && x.UserName.Equals(username) select x).FirstOrDefault(); if (exists == null) { errors.Add("Play list does not exist"); } else { //find tracks to keep //used .Any technique of list a vs list b //could this also be done with .Exclude() var trackskept = context.PlaylistTracks .Where(tr => tr.Playlist.Name.Equals(playlistname) && tr.Playlist.UserName.Equals(username) && !trackstodelete.Any(tod => tod == tr.TrackId)) .OrderBy(tr => tr.TrackNumber) .Select(tr => tr); //remove the tracks listed in trackstodelete PlaylistTrack item = null; foreach (int deletetrackid in trackstodelete) { item = context.PlaylistTracks .Where(tr => tr.Playlist.Name.Equals(playlistname) && tr.Playlist.UserName.Equals(username) && tr.TrackId == deletetrackid) .Select(tr => tr).FirstOrDefault(); if (item != null) { //stage the removal exists.PlaylistTracks.Remove(item); } } //re-sequence //don't care if the remaining tracknumbers are 1,4,5,8,10 //renumber 1(1),2(4),3(5),4(8),5(10) int number = 1; foreach (var track in trackskept) { track.TrackNumber = number; //stage update context.Entry(track).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; number++; } } if (errors.Count > 0) { throw new BusinessRuleException("Remove Tracks", errors); } else { context.SaveChanges(); } } }//eom
} //eom public void MoveTrack(string username, string playlistname, int trackid, int tracknumber, string direction) { using (var context = new ChinookSystemContext()) { //trx //check to see if the playlist exists //no: error exception //yes: // Check to see if song exists // no: error exception // yes: //// //up: //// //check to see if song is at the top //// //yes: error exception //// // no: //// //find record above (tracknumber-1 //// //change above record tracknumber modified to tracknumber + 1 //// //selected record tracnumber modified to tracknumbere - 1 //// //down: //// //check to see if song is at the botom //// //yes: error exception //// //no: //// //find record above (tracknumber+1 //// //change above record tracknumber modified to tracknumber - 1 //// //selected record tracnumber modified to tracknumbere + 1 ////stage records //commit PlaylistTrack moveTrack = null; PlaylistTrack otherTrack = null; List <string> errors = new List <string>(); //for use by BusinessRuleException Playlist exists = (from x in context.Playlists where x.Name.Equals(playlistname) && x.UserName.Equals(username) select x).FirstOrDefault(); //if not if (exists == null) { errors.Add("Playlist does not exist"); } else { moveTrack = (from x in context.PlaylistTracks where x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) && x.TrackId == trackid select x).FirstOrDefault(); if (moveTrack == null) { errors.Add("Playlist track does not exist"); } else { if (direction.Equals("up")) //Move Uo { //this means the track number of the selectred track // will decrease (track 4 => 3) //preparation for move, check if the track is at the top of the list if (moveTrack.TrackNumber == 1) { errors.Add("song on playlist already at the top"); } else { //Manipulation of the actual records //the following test conditions identify the playlistID value // x.Playlist.Name.Equals(playlistname) // x.Playlist.UserName.Equals(username) otherTrack = (from x in exists.PlaylistTracks where x.TrackNumber == tracknumber - 1 && x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) select x).FirstOrDefault(); if (otherTrack == null) { errors.Add("Missing required other song record."); } else { moveTrack.TrackNumber -= 1; otherTrack.TrackNumber += 1; } } } else //Move Down { if (moveTrack.TrackNumber == exists.PlaylistTracks.Count) { errors.Add("song on playlist already at the bottom"); } else { //Manipulation of the actual records //(track 4->5) otherTrack = (from x in exists.PlaylistTracks where x.TrackNumber == tracknumber + 1 && x.Playlist.Name.Equals(playlistname) && x.Playlist.UserName.Equals(username) select x).FirstOrDefault(); if (otherTrack == null) { errors.Add("Missing required other song record."); } else { moveTrack.TrackNumber += 1; otherTrack.TrackNumber -= 1; } } } } } if (errors.Count > 0) { throw new BusinessRuleException("Move Track", errors); } else { //stage changes //a)you can stage an update to alter the entire entity //b) you can stage an update to an entity referencing JUST the property // to be modified //in this example B will be used context.Entry(moveTrack).Property("TrackNumber").IsModified = true; context.Entry(otherTrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; //commit context.SaveChanges(); } } }//eom
}//eom public void DeleteTracks(string username, string playlistname, List <int> trackstodelete) { Playlist playlistExist = null; //PlaylistTrack playlisttrackExist = null; int trackNumber = 0; using (var context = new ChinookSystemContext()) { //code to go here if (string.IsNullOrEmpty(playlistname)) { //there is a data error //setting up an error message //brokenRules.Add(new BusinessRuleException<datatype>("my message", //nameof("fieldnameinerror"), fieldnameinerror)); brokenRules.Add(new BusinessRuleException <string>("playlist name is missing, Unable to add track", "Playlist Name", playlistname)); } if (string.IsNullOrEmpty(username)) { brokenRules.Add(new BusinessRuleException <string>("username is missing, Unable to add a track", "User Name", username)); } if (trackstodelete.Count == 0) { brokenRules.Add(new BusinessRuleException <int>("No tracks we selected. Unable to remove track(s)", "Track list count", 0)); } playlistExist = context.Playlists.Where(x => x.Name.Equals(playlistname) && x.UserName.Equals(username)).FirstOrDefault(); if (playlistExist == null) { brokenRules.Add(new BusinessRuleException <string>("playlist does not exist", nameof(playlistname), playlistname)); } else { //list of all tracks to be kept var trackskept = context.PlaylistTracks.Where(x => x.Playlist.UserName.Equals(username) && x.Playlist.Name.Equals(trackNumber) && !trackstodelete.Any(tod => tod == x.TrackId)).OrderBy(x => x.TrackNumber).Select(x => x).ToList(); //remove the desired tracks PlaylistTrack item = null; foreach (var deleteRecord in trackstodelete) { item = context.PlaylistTracks.Where(x => x.Playlist.UserName.Equals(username) && x.Playlist.Name.Equals(trackNumber) && x.TrackId == deleteRecord).FirstOrDefault(); //delete //stage (parent.navproperty.remove) if (item != null) { playlistExist.PlaylistTracks.Remove(item); } } //re-sequence the list //option a) use a list and update the records of the list //option b) to delete all children records and re add onl the necessary kept records //Within this example, you'll see an update specific columns of a records(option a) trackNumber = 1; foreach (var track in trackskept) { track.TrackNumber = trackNumber; //stage a single field to be updated context.Entry(track).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; trackNumber++; } } //commit? if (brokenRules.Count > 0) { throw new BusinessRuleCollectionException("Track Removal concerns: ", brokenRules); } else { context.SaveChanges(); } } }//eom
}//eom public void MoveTrack(MoveTrackItem moveTrack) { using (var context = new ChinookSystemContext()) { //code to go here if (string.IsNullOrEmpty(moveTrack.PlaylistName)) { //there is a data error //setting up an error message //brokenRules.Add(new BusinessRuleException<datatype>("my message", //nameof("fieldnameinerror"), fieldnameinerror)); brokenRules.Add(new BusinessRuleException <string>("playlist name is missing, Unable to add track", "Playlist Name", moveTrack.PlaylistName)); } if (string.IsNullOrEmpty(moveTrack.UserName)) { brokenRules.Add(new BusinessRuleException <string>("username is missing, Unable to add a track", "User Name", moveTrack.UserName)); } if (moveTrack.TrackId <= 0) { brokenRules.Add(new BusinessRuleException <int>("Invalid track identifier, Unable to move a track", "Track Identifier", moveTrack.TrackId)); } if (moveTrack.TrackNumber <= 0) { brokenRules.Add(new BusinessRuleException <int>("Invalid track number, Unable to move a track(s)", "Track Number", moveTrack.TrackNumber)); } Playlist exist = context.Playlists.Where(x => x.Name.Equals(moveTrack.PlaylistName) && x.UserName.Equals(moveTrack.UserName)).FirstOrDefault(); if (exist == null) { brokenRules.Add(new BusinessRuleException <string>("playlist does not exist", nameof(MoveTrackItem.PlaylistName), moveTrack.PlaylistName)); } else { //list of all tracks to be kept //Chec PlaylistTrack trackExist = context.PlaylistTracks.Where(x => x.Playlist.Name.Equals(moveTrack.PlaylistName) && x.TrackId == moveTrack.TrackId && x.Playlist.UserName.Equals(moveTrack.UserName) && x.TrackNumber == moveTrack.TrackNumber).FirstOrDefault(); if (trackExist == null) { brokenRules.Add(new BusinessRuleException <string>("track name does not exist", "Track Name", moveTrack.PlaylistName)); } else { if (moveTrack.Direction.Equals("up")) { if (trackExist.TrackNumber == 1) { brokenRules.Add(new BusinessRuleException <string>("playlist trac k already on the top.Refresh your display", nameof(Track.Name), trackExist.Track.Name)); } else { //do tthe moeve //get the adajacent track PlaylistTrack otherTrack = context.PlaylistTracks.Where(x => x.Playlist.Name.Equals(moveTrack.PlaylistName) && x.Playlist.UserName.Equals(moveTrack.UserName) && x.TrackNumber == (trackExist.TrackNumber - 1)).Select(x => x).FirstOrDefault(); if (otherTrack == null) { brokenRules.Add(new BusinessRuleException <string>("playlist to swap seems to be missing.Refresh your display", nameof(MoveTrackItem.PlaylistName), moveTrack.PlaylistName)); } else { //good to swap //the swap is a mattter of changing track number value trackExist.TrackNumber--; otherTrack.TrackNumber++; //staging context.Entry(trackExist).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; context.Entry(otherTrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; } } } else { //down if (moveTrack.Direction.Equals("down")) { var play = context.PlaylistTracks.Where(x => x.Playlist.Name.Equals(moveTrack.PlaylistName) && x.Playlist.UserName.Equals(moveTrack.UserName)).Count(); if (trackExist.TrackNumber == play) { brokenRules.Add(new BusinessRuleException <string>("playlist track already on the bottom.Refresh your display", nameof(Track.Name), trackExist.Track.Name)); } else { //do tthe moeve //get the adajacent track PlaylistTrack otherTrack = context.PlaylistTracks.Where(x => x.Playlist.Name.Equals(moveTrack.PlaylistName) && x.Playlist.UserName.Equals(moveTrack.UserName) && x.TrackNumber == (trackExist.TrackNumber + 1)).Select(x => x).FirstOrDefault(); if (otherTrack == null) { brokenRules.Add(new BusinessRuleException <string>("playlist to swap seems to be missing.Refresh your display", nameof(MoveTrackItem.PlaylistName), moveTrack.PlaylistName)); } else { //good to swap //the swap is a mattter of changing track number value trackExist.TrackNumber++; otherTrack.TrackNumber--; //staging context.Entry(trackExist).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; context.Entry(otherTrack).Property(nameof(PlaylistTrack.TrackNumber)).IsModified = true; } } } } } //remove the desired tracks if (brokenRules.Count > 0) { throw new BusinessRuleCollectionException("Track Move concerns: ", brokenRules); } else { context.SaveChanges(); } } } }//eom