Beispiel #1
0
        // POST api/Song
        public HttpResponseMessage PostSong(Song song)
        {
            if (ModelState.IsValid)
            {
                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);
            }
        }
Beispiel #2
0
        // PUT api/Song/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);
        }
Beispiel #3
0
        public static void UpdateSong(HttpClient client, int id, string newTitle = null,
            string newGenre = null, int? newYear = null)
        {
            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException("Id must be a positive integer.");
            }

            HttpResponseMessage response = client.GetAsync("api/Song/" + id).Result;
            if (response.IsSuccessStatusCode)
            {
                var song = response.Content.ReadAsAsync<SerializableSong>().Result;

                Song updatedSong = new Song();
                updatedSong.SongId = id;

                if (newTitle != null)
                {
                    updatedSong.Title = newTitle;
                }
                else
                {
                    updatedSong.Title = song.Title;
                }

                if (newGenre != null)
                {
                    updatedSong.Genre = newGenre;
                }
                else
                {
                    updatedSong.Genre = song.Genre;
                }

                if (newYear != null)
                {
                    updatedSong.Year = newYear.Value;
                }
                else
                {
                    updatedSong.Year = song.Year;
                }

                HttpResponseMessage innerResponse = null;

                if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/xml")))
                {
                    innerResponse = client.PutAsXmlAsync<Song>("api/Song/" + id, updatedSong).Result;
                }
                else if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")))
                {
                    innerResponse = client.PutAsJsonAsync<Song>("api/Song/" + id, updatedSong).Result;
                }

                if (innerResponse == null)
                {
                    throw new InvalidOperationException("Client must use json or xml.");
                }

                if (innerResponse.IsSuccessStatusCode)
                {
                    Console.WriteLine("Update successful.");
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)innerResponse.StatusCode, innerResponse.ReasonPhrase);
                }
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }
Beispiel #4
0
        // POST api/Song
        public HttpResponseMessage PostSong(Song song)
        {
            if (ModelState.IsValid)
            {
                Song songToAdd = new Song();
                songToAdd.SongId = song.SongId;
                songToAdd.Title = song.Title;
                songToAdd.Year = song.Year;
                songToAdd.Genre = song.Genre;

                foreach (var artist in song.Artists)
                {
                    var searchedArtist = db.Artists.FirstOrDefault(a => a.Country == artist.Country &&
                        a.DateOfBirth == artist.DateOfBirth && a.Name == artist.Name);

                    if (searchedArtist != null)
                    {
                        songToAdd.Artists.Add(searchedArtist);
                    }
                    else
                    {
                        songToAdd.Artists.Add(new Artist()
                        {
                            Country = artist.Country,
                            Name = artist.Name,
                            DateOfBirth =artist.DateOfBirth
                        });
                    }
                }

                foreach (var album in song.Albums)
                {
                    var searchedAlbum = db.Albums.FirstOrDefault(a => a.Producer == album.Producer && a.Title == album.Title
                        && a.Year == album.Year);

                    if (searchedAlbum != null)
                    {
                        songToAdd.Albums.Add(searchedAlbum);
                    }
                    else
                    {
                        songToAdd.Albums.Add(new Album()
                        {
                            Title = album.Title,
                            Year = album.Year,
                            Producer = album.Producer
                        });
                    }
                }

                db.Songs.Add(songToAdd);
                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);
            }
        }
Beispiel #5
-1
        public static void AddSong(HttpClient client, Song song)
        {
            HttpResponseMessage response = null;

            if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/xml")))
            {
                response = client.PostAsXmlAsync<Song>("api/Song", song).Result;
            }
            else if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")))
            {
                response = client.PostAsJsonAsync<Song>("api/Song", song).Result;
            }

            if (response == null)
            {
                throw new InvalidOperationException("Client must use json or xml.");
            }

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Song added.");
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }