// PUT api/albums/5 public HttpResponseMessage Put(int id, Album value) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != value.AlbumId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(value).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
private static void AddAlbum(Album album) { HttpResponseMessage response = client.PostAsJsonAsync("Albums", album).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Album added!"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
internal static void AddNewAlbum(string title) { var album = new Album { Title = title }; var response = Client.PostAsJsonAsync("api/Albums/Create", album).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Album added!"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
internal static void UpdateAlbum(int id, Album album) { var newAlbum = new Album { Id = id, Title = album.Title }; var response = Client.PutAsJsonAsync("api/Albums/Update/" + id, newAlbum).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Album updated!"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
public IHttpActionResult Create(AlbumModel album) { if (!this.ModelState.IsValid) { return BadRequest(ModelState); } var newAlbum = new Album { Title = album.Title }; this.data.Albums.Add(newAlbum); this.data.SaveChanges(); album.Id = newAlbum.Id; return Ok(album); }
// POST api/albums public void Post(Album value) { db.Albums.Add(value); db.SaveChanges(); }
private static void UpdateAlbum(int id, Album album) { string url = "Albums/" + id; HttpResponseMessage response = client.PutAsJsonAsync(url, album).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Album with id {0} updated", id); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
static void Main() { // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); Album album = new Album { Title = "Album title 7", Year = "2000", Producer = "Producer 7" }; AddAlbum(album); Console.WriteLine("Show all albums:"); foreach (var a in GetAlbums()) { Console.WriteLine("{0} {1} {2} {3}", a.AlbumId, a.Title, a.Year, a.Producer); } Console.WriteLine("Show album:"); var currentAlbum = GetAlbum(3); Console.WriteLine("{0} {1} {2} {3}", currentAlbum.AlbumId, currentAlbum.Title, currentAlbum.Year, currentAlbum.Producer); DeleteAlbum(8); DeleteAlbum(7); DeleteAlbum(6); Console.WriteLine("Update album:"); Album albumUpdated = new Album { AlbumId = 5, Title = "Album title 7", Year = "2000", Producer = "Producer 7" }; UpdateAlbum(5, albumUpdated); Artist artist = new Artist { Name = "Name3", Country = "Country3", DateOfBirth = DateTime.Parse("30/05/1988"), }; AddArtist(artist); Console.WriteLine("Show all artists:"); foreach (var ar in GetArtists()) { Console.WriteLine("{0} {1} {2} {3}", ar.ArtistId, ar.Name, ar.Country, ar.DateOfBirth); } Console.WriteLine("Show artist:"); var currentArtist = GetArtist(2); Console.WriteLine("{0} {1} {2} {3}", currentArtist.ArtistId, currentArtist.Name, currentArtist.Country, currentArtist.DateOfBirth); Console.WriteLine("Update album:"); var artistUpdate = new Artist { ArtistId = 2, Name = "Name22", Country = "Country22", DateOfBirth = DateTime.Parse("30/05/1988"), }; UpdateArtist(2, artistUpdate); DeleteArtist(1); Song song = new Song { Title = "Song55", Genre = "mixed", Year = "2014", AlbumId = 4, ArtistId = 3 }; AddSong(song); Console.WriteLine("Show all songs:"); foreach (var s in GetSongs()) { Console.WriteLine("{0} {1} {2} {3} {4} {5}", s.SongId, s.Title, s.Genre, s.Year, s.Album.Title, s.Artist.Name); } Console.WriteLine("Show song:"); var currentSong = GetSong(3); Console.WriteLine("{0} {1} {2} {3} {4} {5}", currentSong.SongId, currentSong.Title, currentSong.Genre, currentSong.Year, currentSong.Album.Title, currentSong.Artist.Name); Console.WriteLine("Update song:"); Song songUpdated = new Song { SongId = 4, Title = "Song10", Genre = "mixed", Year = "2014", AlbumId = 4, ArtistId = 3 }; UpdateSong(4, songUpdated); DeleteSong(5); //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); }
public void AddAlbum(Album album) { _albums.Add(album); }