public ActionResult AddBook(Book bookToAdd) { if (ModelState.IsValid) { if (libRepo.BookRepo.ListWhere(target => target.CallNumber.ToLower() == bookToAdd.CallNumber.ToLower()).SingleOrDefault() == null) { libRepo.BookRepo.Add(bookToAdd); libRepo.Save(); TempData["SuccessNoti"] = "Add new book successfully."; return RedirectToAction("Index"); } else { TempData["ErrorNoti"] = "This call number is already exists."; return View(bookToAdd); } } return View(bookToAdd); }
public ActionResult DeleteBook(Book bookToDelete) { List<BorrowEntry> removeBorrowEntry = libRepo.BorrowEntryRepo.ListWhere(target => target.BookID == bookToDelete.BookID).ToList(); Book bookToDel = libRepo.BookRepo.Find(bookToDelete.BookID); if (bookToDel == null) { TempData["ErrorNoti"] = "The book that you want to delete is already deleted."; return RedirectToAction("Index"); } RequestEntry entryToCheck = bookToDel.GetRelatedRequestEntry(ref libRepo); if (entryToCheck != null) libRepo.RequestEntryRepo.Remove(entryToCheck); libRepo.BorrowEntryRepo.Remove(removeBorrowEntry); libRepo.BookRepo.Remove(bookToDel); libRepo.Save(); TempData["SuccessNoti"] = "Delete " + bookToDelete.BookName + " successfully."; return RedirectToAction("Index"); }
public ActionResult Editbook(Book bookToEdit) { if (ModelState.IsValid) { //If book is lost check related borrowentry and force return&delete req.entry// if (bookToEdit.BookStatus == Status.Lost) { BorrowEntry entry = libRepo.BorrowEntryRepo.ListWhere(target => target.BookID == bookToEdit.BookID && target.ReturnDate == null).SingleOrDefault(); if (entry != null) { entry.ReturnDate = DateTime.Now.Date; RequestEntry removeEntry = entry.GetBorrowBook(ref libRepo).GetRelatedRequestEntry(ref libRepo); if (removeEntry != null) libRepo.RequestEntryRepo.Remove(removeEntry); } } //If book is available make sure that book is not reserve or borrowed// else if (bookToEdit.BookStatus == Status.Available) { if (libRepo.BookRepo.ListWhere(target => (target.CallNumber.ToLower() == bookToEdit.CallNumber.ToLower()) && target.BookID != bookToEdit.BookID).SingleOrDefault() == null) { Book bookToFind = libRepo.BookRepo.Find(bookToEdit.BookID); if (bookToFind.BookStatus == Status.Borrowed || bookToFind.BookStatus == Status.Reserved) { TempData["ErrorNoti"] = "Can't edit book status due to this book is " + bookToFind.BookStatus.ToString()+"."; return RedirectToAction("Index"); } bookToFind.BookName = bookToEdit.BookName; bookToFind.CallNumber = bookToEdit.CallNumber; bookToFind.Author = bookToEdit.Author; bookToFind.Publisher = bookToEdit.Publisher; bookToFind.Year = bookToEdit.Year; bookToFind.Detail = bookToEdit.Detail; bookToFind.BookStatus = bookToEdit.BookStatus; libRepo.BookRepo.Update(bookToFind); TempData["SuccessNoti"] = "Edit book successfully."; libRepo.Save(); return RedirectToAction("Index"); } else { TempData["ErrorNoti"] = "This call number is already exists."; return View(bookToEdit); } } //Another check???? Avail -> Borrowed? Avail -> Req. if (libRepo.BookRepo.ListWhere(target => (target.CallNumber.ToLower() == bookToEdit.CallNumber.ToLower()) && target.BookID != bookToEdit.BookID).SingleOrDefault() == null) { libRepo.BookRepo.Update(bookToEdit); TempData["SuccessNoti"] = "Edit book successfully."; libRepo.Save(); return RedirectToAction("Index"); } else { TempData["ErrorNoti"] = "This call number is already exists."; return View(bookToEdit); } } return View(bookToEdit); }
/* Handle method of /api/BookQuery (HTTPPOST) * this method use to get related-book data by target data as JSON object * in this object it contain data of specifed book that user want to find. * Finally return result as HTTPresponse whether it found or not. */ public IHttpActionResult PostBook([FromBody]JObject target) { Book bookToFind = new Book(); //Recall individual data on JSON object by use indexing of target parameter bookToFind.BookName = target["BookName"].ToString(); bookToFind.Author = target["Author"].ToString(); bookToFind.Publisher = target["Publisher"].ToString(); bookToFind.CallNumber = target["CallNumber"].ToString().ToLower(); IEnumerable<Book> list; //If year is not specfied find book exclude year data since year is numeric type if (target["Year"].ToString() == "") { list = from book in LibRepo.BookRepo.List() where StringUtil.IsContains(book.Author,bookToFind.Author) && book.BookName.Contains(bookToFind.BookName) && StringUtil.IsContains(book.Publisher, bookToFind.Publisher) && book.CallNumber.ToLower().Contains(bookToFind.CallNumber) select new Book() { BookID = book.BookID, CallNumber = book.CallNumber, BookName = book.BookName, Author = book.Author, Detail = book.Detail, Publisher = book.Publisher, Year = book.Year, BookStatus = book.BookStatus }; } else { try { /* If year is specfied find book include year data * if error occured while parse string to integer return error result(not found) * otherwise return find result as HTTPresponse. */ bookToFind.Year = int.Parse(target["Year"].ToString()); list = from book in LibRepo.BookRepo.List() where StringUtil.IsContains(book.Author, bookToFind.Author) && book.BookName.Contains(bookToFind.BookName) && StringUtil.IsContains(book.Publisher, bookToFind.Publisher) && book.Year == bookToFind.Year && book.CallNumber.ToLower().Contains(bookToFind.CallNumber) select new Book() { BookID = book.BookID, CallNumber = book.CallNumber, BookName = book.BookName, Author = book.Author, Detail = book.Detail, Publisher = book.Publisher, Year = book.Year, BookStatus = book.BookStatus }; } catch(FormatException){ return InternalServerError(); } } if (list == null) return NotFound(); else if (list.ToList().Count > 0) return Ok(list); else return NotFound(); }