public static void AddAlbum(HttpClient client, Album album) { HttpResponseMessage response = null; if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/xml"))) { response = client.PostAsXmlAsync<Album>("api/Album/", album).Result; } else if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json"))) { response = client.PostAsJsonAsync<Album>("api/Album/", album).Result; } if (response == null) { throw new InvalidOperationException("Client must use json or xml."); } if (response.IsSuccessStatusCode) { Console.WriteLine("Album added."); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
// POST api/Default1 public HttpResponseMessage PostAlbum(Album album) { if (ModelState.IsValid) { Album albumToAdd = new Album(); albumToAdd.AlbumId = album.AlbumId; albumToAdd.Producer = album.Producer; albumToAdd.Title = album.Title; albumToAdd.Year = album.Year; foreach (var song in album.Songs) { var searchedSong = db.Songs.FirstOrDefault(s => s.Title == song.Title && s.Genre == song.Genre && s.Year == song.Year); if (searchedSong != null) { albumToAdd.Songs.Add(searchedSong); } else { albumToAdd.Songs.Add(new Song() { Title = song.Title, Year = song.Year, Genre = song.Genre }); } } foreach (var artist in album.Artists) { var searchedArtist = db.Artists.FirstOrDefault(a => a.Name == artist.Name && a.Country == artist.Country && a.DateOfBirth == artist.DateOfBirth); if (searchedArtist != null) { albumToAdd.Artists.Add(searchedArtist); } else { albumToAdd.Artists.Add(new Artist() { Name = artist.Name, Country = artist.Country, DateOfBirth = artist.DateOfBirth }); } } db.Albums.Add(albumToAdd); 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); } }
// PUT api/Default1/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); }
// POST api/Default1 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 void UpdateAlbum(HttpClient client, int id, string newTitle = null, string newProducer = null, int? newYear = null) { if (id <= 0) { throw new ArgumentOutOfRangeException("Id must be a positive integer."); } HttpResponseMessage response = client.GetAsync("api/Album/" + id).Result; if (response.IsSuccessStatusCode) { var album = response.Content.ReadAsAsync<SerializableAlbum>().Result; Album updatedAlbum = new Album(); updatedAlbum.AlbumId = id; if (newTitle != null) { updatedAlbum.Title = newTitle; } else { updatedAlbum.Title = album.Title; } if (newProducer != null) { updatedAlbum.Producer = newProducer; } else { updatedAlbum.Producer = album.Producer; } if (newYear != null) { updatedAlbum.Year = newYear.Value; } else { updatedAlbum.Year = album.Year; } HttpResponseMessage innerResponse = null; if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/xml"))) { innerResponse = client.PutAsXmlAsync<Album>("api/Album/" + id, updatedAlbum).Result; } else if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json"))) { innerResponse = client.PutAsJsonAsync<Album>("api/Album/" + id, updatedAlbum).Result; } if (innerResponse == null) { throw new InvalidOperationException("Client must use json or xml."); } if (innerResponse.IsSuccessStatusCode) { Console.WriteLine("Update successful."); } else { Console.WriteLine("{0} ({1})", (int)innerResponse.StatusCode, innerResponse.ReasonPhrase); } } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }