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 AddToAlbums(Album album) { if (base.mediaTypeEnum == MediaTypeEnum.Json) { var res = base.client.PostAsJsonAsync("/api/Albums", album).Result; } else { var res = base.client.PostAsXmlAsync("/api/Albums", album).Result; } }
public void UpdateAlbumRecord(int id, Album album) { album.AlbumId = id; if (base.mediaTypeEnum == MediaTypeEnum.Json) { var res = base.client.PutAsJsonAsync("/api/Albums/" + id, album).Result; } else { var res = base.client.PutAsXmlAsync("/api/Albums/" + id, album).Result; } }
// POST api/Albums public HttpResponseMessage PostAlbum(Album album) { if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, album); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = album.AlbumId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public static Album CreateOrLoadAlbum(IRepository<Album> albums, AlbumDetails albumDetails) { Album album = albums.Get(albumDetails.ID); if (album != null) { return album; } Album newAlbum = new Album() { Title = albumDetails.Title, Producer = albumDetails.Producer, Year = albumDetails.Year }; albums.Add(newAlbum); return newAlbum; }
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; }
// PUT api/Albums/5 public HttpResponseMessage PutAlbum(int id, Album album) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != album.AlbumId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(album).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }