public static SongModel ToSongModel(Song songEntity) { SongModel songModel = new SongModel(); songModel.ID = songEntity.ID; songModel.Title = songEntity.Title; songModel.Year = songEntity.Year; songModel.Genre = songEntity.Genre; foreach (Album album in songEntity.Albums) { songModel.Albums.Add(new AlbumDetails() { ID = album.ID, Title = album.Title, Year = album.Year, Producer = album.Producer }); } foreach (Artist artist in songEntity.Artists) { songModel.Artists.Add(new ArtistDetails() { ID = artist.ID, Name = artist.Name, DateOfBirth = artist.DateOfBirth, Country = artist.Country }); } return songModel; }
public void AddSong( string title, int year, string genre, ICollection<ArtistDetails> artists, ICollection<AlbumDetails> albums) { SongModel newSong = new SongModel() { Title = title, Year = year, Genre = genre, Albums = albums, Artists = artists }; HttpResponseMessage response = null; if (this.client.DefaultRequestHeaders.Accept.Contains(JsonMediaType)) { response = this.client.PostAsJsonAsync<SongModel>(ControllerUrl, newSong).Result; } else { response = this.client.PostAsXmlAsync<SongModel>(ControllerUrl, newSong).Result; } if (!response.IsSuccessStatusCode) { throw new HttpRequestException(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase)); } }
public HttpResponseMessage UpdateSong(int id, SongModel song) { if (id <= 0) { HttpResponseMessage errorResponse = Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Provided id must be a positive integer!"); throw new HttpResponseException(errorResponse); } if (id != song.ID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } Song updatedSong = null; try { updatedSong = SongsMapper.ToSongEntity(song, albumsRepository, artistsRepository); } catch (Exception) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid song model provided!"); } this.songsRepository.Update(id, updatedSong); return Request.CreateResponse(HttpStatusCode.OK); }
public static Song ToSongEntity( SongModel songModel, IRepository<Album> albumsRepository, IRepository<Artist> artistsRepository) { Song song = new Song(); song.ID = songModel.ID; song.Title = songModel.Title; song.Year = songModel.Year; song.Genre = songModel.Genre; foreach (AlbumDetails album in songModel.Albums) { song.Albums.Add(Extensions.CreateOrLoadAlbum(albumsRepository, album)); } foreach (ArtistDetails artist in songModel.Artists) { song.Artists.Add(Extensions.CreateOrLoadArtist(artistsRepository, artist)); } return song; }
private static SongDetails ConvertToSongDetails(SongModel songModel) { SongDetails songDetails = new SongDetails() { ID = songModel.ID, Title = songModel.Title, Genre = songModel.Genre, Year = songModel.Year }; return songDetails; }