static void AddAlbum(HttpClient httpClient, Album theAlbum) { HttpContent postContent = new StringContent(JsonConvert.SerializeObject(theAlbum)); postContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var response = httpClient.PostAsync("album", postContent).Result; try { response.EnsureSuccessStatusCode(); Console.WriteLine("Album added!"); } catch (Exception) { Console.WriteLine("Error adding student"); } }
public IHttpActionResult Create(AlbumModel album) { if (!this.ModelState.IsValid) { return BadRequest("Invalid album parameters!"); } var newAlbum = new Album { Title = album.Title, Producer = album.Producer, Year = album.Year }; this.data.Albums.Add(newAlbum); this.data.SaveChanges(); return Ok(HttpStatusCode.Created); }
static void Main(string[] args) { var httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(serviceRoot); var album = new Album() { Title = "Reign of Hell", Producer = "Telerik Academy", Year = 2014 }; var songs = new List<Song>(); songs.Add(new Song() { Title = "DSA is coming!", Genre = "demonical", Year = 2014 }); songs.Add(new Song() { Title = "We are not prepared!", Genre = "demonical", Year = 2014 }); songs.Add(new Song() { Title = "Gonna sell my soul!", Genre = "demonical", Year = 2014 }); var artists = new List<Artist>(); artists.Add(new Artist() { Name = "Daniel", Country = "Bulgaria", DateOfBirth = DateTime.Parse("1986-11-06") }); artists[0].Songs.Add(songs[0]); artists[0].Songs.Add(songs[1]); artists.Add(new Artist() { Name = "Ivan", Country = "Bulgaria", DateOfBirth = DateTime.Parse("1989-07-18") }); artists[1].Songs.Add(songs[2]); album.Artists = artists; AddAlbum(httpClient, album); }