public async Task <IHttpActionResult> DeleteBook(int id) { try { string userId = User.Identity.GetUserId(); UserBook userBook = await db.UserBooks.Include("Book").FirstOrDefaultAsync(ub => ub.Id == id); if (userBook == null) { return(NotFound()); } if (userBook.UserId != userId) { return(BadRequest("User does not own the book.")); } if (userBook.Book.CanBeUpdatedBy(userId)) { db.Books.Remove(userBook.Book); // . all user books will be deleted cascadely } else { // . remove only user book db.UserBooks.Remove(userBook); } await db.SaveChangesAsync(); return(Ok(userBook)); } catch (Exception e) { _logger.Error(e, "Can not delete user book"); throw; } }
// GET api/BookWord/5 //[ResponseType(typeof(BookWord))] //public async Task<IHttpActionResult> GetBookWord(int id) //{ // BookWord bookword = await db.BookWords.FindAsync(id); // if (bookword == null) // { // return NotFound(); // } // return Ok(bookword); //} // PUT api/BookWord/5 public async Task <IHttpActionResult> PutBookWord(int id, BookWordDto bookWordDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var entity = bookWordDto.ToEntity(); entity.Id = id; db.Entry(entity).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookWordExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
// PUT api/Word/5 public async Task <IHttpActionResult> PutWord(int id, Word word) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != word.Id) { return(BadRequest()); } db.Entry(word).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!WordExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
// DELETE api/Translation/5 public async Task <IHttpActionResult> DeleteTranslation(int id) { Translation bookwordtranslation = await db.Translations.FindAsync(id); if (bookwordtranslation == null) { return(NotFound()); } db.Translations.Remove(bookwordtranslation); await db.SaveChangesAsync(); return(Ok(bookwordtranslation)); }
// PUT api/Book/5 public async Task <IHttpActionResult> PutBook(int id, BookDto bookDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var book = db.Books.Find(id); book.Name = bookDto.name; //db.Entry(book).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }