public void AddToCart(Song song) { // Get the matching cart and album instances var cartItem = db.Playlists.SingleOrDefault( c => c.playlistID == PlaylistCartId && c.songID == song.songID); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new Playlist { songID = song.songID, playlistID = PlaylistCartId, count = 1, }; db.Playlists.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.count++; } // Save changes db.SaveChanges(); }
// POST api/Songs public HttpResponseMessage PostSong(Song song) { if (ModelState.IsValid) { var newArtist = (from artist in db.Artists where artist.ArtistId == song.ArtistId select artist).FirstOrDefault(); song.Artist = newArtist; var newAlbum = (from album in db.Albums where album.AlbumId == song.AlbumId select album).FirstOrDefault(); song.Album = newAlbum; db.Songs.Add(song); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, song); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = song.SongId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/Songs/5 public HttpResponseMessage PutSong(int id, Song song) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != song.SongId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(song).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
public static void SongsDemo() { Client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); // Uncomment lines below to work with xml. //Client.DefaultRequestHeaders.Accept.Add( //new MediaTypeWithQualityHeaderValue("application/xml")); Console.WriteLine("Try add data"); Artist testArtist = new Artist() { Name = "Grafa", DateOfBirth = DateTime.Now, Country = "Bulgaria" }; Song testSong = new Song() { Artist = testArtist, Genre = "Pop", Title = "Ako ima rai", Year = 2005 }; Song secondTestSong = new Song() { Genre = "Folk", Title = "Some song title", Year = 2008 }; Album someAlbum = new Album() { Producer = "Some Producer", Title = "Some Title", Year = 2010 }; someAlbum.Songs.Add(testSong); someAlbum.Songs.Add(secondTestSong); someAlbum.Artists.Add(testArtist); AddAlbum(someAlbum); int lastSongId = 0; Console.WriteLine("\nGet all songs and print title."); var getSongs = Client.GetAsync("api/Songs").Result.Content.ReadAsAsync<IEnumerable<SongDto>>().Result; foreach (var song in getSongs) { Console.WriteLine(song.Title); lastSongId = song.Id; } Console.WriteLine("\nGet last song by ID"); var lastSong = Client.GetAsync("api/Songs/" + lastSongId).Result.Content.ReadAsAsync<SongDto>().Result; Console.WriteLine(lastSong.Title); Console.WriteLine("\nChange last song name from \"{0}\" to \"Other song title\"", lastSong.Title); lastSong.Title = "Other song title"; var update = Client.PutAsJsonAsync("api/Songs/" + lastSong.Id, lastSong).Result; Console.WriteLine("\nGet last song by ID with changed name"); var lastSongWithChangedName = Client.GetAsync("api/Songs/" + lastSongId).Result.Content.ReadAsAsync<Song>().Result; Console.WriteLine(lastSongWithChangedName.Title); Console.WriteLine("\nDelete last song"); var delete = Client.DeleteAsync("api/Songs/" + lastSongId).Result; Console.WriteLine("\nGet all songs again"); var getSongsAfterDelete = Client.GetAsync("api/Songs").Result.Content.ReadAsAsync<IEnumerable<Song>>().Result; foreach (var song in getSongsAfterDelete) { Console.WriteLine(song.Title); lastSongId = song.SongId; } }
public static void AddSong(Song song) { var response = Client.PostAsJsonAsync("api/Songs", song).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Song \"{0}\" added!", song.Title); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
protected static SongDto ConvertSongs(Song song) { var songDto = new SongDto() { Id = song.SongId }; if (song.Title != null) { songDto.Title = song.Title; } if (song.Year != 0) { songDto.Year = song.Year; } if (song.Genre != null) { songDto.Genre = song.Genre; } if (song.Artist != null) { songDto.ArtistName = song.Artist.Name; } return songDto; }