Пример #1
0
        public async Task <ServiceResponse <BookDTO_ToReturn> > UpdateBook(BookDTO_ToUpdate UpdateBook, int id)
        {
            var oldBookData       = _dbContext.Books.FirstOrDefault(x => x.Id == id);
            var cateGoryBookCheck = _dbContext.CategoryBooks.FirstOrDefault(x => x.Id == UpdateBook.CategoryBookId);

            if (cateGoryBookCheck == null)
            {
                return(ResponseResult.Failure <BookDTO_ToReturn>("CategoryBook id not found"));
            }
            else
            {
                if (oldBookData.Name != UpdateBook.Name ||
                    oldBookData.Writter != UpdateBook.Writter ||
                    oldBookData.BorrowDay != UpdateBook.BorrowDay ||
                    oldBookData.BorrowPrice != UpdateBook.BorrowPrice ||
                    oldBookData.LatePrice != UpdateBook.LatePrice ||
                    oldBookData.CategoryBookId != UpdateBook.CategoryBookId)
                {
                    oldBookData.Name        = UpdateBook.Name;
                    oldBookData.Writter     = UpdateBook.Writter;
                    oldBookData.BorrowDay   = UpdateBook.BorrowDay;
                    oldBookData.BorrowPrice = UpdateBook.BorrowPrice;
                    oldBookData.LatePrice   = UpdateBook.LatePrice;
                    //check categoryBook
                    oldBookData.CategoryBookId = UpdateBook.CategoryBookId;
                    await _dbContext.SaveChangesAsync();
                }
                return(ResponseResult.Success(_mapper.Map <BookDTO_ToReturn>(oldBookData)));
            }
        }
 public async Task <IActionResult> UpdateBook(BookDTO_ToUpdate book, int id)
 {
     return(Ok(await _bookService.UpdateBook(book, id)));
 }