protected void MoveTrack(MoveTrackItem movetrack) { //call BLL to move track string username = "******"; //until security movetrack.UserName = username; movetrack.PlaylistName = PlaylistName.Text; MessageUserControl.TryRun(() => { PlaylistTracksController sysmgr = new PlaylistTracksController(); sysmgr.MoveTrack(movetrack); RefreshPlaylist(sysmgr, username); }, "movement success", "good job moving that little guy, it looks so good"); }
protected void MoveTrack(MoveTrackItem movetrack) { string username = "******"; //until security is implemented movetrack.UserName = username; movetrack.PlaylistName = PlaylistName.Text; MessageUserControl.TryRun(() => { PlaylistTracksController sysmgr = new PlaylistTracksController(); sysmgr.MoveTrack(movetrack); RefreshPlayList(sysmgr, username); }, "Track Movement", "Track has been moved."); }
protected void MoveTrack(MoveTrackItem moveTrack) { //call BLL to move track string username = "******"; moveTrack.UserName = username; moveTrack.PlaylistName = PlaylistName.Text; MessageUserControl.TryRun(() => { PlaylistTracksController sysmgr = new PlaylistTracksController(); sysmgr.MoveTrack(moveTrack); RefreshPlayList(sysmgr, username); }, "Track Movement", "Track moved successfully"); }
}//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(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
protected void MoveUp_Click(object sender, EventArgs e) { //code to go here if (string.IsNullOrEmpty(PlaylistName.Text)) { MessageUserControl.ShowInfo("Missing Data", "Enter a playlist name"); } else { if (PlayList.Rows.Count == 0) { MessageUserControl.ShowInfo("Track Movement", "You must have a play list visible to choose tracks for movement. Select fromt he displayed playlist."); } else { MoveTrackItem moveTrack = new MoveTrackItem(); int rowsSelected = 0; CheckBox trackSelection = null; //traverse the gridview control PlayList //you could do this same code using a foreach() for (int i = 0; i < PlayList.Rows.Count; i++) { //point to the checkbox control on the gridview row trackSelection = PlayList.Rows[i].FindControl("Selected") as CheckBox; //test the setting of the checkbox if (trackSelection.Checked) { rowsSelected++; moveTrack.TrackID = int.Parse((trackSelection.FindControl("TrackId") as Label).Text); moveTrack.TrackNumber = int.Parse((trackSelection.FindControl("TrackNumber") as Label).Text); } } //was a single song selected? switch (rowsSelected) { case 0: { MessageUserControl.ShowInfo("Track Movement", "You must select one song to remove."); break; } case 1: { //rule: do not move if last song if (moveTrack.TrackNumber == 1) { MessageUserControl.ShowInfo("Track Movement", "Song selected is already the first song. Moving up not necessary."); } else { moveTrack.Direction = "up"; MoveTrack(moveTrack); } break; } default: { //more than 1 MessageUserControl.ShowInfo("Track Movement", "You must select only one song to move."); break; } } } } }
protected void MoveUp_Click(object sender, EventArgs e) { //form event validation - presence if (string.IsNullOrEmpty(PlaylistName.Text)) { MessageUserControl.ShowInfo("missing data", "enter a playlist name to playlist name field"); } else { if (PlayList.Rows.Count == 0) { MessageUserControl.ShowInfo("track movement", "no rows to select from"); } else { //collect the selected tracks MoveTrackItem moveTrack = new MoveTrackItem(); int rowselected = 0; CheckBox trackSelection = null; //traverse the gridview controll playlist //could use FOREACH here for (int i = 0; i < PlayList.Rows.Count; i++) { //point to the checkbox control on the gridview row trackSelection = PlayList.Rows[i].FindControl("Selected") as CheckBox; //Test the setting of the Checkbox if (trackSelection.Checked) { rowselected++; moveTrack.TrackID = int.Parse((trackSelection.FindControl("TrackId") as Label).Text); moveTrack.TrackNumber = int.Parse((trackSelection.FindControl("TrackNumber") as Label).Text); } } //was a single song selected? switch (rowselected) { case 0: { MessageUserControl.ShowInfo("track movement", "no rows to selected"); break; } case 1: { if (moveTrack.TrackNumber == 1) { MessageUserControl.ShowInfo("track movement", "song is first selected"); } else { moveTrack.Direction = "up"; MoveTrack(moveTrack); } } break; default: { MessageUserControl.ShowInfo("track movement", "too many rows selected, select one"); break; } } } } }
}//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
protected void MoveUp_Click(object sender, EventArgs e) { //code to go here if (string.IsNullOrEmpty(PlaylistName.Text)) { MessageUserControl.ShowInfo("Missing Data", "Enter a playlist name"); } else { MoveTrackItem moveTrack = new MoveTrackItem(); if (PlayList.Rows.Count == 0) { MessageUserControl.ShowInfo("Track movement", "You must a have a playlist for movement"); } else { ; int rowsSelected = 0; CheckBox trackSelection = null; //treverse the grid view control PlayList //you could do the same code using foreach() foreach (GridViewRow row in PlayList.Rows) { trackSelection = row.FindControl("Selected") as CheckBox; if (trackSelection.Checked) { rowsSelected++; moveTrack.TrackId = int.Parse((trackSelection.FindControl("TrackID") as Label).Text); moveTrack.TrackNumber = int.Parse((trackSelection.FindControl("TrackNumber") as Label).Text); } } //was a single song selected switch (rowsSelected) { case 0: { MessageUserControl.ShowInfo("Track movement Concern", "You must select one song to remove"); break; } case 1: { if (moveTrack.TrackNumber == 1) { MessageUserControl.ShowInfo("Track Movement concern", "Track selected already at the top of your playlist"); } else { moveTrack.Direction = "up"; MoveTrack(moveTrack); } break; } default: { MessageUserControl.ShowInfo("Track movement Concern", "You must select at only one song to remove"); break; } } } } }