Ejemplo n.º 1
0
 public void Albums_Delete(int albumid)
 {
     using (var context = new ChinookContext())
     {
         var existing = context.Albums.Find(albumid);
         if (existing == null)
         {
             throw new Exception("Album does not exists on file.");
         }
         //else statement here is optional.
         context.Albums.Remove(existing);
         context.SaveChanges();
     }
 }
Ejemplo n.º 2
0
        }//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
Ejemplo n.º 3
0
 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);
         }
     }
 }
Ejemplo n.º 4
0
        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);
                }
        }
        public void DeleteTrack(int trackid)
        {
            using (var context = new ChinookContext())
            {
                //any business rules

                //do the delete
                //1. find the existing record on the database
                var existing = context.Tracks.Find(trackid);
                //2. delete the record from the database
                context.Tracks.Remove(existing);
                //3. commit delete transaction
                context.SaveChanges();
            }
        }
Ejemplo n.º 6
0
        public void Album_Delete(int albumid)
        {
            using (var context = new ChinookContext())
            {
                //any business rules

                //do the delete
                //find the existing record on the database
                var existing = context.Albums.Find(albumid);
                //delete the record from the database
                context.Albums.Remove(existing);
                //commit the transaction
                context.SaveChanges();
            }
        }
        }//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
Ejemplo n.º 8
0
 public int Album_Add(Album album)
 {
     using (var context = new ChinookContext())
     {
         if (CheckReleaseYear(album))
         {
             context.Albums.Add(album);  //staged
             context.SaveChanges();      //committed
             return(album.AlbumId);
         }
         else
         {
             throw new BusinessRuleException("Validation error: ", reasons);
         }
     }
 }
Ejemplo n.º 9
0
 public int Album_Delete(int albumID)
 {
     using (var context = new ChinookContext())
     {
         var existing = context.Albums.Find(albumID);
         if (existing == null)
         {
             throw new Exception("Album not on file. Delete failed.");
         }
         else
         {
             context.Albums.Remove(existing);                //staged
             return(context.SaveChanges());                  //committed
         }
     }
 }
Ejemplo n.º 10
0
        public void Albums_Delete(int _albumID)
        {
            using (ChinookContext context = new ChinookContext())
            {
                var existingAlbum = context.Albums.Find(_albumID);

                // Ensure provided record exists in the database
                if (existingAlbum == null)
                {
                    throw new Exception("Provided album does not exist on file");
                }

                context.Albums.Remove(existingAlbum);
                context.SaveChanges();
            }
        }
Ejemplo n.º 11
0
 public int Album_Delete(int albumid)
 {
     using (var context = new ChinookContext())
     {
         var existing = context.Albums.Find(albumid);
         if (existing == null)
         {
             throw new Exception("Album not on file. Delete unnecessary.");
         }
         else
         {
             context.Albums.Remove(existing);     //staging
             return(context.SaveChanges());       //now committed //returns # records changed
         }
     }
 }
Ejemplo n.º 12
0
 public int Album_Add(Album item)
 {
     using (var context = new ChinookContext())
     {
         if (CheckReleaseYear(item))
         {
             context.Albums.Add(item);   //staging
             context.SaveChanges();      //committed
             return(item.AlbumId);       //return new id value
         }
         else
         {
             throw new BusinessRuleException("Validation Error", reasons); //Include the library by right clicking on it
         }
     }
 }
Ejemplo n.º 13
0
        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();
            }
        }
Ejemplo n.º 14
0
 public int Album_Add(Album item)
 {
     using (var context = new ChinookContext())
     {
         if (CheckReleaseYear(item))
         {
             context.Albums.Add(item); //staging
             context.SaveChanges();    //comitted
             return(item.AlbumId);     //returns new ID value
         }
         else
         {
             throw new BusinessRuleException("Validation Error", reasons);
         }
     }
 }
Ejemplo n.º 15
0
 public int Album_Add(Album item)
 {
     using (var context = new ChinookContext())
     {
         if (CheckReleaseYear(item))
         {
             context.Albums.Add(item); //staging step, not on db.
             context.SaveChanges();    //now committed to the db.
             return(item.AlbumId);     //returns new id value.
         }
         else
         {
             throw new BusinessRuleException("Validation Error", reasons);
         }
     }
 }
Ejemplo n.º 16
0
        public void Add_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;
                // add the instance of track info to the database
                context.Tracks.Add(trackinfo);
                // commit of the add
                context.SaveChanges();
            }
        }
Ejemplo n.º 17
0
 public int Album_Delete(int albumid)
 {
     using (var context = new ChinookContext())
     {
         var existing = context.Albums.Find(albumid);
         if (existing == null)
         {
             throw new Exception("Album not on file");
         }
         else
         {
             context.Albums.Remove(existing);
             return(context.SaveChanges());
         }
     }
 }
Ejemplo n.º 18
0
        }//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
Ejemplo n.º 19
0
        }     //eom

        public void Remove_TracksFromPlaylist(string playlistName, int trackId)
        {
            using (var context = new ChinookContext())
            {
                //need to get the Playlist with the playlistName parameter
                Playlist existing = (
                    from x in context.Playlists
                    where x.Name.Equals(playlistName)
                    select x).FirstOrDefault();
                //find the track with the traclId
                var trackToRemove = context.PlaylistTracks.Find(existing.PlaylistId, trackId);
                //remove the track
                context.PlaylistTracks.Remove(trackToRemove);
                //save the changes to the database
                context.SaveChanges();
            } //end of using
        }     //eom
Ejemplo n.º 20
0
        }//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
Ejemplo n.º 21
0
        }//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
Ejemplo n.º 22
0
 public int Album_Add(Album item)
 {
     using (var context = new ChinookContext())
     {
         if (CheckReleaseYear(item))
         {
             //item.Title = null; use to test entity validation
             context.Albums.Add(item);   //staging
             context.SaveChanges();      //committed
             return(item.AlbumId);       //return new id value
         }
         else
         {
             throw new BusinessRuleException("Validation Error", reasons);
         }
     }
 }
        }//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
Ejemplo n.º 24
0
        }//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
Ejemplo n.º 25
0
        public void AddTrack(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;

                //Add the instance of track info to the database
                context.Tracks.Add(trackInfo);

                //Commit transaction
                context.SaveChanges();
            }
        }
Ejemplo n.º 26
0
        public void DeleteTrack(int trackid)
        {
            using (var context = new ChinookContext())
            {
                // Any business rules

                // find the existing record on the database
                var existing = context.Tracks.Find(trackid);
                // or
                // var existing = Get_Track(trackid);

                // remove track
                context.Tracks.Remove(existing);

                // commit the transaction
                context.SaveChanges();
            }
        }
        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();
            }
        }
Ejemplo n.º 28
0
        public void DeleteTrack(int trackid)
        {
            using (var context = new ChinookContext())
            {
                //Any buisness rules

                //Do the delete, find the existing record on the database
                var existing = context.Tracks.Find(trackid);
                //Or you can do:
                //var existing = GetTrack(trackid);

                //Delete the record from the database
                context.Tracks.Remove(existing);

                //Commit transaction
                context.SaveChanges();
            }
        }
Ejemplo n.º 29
0
        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);
                }
            }
        }
Ejemplo n.º 30
0
        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();
            }
        }