public IHttpActionResult Create(SongModel song) { if (!this.ModelState.IsValid) { return BadRequest(ModelState); } var artist = this.data.Artists.All().First(a => a.Id == song.ArtistId); if (artist == null) { return BadRequest("Such artist does not exist!"); } var newSong = new Song { Title = song.Title, ArtistId = song.ArtistId }; this.data.Songs.Add(newSong); this.data.SaveChanges(); song.Id = newSong.Id; return Ok(song); }
private static void AddSong(Song song) { HttpResponseMessage response = client.PostAsJsonAsync("Songs", song).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Song added!"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
private static void UpdateSong(int id, Song song) { string url = "Songs/" + id; HttpResponseMessage response = client.PutAsJsonAsync(url, song).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Song with id {0} changed", 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")); }