Exemple #1
0
        public Response <AlbumDto> FindById(int id)
        {
            if (id <= 0)
            {
                return(new Response <AlbumDto>(false, "Id must be grater than 0", null));
            }

            AlbumDto response = new AlbumDto();

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    Album album = db.Album.Find(id);
                    response.Id       = album.album_id;
                    response.Name     = album.name;
                    response.Released = album.relesed;
                }
                return(new Response <AlbumDto>(true, "Element was found", response));
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    return(new Response <AlbumDto>(false, "Somethig was wrong. Exception: " + e.Message, null));
                }
                else
                {
                    return(new Response <AlbumDto>(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, null));
                }
            }
        }
Exemple #2
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 #3
0
        public Response <IEnumerable <AlbumDto> > SelectAll()
        {
            List <AlbumDto> response = new List <AlbumDto>();

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    List <Album> albums = db.Album.ToList();

                    foreach (Album album in albums)
                    {
                        AlbumDto aux = new AlbumDto();
                        aux.Id       = album.album_id;
                        aux.Name     = album.name;
                        aux.Released = album.relesed;
                        response.Add(aux);
                    }
                }
                return(new Response <IEnumerable <AlbumDto> >(true, response.Count + " albums found", response));
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    return(new Response <IEnumerable <AlbumDto> >(false, "Somethig was wrong. Exception: " + e.Message, response));
                }
                else
                {
                    return(new Response <IEnumerable <AlbumDto> >(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, response));
                }
            }
        }
Exemple #4
0
        public Response <IEnumerable <SongDto> > SelectAll()
        {
            List <SongDto> response = new List <SongDto>();

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    List <Song> songs = db.Song.ToList();

                    foreach (Song song in songs)
                    {
                        SongDto aux = new SongDto(song.song_id, song.title, song.genre, song.released, song.duration,
                                                  song.fk_album_id.GetValueOrDefault(), song.fk_artist_id.GetValueOrDefault());

                        response.Add(aux);
                    }
                }
                return(new Response <IEnumerable <SongDto> >(true, response.Count + " Songs found", response));
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    return(new Response <IEnumerable <SongDto> >(false, "Somethig was wrong. Exception: " + e.Message, response));
                }
                else
                {
                    return(new Response <IEnumerable <SongDto> >(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, response));
                }
            }
        }
Exemple #5
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 #6
0
        public Response <SongDto> FindById(int id)
        {
            if (id <= 0)
            {
                return(new Response <SongDto>(false, "Id must be grater than 0", null));
            }

            SongDto response;

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    Song song = db.Song.Find(id);
                    response = new SongDto(song.song_id, song.title, song.genre, song.released, song.duration,
                                           song.fk_album_id.GetValueOrDefault(), song.fk_artist_id.GetValueOrDefault());
                }
                return(new Response <SongDto>(true, "Song was find", response));
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    return(new Response <SongDto>(false, "Somethig was wrong. Exception: " + e.Message, null));
                }
                else
                {
                    return(new Response <SongDto>(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, null));
                }
            }
        }
Exemple #7
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 #8
0
 public Response <ArtistDto> FindById(int id)
 {
     if (id <= 0)
     {
         return(new Response <ArtistDto>(false, "Id must be grater than 0", null));
     }
     try
     {
         ArtistDto response = new ArtistDto();
         using (musicDBEntities db = new musicDBEntities())
         {
             Artist artist = db.Artist.Find(id);
             response.Id   = artist.artist_id;
             response.Name = artist.name;
         }
         return(new Response <ArtistDto>(true, "Artist was found", response));
     }
     catch (Exception e)
     {
         if (e.InnerException == null)
         {
             return(new Response <ArtistDto>(false, "Somethig was wrong. Exception: " + e.Message, null));
         }
         else
         {
             return(new Response <ArtistDto>(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, null));
         }
     }
 }
Exemple #9
0
        public Response <IEnumerable <ArtistDto> > SelectAll()
        {
            List <ArtistDto> response = new List <ArtistDto>();

            try
            {
                using (musicDBEntities db = new musicDBEntities())
                {
                    List <Artist> artists = db.Artist.ToList();

                    foreach (Artist artist in artists)
                    {
                        ArtistDto aux = new ArtistDto();
                        aux.Id   = artist.artist_id;
                        aux.Name = artist.name;
                        response.Add(aux);
                    }
                }
                return(new Response <IEnumerable <ArtistDto> >(true, response.Count + " artists found", response));
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    return(new Response <IEnumerable <ArtistDto> >(false, "Somethig was wrong. Exception: " + e.Message, response));
                }
                else
                {
                    return(new Response <IEnumerable <ArtistDto> >(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, response));
                }
            }
        }
Exemple #10
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 #11
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 #12
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));
                }
            }
        }
Exemple #13
0
        /// <summary>
        /// gets the songs of an specific album. Songs are sort by title. Results are paginated
        /// </summary>
        /// <param name="albumId">Id of specific album</param>
        /// <param name="page">page number of results</param>
        /// <returns> Response object of IEnumerable<SongDto> </SongDto></returns>
        public Response <IEnumerable <SongDto> > SelectSongsByAlbum(int albumId, int page = 0)
        {
            if (albumId <= 0)
            {
                return(new Response <IEnumerable <SongDto> >(false, "Id of album must be greater than 0", null));
            }

            try
            {
                List <SongDto> response = new List <SongDto>();
                using (musicDBEntities db = new musicDBEntities())
                {
                    List <Song> albumSongs = db.Song.Where(s => s.fk_album_id == albumId).OrderBy(pet => pet.title).Skip(page * elementsPerPage).Take(elementsPerPage).ToList();

                    foreach (Song song in albumSongs)
                    {
                        SongDto aux = new SongDto(song.song_id, song.title, song.genre, song.released, song.duration,
                                                  song.fk_album_id.GetValueOrDefault(), song.fk_artist_id.GetValueOrDefault());

                        response.Add(aux);
                    }
                }
                return(new Response <IEnumerable <SongDto> >(true, response.Count + " songs in page " + page, response));
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    return(new Response <IEnumerable <SongDto> >(false, "Somethig was wrong. Exception: " + e.Message, null));
                }
                else
                {
                    return(new Response <IEnumerable <SongDto> >(false, "Somethig was wrong. Exception: " + e.InnerException.InnerException.Message, null));
                }
            }
        }