public static AlbumModel ToAlbumModel(Album albumEntity) { AlbumModel albumModel = new AlbumModel(); albumModel.ID = albumEntity.ID; albumModel.Title = albumEntity.Title; albumModel.Year = albumEntity.Year; albumModel.Producer = albumEntity.Producer; foreach (Song song in albumEntity.Songs) { albumModel.Songs.Add(new SongDetails() { ID = song.ID, Title = song.Title, Year = song.Year, Genre = song.Genre }); } foreach (Artist artist in albumEntity.Artists) { albumModel.Artists.Add(new ArtistDetails() { ID = artist.ID, Name = artist.Name, DateOfBirth = artist.DateOfBirth, Country = artist.Country }); } return albumModel; }
public void AddAlbum( string title, int year, string producer, ICollection<SongDetails> songs, ICollection<ArtistDetails> artists) { AlbumModel newAlbum = new AlbumModel() { Title = title, Year = year, Producer = producer, Songs = songs, Artists = artists }; HttpResponseMessage response = null; if (this.client.DefaultRequestHeaders.Accept.Contains(JsonMediaType)) { response = this.client.PostAsJsonAsync<AlbumModel>(ControllerUrl, newAlbum).Result; } else { response = this.client.PostAsXmlAsync<AlbumModel>(ControllerUrl, newAlbum).Result; } if (!response.IsSuccessStatusCode) { throw new HttpRequestException(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase)); } }
public HttpResponseMessage UpdateAlbum(int id, AlbumModel album) { if (id <= 0) { HttpResponseMessage errorResponse = Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Provided id must be a positive integer!"); throw new HttpResponseException(errorResponse); } if (id != album.ID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } Album updatedAlbum = null; try { updatedAlbum = AlbumsMapper.ToAlbumEntity(album, artistsRepository, songsRepository); } catch (Exception) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid album model provided!"); } this.albumsRepository.Update(id, updatedAlbum); return Request.CreateResponse(HttpStatusCode.OK); }
public HttpResponseMessage AddAlbum(AlbumModel album) { Album newAlbum = null; try { newAlbum = AlbumsMapper.ToAlbumEntity(album, artistsRepository, songsRepository); } catch (Exception) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid album model provided!"); } albumsRepository.Add(newAlbum); album.ID = newAlbum.ID; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, album); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = album.ID })); return response; }
public static Album ToAlbumEntity( AlbumModel albumModel, IRepository<Artist> artistsRepository, IRepository<Song> songsRepository) { Album album = new Album(); album.ID = albumModel.ID; album.Title = albumModel.Title; album.Year = albumModel.Year; album.Producer = albumModel.Producer; foreach (SongDetails song in albumModel.Songs) { album.Songs.Add(Extensions.CreateOrLoadSong(songsRepository, song)); } foreach (ArtistDetails artist in albumModel.Artists) { album.Artists.Add(Extensions.CreateOrLoadArtist(artistsRepository, artist)); } return album; }
private static AlbumDetails ConvertToAlbumDetails(AlbumModel albumModel) { AlbumDetails albumDetails = new AlbumDetails() { ID = albumModel.ID, Title = albumModel.Title, Producer = albumModel.Producer, Year = albumModel.Year }; return albumDetails; }