コード例 #1
0
ファイル: PostOperations.cs プロジェクト: PetarPenev/Telerik
        public static void AddArtist(HttpClient client, Artist artist)
        {
            HttpResponseMessage response = null;

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

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

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Artist added.");
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }
コード例 #2
0
        // POST api/Artist
        public HttpResponseMessage PostArtist(Artist artist)
        {
            if (ModelState.IsValid)
            {
                db.Artists.Add(artist);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, artist);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = artist.ArtistId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
コード例 #3
0
ファイル: PutOperations.cs プロジェクト: PetarPenev/Telerik
        public static void UpdateArtist(HttpClient client, int id, string newName = null, 
            string newCountry = null, DateTime? newDateOfBirth = null)
        {
            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException("Id must be a positive integer.");
            }

            HttpResponseMessage response = client.GetAsync("api/Artist/" + id).Result;
            if (response.IsSuccessStatusCode)
            {
                var artist = response.Content.ReadAsAsync<SerializableArtist>().Result;

                Artist updatedArtist = new Artist();
                updatedArtist.ArtistId = id;

                if (newCountry != null)
                {
                    updatedArtist.Country = newCountry;
                }
                else
                {
                    updatedArtist.Country = artist.Country;
                }

                if (newDateOfBirth != null)
                {
                    updatedArtist.DateOfBirth = newDateOfBirth.Value;
                }
                else
                {
                    updatedArtist.DateOfBirth = artist.DateOfBirth;
                }

                if (newName != null)
                {
                    updatedArtist.Name = newName;
                }
                else
                {
                    updatedArtist.Name = artist.Name;
                }

                HttpResponseMessage innerResponse = null;

                if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/xml")))
                {
                    innerResponse = client.PutAsXmlAsync<Artist>("api/Artist/" + id, updatedArtist).Result;
                }
                else if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")))
                {
                    innerResponse = client.PutAsJsonAsync<Artist>("api/Artist/" + id, updatedArtist).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);
            }
        }
コード例 #4
0
        // PUT api/Artist/5
        public HttpResponseMessage PutArtist(int id, Artist artist)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != artist.ArtistId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

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

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }
コード例 #5
0
        // POST api/Artist
        public HttpResponseMessage PostArtist(Artist artist)
        {
            if (ModelState.IsValid)
            {
                Artist artistToAdd = new Artist();
                artistToAdd.ArtistId = artist.ArtistId;
                artistToAdd.Country = artist.Country;
                artistToAdd.DateOfBirth = artist.DateOfBirth;
                artistToAdd.Name = artist.Name;

                foreach (var song in artist.Songs)
                {
                    var searchedSong = db.Songs.FirstOrDefault(s => s.Title == song.Title && s.Genre == song.Genre
                        && s.Year == song.Year);

                    if (searchedSong != null)
                    {
                        artistToAdd.Songs.Add(searchedSong);
                    }
                    else
                    {
                        artistToAdd.Songs.Add(new Song()
                        {
                            Title = song.Title,
                            Year = song.Year,
                            Genre = song.Genre
                        });
                    }
                }

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

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

                db.Artists.Add(artistToAdd);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, artist);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = artist.ArtistId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }