コード例 #1
0
        public IHttpActionResult Create(ArtistModels artist)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var newArtist = new Artist
            {
                Name      = artist.Name,
                Country   = artist.Country,
                BirthDate = artist.BirthDate,
                WebSite   = artist.WebSite
            };

            this.data.Artists.Add(newArtist);
            this.data.SaveChanges();

            artist.Id = newArtist.Id;
            return(this.Ok(artist));
        }
コード例 #2
0
ファイル: EntryPoint.cs プロジェクト: JohnAgarwal57/Telerik
        private static void UpdateArtist(int id, string newName, string newCountry, string newBirthDate, string newWebSite)
        {
            Console.WriteLine("Updating artist...");
            var artist = new ArtistModels
            {
                Name      = newName,
                Country   = newCountry,
                BirthDate = newBirthDate,
                WebSite   = newWebSite
            };

            HttpResponseMessage response = client.PutAsJsonAsync(Artists + Update + id, artist).Result;

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Artist updated!");
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }
コード例 #3
0
ファイル: EntryPoint.cs プロジェクト: JohnAgarwal57/Telerik
        private static void AddNewArtist(string name, string country, string birthdate, string website)
        {
            Console.WriteLine("Creating artist...");
            var artist = new ArtistModels
            {
                Name      = name,
                Country   = country,
                BirthDate = birthdate,
                WebSite   = website
            };

            HttpResponseMessage response = client.PostAsJsonAsync(Artists + Create, artist).Result;

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Artist added!");
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }
コード例 #4
0
        public IHttpActionResult Update(int id, ArtistModels artist)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var existingArtist = this.data.Artists.All().FirstOrDefault(a => a.Id == id);

            if (existingArtist == null)
            {
                return(this.BadRequest("Such artist does not exists!"));
            }

            existingArtist.Name      = artist.Name;
            existingArtist.Country   = artist.Country;
            existingArtist.BirthDate = artist.BirthDate;
            if (artist.WebSite != null)
            {
                existingArtist.WebSite = artist.WebSite;
            }

            this.data.SaveChanges();

            artist.Id      = id;
            artist.WebSite = existingArtist.WebSite;

            var newArtist = new
            {
                Id        = artist.Id,
                Name      = artist.Name,
                Country   = artist.Country,
                BirthDate = artist.BirthDate,
                WebSite   = artist.WebSite
            };

            return(this.Ok(newArtist));
        }