Beispiel #1
0
        // GET api/Song
        public IEnumerable<SerializableSong> GetSongs()
        {
            var songs = from artist in db.Songs.Include("Albums").Include("Artists")
                          select artist;

            List<SerializableSong> songsToReturn = new List<SerializableSong>();

            foreach (var song in songs)
            {
                SerializableSong newSong = new SerializableSong();
                newSong.SongId = song.SongId;
                newSong.Year = song.Year;
                newSong.Title = song.Title;
                newSong.Genre = song.Genre;

                var albums = new List<string>();
                foreach (var album in song.Albums)
                {
                    albums.Add(album.Title);
                }

                newSong.AddAlbums(albums);

                var artists = new List<string>();

                foreach (var artist in song.Artists)
                {
                    artists.Add(artist.Name);
                }

                newSong.AddArtists(artists);

                songsToReturn.Add(newSong);
            }

            return songsToReturn;
        }
Beispiel #2
0
        // GET api/Song/5
        public SerializableSong GetSong(int id)
        {
            var songs = from song in db.Songs.Include("Albums").Include("Artists")
                          select song;

            Song searchedSong = songs.FirstOrDefault(a => a.SongId == id);

            if (searchedSong == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            SerializableSong songToReturn = new SerializableSong();
            songToReturn.SongId = searchedSong.SongId;
            songToReturn.Year = searchedSong.Year;
            songToReturn.Title = searchedSong.Title;
            songToReturn.Genre = searchedSong.Genre;

            var albums = new List<string>();
            foreach (var album in searchedSong.Albums)
            {
                albums.Add(album.Title);
            }

            songToReturn.AddAlbums(albums);

            var artists = new List<string>();

            foreach (var artist in searchedSong.Artists)
            {
                artists.Add(artist.Name);
            }

            songToReturn.AddArtists(artists);

            return songToReturn;
        }