public IHttpActionResult Create(SongModel item) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var newItem = new Song() { Title = item.Title, Year = item.Year, Genre = item.Genre, }; this.data.Songs.Add(newItem); this.data.Songs.SaveChanges(); item.Id = newItem.Id; return this.Ok(item); }
public IHttpActionResult Update(int id, SongModel item) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var existingItem = this.data.Songs.Find(id); if (existingItem == null) { return this.BadRequest("Invalid Song"); } // 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.Genre)) { existingItem.Genre = item.Genre; } existingItem.Year = item.Year; this.data.Songs.SaveChanges(); item.Id = existingItem.Id; return this.Ok(item); }