Example #1
0
        public Response <int> Update(SongDto updatedObject)
        {
            if (updatedObject == null)
            {
                return(new Response <int>(false, "Song to update cannot be null", -1));
            }

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    Song songToUpdate = db.Song.Find(updatedObject.Id);

                    if (songToUpdate == null)
                    {
                        return(new Response <int>(false, "Song to update isn't in the database", -1));
                    }

                    songToUpdate.title        = updatedObject.Title;
                    songToUpdate.genre        = updatedObject.Genre;
                    songToUpdate.released     = updatedObject.Released;
                    songToUpdate.duration     = updatedObject.Duration;
                    songToUpdate.fk_album_id  = updatedObject.AlbumId;
                    songToUpdate.fk_artist_id = updatedObject.ArtistId;

                    db.Song.Attach(songToUpdate);
                    db.Entry(songToUpdate).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
                return(new Response <int>(true, "Song was updated", updatedObject.Id));
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    return(new Response <int>(false, "Somethig was wrong. Exception: " + e.Message, -1));
                }
                else
                {
                    return(new Response <int>(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, -1));
                }
            }
        }
Example #2
0
        public Response <int> Update(AlbumDto updatedObject)
        {
            if (updatedObject == null)
            {
                return(new Response <int>(false, "Album to update cannot be null", -1));
            }

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    Album albumToUpdate = db.Album.Find(updatedObject.Id);

                    if (albumToUpdate == null)
                    {
                        return(new Response <int>(false, "Album to update isn't in the database", -1));
                    }

                    albumToUpdate.name    = updatedObject.Name;
                    albumToUpdate.relesed = updatedObject.Released;

                    db.Album.Attach(albumToUpdate);
                    db.Entry(albumToUpdate).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
                return(new Response <int>(true, "Album was updated", updatedObject.Id));
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    return(new Response <int>(false, "Somethig was wrong. Exception: " + e.Message, -1));
                }
                else
                {
                    return(new Response <int>(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, -1));
                }
            }
        }