Example #1
0
        public void AddReturnBook(ReturnBookPlus returnBookPlus)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    // change borrow book state
                    BorrowBook br = _context.BorrowBook.Find(returnBookPlus.ReturnBook.BorrowBook_Id);
                    br.IsReturn = true;
                    _context.SaveChanges();

                    _context.ReturnBook.Add(returnBookPlus.ReturnBook);
                    _context.SaveChanges();
                    foreach (ReturnBookDetail detail in returnBookPlus.ReturnBookDetails)
                    {
                        detail.ReturnBook_Id = returnBookPlus.ReturnBook.Id;
                        _context.ReturnBookDetail.Add(detail);
                        _context.SaveChanges();

                        if (detail.Book_Id != null)
                        {
                            // update Inventory of Book
                            _bookDAL.UpdateInventory((int)detail.Book_Id, (int)detail.Quantity);
                        }
                    }
                    transaction.Complete();
                }
                catch (Exception ex)
                {
                    transaction.Dispose();
                    throw new Exception("Error from ReturnBookDAL: " + ex.Message.ToString());
                }
            }
        }
Example #2
0
 public void EditReturnBook(int id, ReturnBookPlus newReturnBookPlus)
 {
     try
     {
         _returnBookDAL.EditReturnBook(id, newReturnBookPlus);
     }
     catch (Exception ex)
     {
         throw new Exception("Error from ReturnBookBLL: " + ex.Message.ToString());
     }
 }
Example #3
0
 public void AddReturnBook(ReturnBookPlus returnBookPlus)
 {
     try
     {
         _returnBookDAL.AddReturnBook(returnBookPlus);
     }
     catch (Exception ex)
     {
         throw new Exception("Error from ReturnBookBLL: " + ex.Message.ToString());
     }
 }
 public ResultModel Put(int id, [FromBody] ReturnBookPlus returnBookPlus)
 {
     try
     {
         _returnBookBLL.EditReturnBook(id, returnBookPlus);
         return(new ResultModel(Code.OK, "thành công"));
     }
     catch (Exception)
     {
         return(new ResultModel(Code.SVERROR, "lỗi hệ thống"));
     }
 }
 public ResultModel Post([FromBody] ReturnBookPlus returnBookPlus)
 {
     try
     {
         _returnBookBLL.AddReturnBook(returnBookPlus);
         return(new ResultModel(Code.CREATED, "thành công"));
     }
     catch (Exception)
     {
         return(new ResultModel(Code.SVERROR, "lỗi hệ thống"));
     }
 }
Example #6
0
        public void EditReturnBook(int id, ReturnBookPlus newReturnBookPlus)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    ReturnBook newReturnBook = newReturnBookPlus.ReturnBook;
                    ReturnBook oldReturnBook = _context.ReturnBook.Where(oldRB => oldRB.Id == id).SingleOrDefault();

                    // change borrow book state
                    if (oldReturnBook.BorrowBook_Id != newReturnBook.BorrowBook_Id)
                    {
                        BorrowBook borrowBookNew = _context.BorrowBook.Find(newReturnBook.BorrowBook_Id);
                        BorrowBook borrowBookOld = _context.BorrowBook.Find(oldReturnBook.BorrowBook_Id);
                        borrowBookNew.IsReturn = true;
                        borrowBookOld.IsReturn = false;
                        _context.SaveChanges();
                    }

                    if (oldReturnBook != null)
                    {
                        oldReturnBook.ReturnBookCode = newReturnBook.ReturnBookCode;
                        oldReturnBook.ReturnDate     = newReturnBook.ReturnDate;
                        oldReturnBook.Description    = newReturnBook.Description;
                        oldReturnBook.BorrowBook_Id  = newReturnBook.BorrowBook_Id;
                        _context.SaveChanges();
                    }
                    else
                    {
                        throw new Exception("Return Book doesn't exist");
                    }


                    // delete all old borrow book detail
                    List <ReturnBookDetail> returnBookDetails = _context.ReturnBookDetail.Where(item => item.ReturnBook_Id == id).ToList();
                    _context.ReturnBookDetail.RemoveRange(returnBookDetails);
                    _context.SaveChanges();

                    // add all new borrow book detail
                    _context.ReturnBookDetail.AddRange(newReturnBookPlus.ReturnBookDetails);
                    _context.SaveChanges();

                    transaction.Complete();

                    //foreach (ReturnBookDetail newReturnBookDetail in newReturnBookPlus.ReturnBookDetails)
                    //{
                    //    ReturnBookDetail oldReturnBookDetail = _context.ReturnBookDetail.
                    //        Where(rbDetail => rbDetail.Id == newReturnBookDetail.Id).SingleOrDefault();

                    //    //if (newReturnBookDetail.Book_Id != null)
                    //    //{
                    //    //    // update Inventory of Book
                    //    //    //int? quantityOfUpdate = newReturnBookDetail.Quantity - oldReturnBookDetail.Quantity;
                    //    //    //_bookDAL.UpdateInventory((int)newReturnBookDetail.Book_Id, (int)quantityOfUpdate);
                    //    //}

                    //    if (newReturnBookDetail != null && newReturnBookDetail.Id != 0 && oldReturnBookDetail  != null)
                    //    {
                    //        oldReturnBookDetail.ReturnBookDetailCode = newReturnBookDetail.ReturnBookDetailCode;
                    //        oldReturnBookDetail.Quantity = newReturnBookDetail.Quantity;
                    //        oldReturnBookDetail.Description = newReturnBookDetail.Description;
                    //        oldReturnBookDetail.Book_Id = newReturnBookDetail.Book_Id;
                    //        oldReturnBookDetail.ReturnBook_Id = newReturnBookDetail.ReturnBook_Id;
                    //        _context.SaveChanges();
                    //    }
                    //    else
                    //    {
                    //        transaction.Dispose();
                    //        throw new Exception("Return Book Detail doesn't not exist");
                    //    }
                    //}
                }
                catch (Exception ex)
                {
                    transaction.Dispose();
                    throw new Exception("Error from ReturnBookDAL: " + ex.Message.ToString());
                }
            }
        }