コード例 #1
0
        public IHttpActionResult Update(int id, AlbumModel item)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var existingItem = this.data.Albums.Find(id);

            if (existingItem == null)
            {
                return this.BadRequest("Invalid Album");
            }

            // Check if some of the fields in the new data are not null
            if (!string.IsNullOrEmpty(item.Title))
            {
                existingItem.Title = item.Title;
            }

            if (!string.IsNullOrEmpty(item.Producer))
            {
                existingItem.Producer = item.Producer;
            }
            
            existingItem.Year = item.Year;

            this.data.Albums.SaveChanges();

            item.Id = existingItem.Id;

            return this.Ok(item);
        }
コード例 #2
0
        public IHttpActionResult Create(AlbumModel item)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newItem = new Album()
            {
                Title = item.Title,
                Year = item.Year,
                Producer = item.Producer,
            };

            this.data.Albums.Add(newItem);
            this.data.Albums.SaveChanges();

            item.Id = newItem.Id;

            return this.Ok(item);
        }