Exemple #1
0
        public Response <int> Insert(AlbumDto newObject)
        {
            if (newObject == null)
            {
                return(new Response <int>(false, "Album cannot be null", -1));
            }

            Album album = new Album
            {
                name    = newObject.Name,
                relesed = newObject.Released
            };

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    db.Album.Add(album);
                    db.SaveChanges();
                }
                return(new Response <int>(true, "Album was inserted in DB", album.album_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));
                }
            }
        }
Exemple #2
0
        public Response <int> Delete(int id)
        {
            if (id <= 0)
            {
                return(new Response <int>(false, "Id must be grater than 0", -1));
            }

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    Album albumToDelete = db.Album.Find(id);
                    db.Album.Attach(albumToDelete);
                    id = db.Album.Remove(albumToDelete).album_id;
                    db.SaveChanges();
                }
                return(new Response <int>(true, "Album was delete from DB", 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));
                }
            }
        }
Exemple #3
0
        public Response <int> Insert(ArtistDto newObject)
        {
            if (newObject == null)
            {
                return(new Response <int>(false, "Artist cannot be null", -1));
            }

            Artist artist = new Artist();

            artist.name = newObject.Name;

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    db.Artist.Add(artist);
                    db.SaveChanges();
                }
                return(new Response <int>(true, "Artist was inserted in DB", artist.artist_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));
                }
            }
        }
Exemple #4
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));
                }
            }
        }
Exemple #5
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));
                }
            }
        }
Exemple #6
0
        public Response <int> Insert(SongDto newObject)
        {
            if (newObject == null)
            {
                return(new Response <int>(false, "Song cannot be null", -1));
            }

            Song song = new Song
            {
                title        = newObject.Title,
                genre        = newObject.Genre,
                released     = newObject.Released,
                duration     = newObject.Duration,
                fk_album_id  = newObject.AlbumId,
                fk_artist_id = newObject.ArtistId
            };

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    db.Song.Add(song);
                    db.SaveChanges();
                }
                return(new Response <int>(true, "Song was inserted in DB", song.song_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));
                }
            }
        }