public void AddArtist( string name, string country, DateTime dateOfBirth, ICollection<SongDetails> songs, ICollection<AlbumDetails> albums) { ArtistModel newArtist = new ArtistModel() { Name = name, Country = country, DateOfBirth = dateOfBirth, Songs = songs, Albums = albums }; HttpResponseMessage response = null; if (this.client.DefaultRequestHeaders.Accept.Contains(JsonMediaType)) { response = this.client.PostAsJsonAsync<ArtistModel>(ControllerUrl, newArtist).Result; } else { response = this.client.PostAsXmlAsync<ArtistModel>(ControllerUrl, newArtist).Result; } if (!response.IsSuccessStatusCode) { throw new HttpRequestException(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase)); } }
public static ArtistModel ToArtistModel(Artist artistEntity) { ArtistModel artistModel = new ArtistModel(); artistModel.ID = artistEntity.ID; artistModel.Name = artistEntity.Name; artistModel.Country = artistEntity.Country; artistModel.DateOfBirth = artistEntity.DateOfBirth; foreach (Song song in artistEntity.Songs) { artistModel.Songs.Add(new SongDetails() { ID = song.ID, Title = song.Title, Year = song.Year, Genre = song.Genre }); } foreach (Album album in artistEntity.Albums) { artistModel.Albums.Add(new AlbumDetails() { ID = album.ID, Title = album.Title, Producer = album.Producer, Year = album.Year }); } return artistModel; }
public HttpResponseMessage AddArtist(ArtistModel artist) { Artist newArtist = null; try { newArtist = ArtistsMapper.ToArtistEntity(artist, albumsRepository, songsRepository); } catch (Exception) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid artist model provided!"); } artistsRepository.Add(newArtist); artist.ID = newArtist.ID; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, artist); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = artist.ID })); return response; }
public static Artist ToArtistEntity( ArtistModel artistModel, IRepository<Album> albumsRepository, IRepository<Song> songsRepository) { Artist artist = new Artist(); artist.ID = artistModel.ID; artist.Name = artistModel.Name; artist.DateOfBirth = artistModel.DateOfBirth; artist.Country = artistModel.Country; foreach (SongDetails song in artistModel.Songs) { artist.Songs.Add(Extensions.CreateOrLoadSong(songsRepository, song)); } foreach (AlbumDetails album in artistModel.Albums) { artist.Albums.Add(Extensions.CreateOrLoadAlbum(albumsRepository, album)); } return artist; }
private static ArtistDetails ConvertToArtistDetails(ArtistModel artistModel) { ArtistDetails artistDetails = new ArtistDetails() { ID = artistModel.ID, Name = artistModel.Name, DateOfBirth = artistModel.DateOfBirth, Country = artistModel.Country }; return artistDetails; }