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

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

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

            artistModel.Id = artist.Id;
            return this.Ok(artistModel);
        }
コード例 #2
0
        public IHttpActionResult Update(int id, ArtistModels artistModel)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var artistToUpdate = this.data.Artists.All().FirstOrDefault(a => a.Id == id);
            if (artistToUpdate == null)
            {
                return this.BadRequest("Such artist does not exists!");
            }

            artistToUpdate.Name = artistModel.Name;
            artistToUpdate.Country = artistModel.Country;
            artistToUpdate.BirthDate = artistModel.BirthDate;
            if (artistModel.WebSite != null)
            {
                artistToUpdate.WebSite = artistModel.WebSite;
            }
            
            this.data.SaveChanges();

            var updatedArtistModel = new
            {
                Id = artistToUpdate.Id,
                Name = artistToUpdate.Name,
                Country = artistToUpdate.Country,
                BirthDate = artistToUpdate.BirthDate,
                WebSite = artistToUpdate.WebSite
            };

            return this.Ok(updatedArtistModel);
        }