public async Task <IHttpActionResult> PutAuthor(int id, Author author) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != author.Id) { return(BadRequest()); } db.Entry(author).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AuthorExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> PutBook(int id, BookDetailDto bookDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != bookDto.Id) { return(BadRequest()); } if (bookDto.AuthorId == 0) { Author newAu = new Author { Name = bookDto.AuthorName }; IHttpActionResult result = await new AuthorsController().PostAuthor(newAu); bookDto.AuthorId = newAu.Id; } Book book = Mapper.Map <BookDetailDto, Book>(bookDto); book.Id = bookDto.Id; db.Entry(book).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }