public IHttpActionResult Create(ArtistModel item) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var newItem = new Artist() { Name = item.Name, Country = item.Country, DateOfBirth = item.DateOfBirth, }; this.data.Artists.Add(newItem); this.data.Artists.SaveChanges(); item.Id = newItem.Id; return this.Ok(item); }
public IHttpActionResult Update(int id, ArtistModel item) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var existingItem = this.data.Artists.Find(id); if (existingItem == null) { return this.BadRequest("Invalid Artist"); } // Check if some of the fields in the new data are not null if (!string.IsNullOrEmpty(item.Name)) { existingItem.Name = item.Name; } if (!string.IsNullOrEmpty(item.Country)) { existingItem.Country = item.Country; } if (item.DateOfBirth != null) { existingItem.DateOfBirth = item.DateOfBirth; } this.data.Artists.SaveChanges(); item.Id = existingItem.Id; return this.Ok(item); }