// GET api/Default1/5 public SerializableAlbum GetAlbum(int id) { var albums = from album in db.Albums.Include("Artists").Include("Songs") select album; Album searchedAlbum = albums.FirstOrDefault(a => a.AlbumId == id); if (searchedAlbum == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } SerializableAlbum albumToReturn = new SerializableAlbum(); albumToReturn.AlbumId = searchedAlbum.AlbumId; albumToReturn.Title = searchedAlbum.Title; albumToReturn.Year = searchedAlbum.Year; albumToReturn.Producer = searchedAlbum.Producer; List<string> songs = new List<string>(); foreach (var song in searchedAlbum.Songs) { songs.Add(song.Title); } albumToReturn.AddSongs(songs); List<string> artists = new List<string>(); foreach (var artist in searchedAlbum.Artists) { artists.Add(artist.Name); } albumToReturn.AddArtists(artists); return albumToReturn; }
// GET api/Default1 public IEnumerable<SerializableAlbum> GetAlbums() { var albums = from album in db.Albums.Include("Artists").Include("Songs") select album; List<SerializableAlbum> albumsToReturn = new List<SerializableAlbum>(); foreach (var album in albums) { SerializableAlbum newAlbum = new SerializableAlbum(); newAlbum.AlbumId = album.AlbumId; newAlbum.Producer = album.Producer; newAlbum.Title = album.Title; newAlbum.Year = album.Year; var artists = new List<string>(); foreach (var artist in album.Artists) { artists.Add(artist.Name); } newAlbum.AddArtists(artists); var songs = new List<string>(); foreach (var song in album.Songs) { songs.Add(song.Title); } newAlbum.AddSongs(songs); albumsToReturn.Add(newAlbum); } return albumsToReturn; }