// POST api/Albums
        public HttpResponseMessage PostAlbum(Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, album);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = album.AlbumId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
        public static void AddAlbum(HttpClient client, Album album)
        {
            HttpResponseMessage response;
            if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")))
            {
                response = client.PostAsJsonAsync("api/albums", album).Result;
            }
            else
            {
                response = client.PostAsJsonAsync("api/albums", album).Result;
            }

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Album {0} added successfully", album.Title);
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }
Beispiel #3
0
        public static void Main()
        {
            #region json
            var client = new HttpClient
            {
                BaseAddress = new Uri("http://localhost:19872/")
            };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //foreach (var c in MusicCatalogDAL.GetAlbums(client))
            //{
            //    Console.WriteLine(c.AlbumId + " " + c.Title);
            //}
            //Console.WriteLine();

            //Artist artistToAdd = new Artist()
            //    {
            //        Name = "Random name 3",
            //        Age = 35,
            //        Country = "Bulgaria"
            //    };
            //MusicCatalogDAL.AddArtist(client, artistToAdd);
            //MusicCatalogDAL.GetArtists(client);

            //Album updatedAlbum = new Album()
            //    {
            //        AlbumId = 8,
            //        Title = "xml Title",
            //        Year = 1234
            //    };
            //MusicCatalogDAL.UpdateAlbum(client, 8, updatedAlbum);
            //MusicCatalogDAL.GetAlbums(client);
            //MusicCatalogDAL.DeleteAlbum(client, 7);

            //Album newAlbum = new Album()
            //{
            //    Title = "Album with songs",
            //    Year = 2010
            //};
            //Song first = new Song()
            //{
            //    Title = "First Song",
            //    Genre = "Rock"
            //};
            //Song second = new Song()
            //{
            //    Title = "Second Song",
            //    Genre = "House"
            //};

            //newAlbum.Songs.Add(first);
            //newAlbum.Songs.Add(second);
            //MusicCatalogDAL.AddAlbum(client, newAlbum);

            #endregion

            var client2 = new HttpClient
            {
                BaseAddress = new Uri("http://localhost:19872/")
            };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

            Album newXmlAlbum = new Album()
            {
                Title = "Album with xmlsongs",
                Year = 2010
            };
            Song xmlFirst = new Song()
            {
                Title = "First xmlSong",
                Genre = "Rock"
            };
            Song xmlSecond = new Song()
            {
                Title = "Second xmlSong",
                Genre = "House"
            };

            newXmlAlbum.Songs.Add(xmlFirst);
            newXmlAlbum.Songs.Add(xmlSecond);
            MusicCatalogDAL.AddAlbum(client2, newXmlAlbum);
        }
        // PUT api/Albums/5
        public HttpResponseMessage PutAlbum(int id, Album album)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != album.AlbumId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

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

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }