public ActionResult Create([Bind(Include = "ID,StudentID,BookID,IsReturn")] BorrowAndReturnInput borrowAndReturnInput) { var borrowAndReturn = new BorrowAndReturn(); if (ModelState.IsValid) { //db.BorrowAndReturns.Add(borrowAndReturn); //db.SaveChanges(); var userInfoObj = unitOfWork.UserInfoRepository.Get() .Where(ctx => ctx.StudentID.Equals(borrowAndReturnInput.StudentID) == true) .ToList(); if (userInfoObj.Count() == 0) { ModelState.AddModelError("StudentID", "No student with this ID"); } var book = unitOfWork.BookRepository.Get() .Where(item => item.ID == borrowAndReturnInput.BookID) .ToList(); if (book.Count() == 0) { ModelState.AddModelError("BookID", "No book with this ID"); } else { if (book[0].IsAvaliable == true) { ModelState.AddModelError("BookID", "The book has been borrowed!"); } else { book[0].IsAvaliable = true; borrowAndReturn = new BorrowAndReturn { BookID = borrowAndReturnInput.BookID, UserID = userInfoObj[0].UserID, BorrowTime = DateTime.Now, IsReturn = false }; unitOfWork.BorrowAndReturnRepository.Insert(borrowAndReturn); unitOfWork.Save(); return(RedirectToAction("Index")); } } } return(View(borrowAndReturnInput)); }
public ActionResult Return([Bind(Include = "BookID")] BorrowAndReturnInput borrowAndReturnInput) { var bookObj = unitOfWork.BookRepository.Get() .Where(item => item.ID == borrowAndReturnInput.BookID) .ToList(); if (bookObj.Count() == 0) { //throw new Exception("请核查图书库内是否有该本图书"); ModelState.AddModelError("BookID", "Please check whether exists the book in the library!"); } var borrow = unitOfWork.BorrowAndReturnRepository.Get() .Where(item => item.BookID == bookObj[0].ID) .ToList(); if (borrow == null) { //throw new Exception("请核查该本图书是否已经借阅"); ModelState.AddModelError("BookID", "Please check whether this book is borrowed!"); } var tmp = DateTime.Now - borrow[0].BorrowTime; var days = tmp.Days; /*var borrowAndReturn = new BorrowAndReturnInput() * { * BookID = borrow[0].BookID, * StudentID = borrowAndReturnInput.StudentID, * ReturnTime = DateTime.Now, * IsReturn = true, * ExpiredDays = days * };*/ borrow.LastOrDefault().IsReturn = true; borrow.LastOrDefault().ReturnTime = DateTime.Now; borrow.LastOrDefault().ExpiredDays = days; var book = unitOfWork.BookRepository.GetByID(borrowAndReturnInput.BookID); book.IsAvaliable = false; unitOfWork.Save(); return(RedirectToAction("Index")); }