public async Task<IHttpActionResult> Put(EditAuthorViewModel model) { if(!ModelState.IsValid) return BadRequest(ModelState); using (var ctx = new NewsDbContext()) { var author = new Author() { Id = model.Id.Value, Name = model.Name }; ctx.Authors.Attach(author); ctx.Entry(author).State = EntityState.Modified; await ctx.SaveChangesAsync(); return StatusCode(HttpStatusCode.NoContent); } }
public async Task<IHttpActionResult> Post(CreateAuthorViewModel model) { if (!ModelState.IsValid) return BadRequest(ModelState); using (var ctx = new NewsDbContext()) { var author = new Author() { Name = model.Name }; ctx.Authors.Add(author); await ctx.SaveChangesAsync(); var data = new AuthorViewModel() { Id = author.Id, Name = author.Name }; return Created(new Uri(Request.RequestUri + "api/authors" + data.Id), data); } }