Пример #1
0
        // PUT api/Songs/5
        public HttpResponseMessage PutSong(int id, Song song)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            var songToUpdate = db.Songs.FirstOrDefault(a => a.SongId == id);

            if (songToUpdate != null && song != null)
            {
                songToUpdate.SongTitle = song.SongTitle ?? songToUpdate.SongTitle;
                songToUpdate.SongYear = song.SongYear ?? songToUpdate.SongYear;
                songToUpdate.Genre = song.Genre ?? songToUpdate.Genre;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(songToUpdate).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
Пример #2
0
        static void Main()
        {
            var baseUrl = "http://localhost:12243/";
            var contentType = "application/json";
            var requester = new HttpRequester(baseUrl, contentType);

            // Test album Creation
            Album album = new Album { Producer = "Pesho", Title = "Testov album", ReleaseDate = DateTime.Now };
            requester.Post<Album>("api/album", album);
            Console.WriteLine("Simple Album added!");

            Song song = new Song { Genre = "RnB", Title = "Shake it!", Year = DateTime.Now.AddYears(-20) };
            Artist artist = new Artist { Name = "Pesho Zlia", BirthDate = new DateTime(1950, 1, 1), Country = "Germany" };
            album.Songs.Add(song);
            album.Artists.Add(artist);

            requester.Post<Album>("api/album", album);
            Console.WriteLine("Album added!");

            // Test albums Retrieving
            var retrievedAlbums = requester.Get<IList<Album>>("api/album/");
            foreach (var retrievedAlbum in retrievedAlbums)
            {
                Console.WriteLine("Album found! Title:" + retrievedAlbum.Title);
            }

            // Test one album Retrieving
            var getedAlbum = requester.Get<Album>("api/album/" + retrievedAlbums[0].AlbumId);

            Console.WriteLine("First Album found! Title:" + getedAlbum.Title);

            // Test album updating

            getedAlbum.Title = "Updated Title";
            requester.Put<Album>("api/album/" + getedAlbum.AlbumId, getedAlbum);
            Console.WriteLine("Title updated!");

            // Check new title
            getedAlbum = requester.Get<Album>("api/album/" + retrievedAlbums[0].AlbumId);
            Console.WriteLine("First Album found! Title:" + getedAlbum.Title);

            // Add
            Album newAlbum = new Album { Producer = "Pesho", Title = "Testov album", ReleaseDate = DateTime.Now };

            //Delete the album!
            requester.Delete<Album>("api/album/" + getedAlbum.AlbumId, getedAlbum);
            Console.WriteLine("Album deleted");
        }
Пример #3
0
        // POST api/Songs
        public HttpResponseMessage PostSong(Song song)
        {
            
            if (ModelState.IsValid)
            {
                CheckAlbumsInSong(song);
                var artist = db.Artists.FirstOrDefault(x => x.ArtistId == song.ArtistId);

                song.Artist = artist;
                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);
            }
        }
Пример #4
0
 private void CheckAlbumsInSong(Song song)
 {
     var albums = new List<Album>();
     song.Albums.ToList().ForEach(album =>
     {
         var a = db.Albums.FirstOrDefault(x => x.AlbumId == album.AlbumId);
         //if (a == null)
         //{
         //    a = new Album();
         //    a.AlbumTitle = album.AlbumTitle;
         //}
         albums.Add(a);
     });
     song.Albums = albums;
 }
Пример #5
0
        private static void UpdateSong(string artistName, string title, string newTitle, int? year, string genre, string applicationType)
        {
            if (string.IsNullOrWhiteSpace(title))
            {
                Console.WriteLine("Song title cannot be empty.");
            }

            var artist = AllArtists.FirstOrDefault(a => a.ArtistName == artistName);

            if (artist == null)
            {
                Console.WriteLine("No such artist.");
            }

            var song = artist.Songs.FirstOrDefault(s => s.SongTitle == title);

            if (song == null)
            {
                Console.WriteLine("No such song.");
            }

            var modelSong = new Song
            {
                SongTitle = newTitle,
                SongYear = year,
                Genre = genre
            };

            HttpResponseMessage response = null;

            if (applicationType == "application/json")
            {
                response = Client.PutAsJsonAsync("api/Songs/" + song.SongId, modelSong).Result;
            }
            else
            {
                response = Client.PutAsXmlAsync("api/Songs/" + song.SongId, modelSong).Result;
            }

            if (response.IsSuccessStatusCode)
            {
                song.SongTitle = modelSong.SongTitle ?? song.SongTitle;
                song.SongYear = modelSong.SongYear ?? song.SongYear;
                song.Genre = modelSong.Genre ?? song.Genre;
                Console.WriteLine("Song updated!");
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }
Пример #6
0
        internal static void AddNewSong(string title, int year, string genre, string applicationType, string artistName, string albumTitle)
        {
            Artist artist = AllArtists.FirstOrDefault(a => a.ArtistName == artistName);
            if (artist == null)
            {
                Console.WriteLine("Artist doesn't exist.");
                return;
            }

            Album album = artist.Albums.FirstOrDefault(a => a.AlbumTitle == albumTitle);
            if (album == null)
            {
                Console.WriteLine("Artist dosn't have such an album.");
                return;
            }

            // Add an Accept header for JSON format.
            Client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue(applicationType));

            var song = new Song
            {
                SongTitle = title,
                SongYear = year,
                Genre = genre,
            };

            song.Albums.Add(album);
            song.Artist = artist;
            song.ArtistId = artist.ArtistId;
            HttpResponseMessage response = null;

            if (applicationType == "application/json")
            {
                response = Client.PostAsJsonAsync("api/Songs", song).Result;
            }
            else
            {
                response = Client.PostAsXmlAsync("api/Songs", song).Result;
            }

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Song added!");
                int songID = int.Parse(response.Headers.Location.Segments[3]);
                song.SongId = songID;
                artist.Songs.Add(song);
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }