// 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); }
// 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 void PutArtist(int id, Artist newArtist) { var response = Client.PutAsJsonAsync("api/Artists/" + id, newArtist).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Put complete!"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
public static void PostArtist(Artist artist) { var response = Client.PostAsJsonAsync("api/Artists", artist).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Artist added!"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
public static void Main() { // Add an Accept header for JSON format. Client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/xml")); Client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); #region AlbumsOperations //Get All albums IEnumerable<Album> albums = GetAllAlbums(); foreach (var album in albums) { Console.WriteLine(album.Title); } //Get album by id Album singleAlbum = GetAlbumById(1); Console.WriteLine(singleAlbum.Title); //PostAlbum Album albumForPost = new Album() { Title = "PostedAlbum", Producer = "PostedProducer", Year = new DateTime(2011, 01, 01) }; PostAlbum(albumForPost); //Put album Album albumForPut = new Album() { AlbumID = 2, Title = "PutedAlbum", Producer = "PutedProducer", Year = new DateTime(2012, 01, 01) }; PutAlbum(2, albumForPut); //Delete album DeleteAlbum(4); #endregion #region ArtistsOperations //Get all artists IEnumerable<Artist> artists = GetAllArtists(); foreach (var artist in artists) { Console.WriteLine(artist.Name); } //Get artist by id Artist artistById = GetArtistById(1); Console.WriteLine(artistById.Name); //Post artist Artist artistForPost = new Artist() { Name = "PostName", Country = "PostCountry", DateOfBirth = new DateTime(2010, 01, 01) }; PostArtist(artistForPost); //Put artist Artist artistForPut = new Artist() { ArtistID = 2, Name = "PutName", Country = "PutCountry", DateOfBirth = new DateTime(2010, 01, 01) }; PutArtist(2, artistForPut); //Delete artist DeleteArtist(3); #endregion #region SongsOperations //GetAllSongs IEnumerable<Song> songs = GetAllSongs(); foreach (var song in songs) { Console.WriteLine(song.Title); } //GetSongById Song songById = GetSongById(3); Console.WriteLine(songById.Title); //Post song Song songForPost = new Song() { ArtistID = 1, AlbumID = 1, Title = "PostSong", Year = new DateTime(2002, 01, 01), Genre = "TestGenre" }; PostSong(songForPost); //Put song Song songForPut = new Song() { SongID = 3, ArtistID = 1, AlbumID = 1, Title = "PutSong", Year = new DateTime(2002, 01, 01), Genre = "PutGenre" }; PutSong(3, songForPut); //Delete song DeleteSong(7); #endregion }