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 AddToSongs(Song song) { if (base.mediaTypeEnum == MediaTypeEnum.Json) { var res = base.client.PostAsJsonAsync("/api/Songs", song).Result; } else { var res = base.client.PostAsXmlAsync("/api/Songs", song).Result; } }
public void UpdateSongRecord(int id, Song song) { song.SongId = id; if (base.mediaTypeEnum == MediaTypeEnum.Json) { var res = base.client.PutAsJsonAsync("/api/Songs/" + id, song).Result; } else { var res = base.client.PutAsXmlAsync("/api/Songs/" + id, song).Result; } }
// POST api/Songs public HttpResponseMessage PostSong(Song song) { if (ModelState.IsValid) { db.Songs.Add(song); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, song); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = song.SongId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public static Song CreateOrLoadSong(IRepository<Song> songs, SongDetails songDetails) { Song song = songs.Get(songDetails.ID); if (song != null) { return song; } Song newSong = new Song() { Title = songDetails.Title, Genre = songDetails.Genre, Year = songDetails.Year }; songs.Add(newSong); return newSong; }
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; }
// PUT api/Songs/5 public HttpResponseMessage PutSong(int id, Song song) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != song.SongId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(song).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }