public int BorrowBook(int bookId, CreateBorrowDto dto) { var book = _dbContext.Books .FirstOrDefault(b => b.Id == bookId); if (book is null) { throw new NotFoundException("Book not found."); } var borrowEntity = _mapper.Map <Borrow>(dto); borrowEntity.BookId = bookId; borrowEntity.TakenDate = DateTime.Now; _dbContext.Borrows.Add(borrowEntity); _dbContext.SaveChanges(); return(borrowEntity.Id); }
public ActionResult Borrow([FromRoute] int bookId, [FromBody] CreateBorrowDto dto) { var book = _dbContext.Books .FirstOrDefault(b => b.Id == bookId); if (book is null) { throw new NotFoundException("Book not found."); } var borrowEntity = _mapper.Map <Borrow>(dto); borrowEntity.BookId = bookId; borrowEntity.TakenDate = DateTime.Now; borrowEntity.UserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value); _dbContext.Borrows.Add(borrowEntity); _dbContext.SaveChanges(); return(Created($"api/library/borrow/{bookId}/dish/{borrowEntity.Id}", null)); }