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 void AddToArtists(Artist artist) { if (base.mediaTypeEnum == MediaTypeEnum.Json) { var res = base.client.PostAsJsonAsync("/api/Artists", artist).Result; } else { var res = base.client.PostAsXmlAsync("/api/Artists", artist).Result; } }
public void UpdateArtistRecord(int id, Artist artist) { artist.ArtistId = id; if (base.mediaTypeEnum == MediaTypeEnum.Json) { var res = base.client.PutAsJsonAsync("/api/Artists/" + id, artist).Result; } else { var res = base.client.PutAsXmlAsync("/api/Artists/" + id, artist).Result; } }
// POST api/Artists public HttpResponseMessage PostArtist(Artist artist) { if (ModelState.IsValid) { db.Artists.Add(artist); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, artist); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = artist.ArtistId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public static Artist CreateOrLoadArtist(IRepository<Artist> artists, ArtistDetails artistDetails) { Artist artist = artists.Get(artistDetails.ID); if (artist != null) { return artist; } Artist newArtist = new Artist() { Name = artistDetails.Name, Country = artistDetails.Country, DateOfBirth = artistDetails.DateOfBirth }; artists.Add(newArtist); return newArtist; }
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; }
// PUT api/Artists/5 public HttpResponseMessage PutArtist(int id, Artist artist) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != artist.ArtistId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(artist).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }