public ActionResult Create(tblBook tblBook, HttpPostedFileBase file) { if (!isStaff()) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (ModelState.IsValid) { //check params, check duplicate if (tblBook.ID == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tblBook book = db.tblBooks.Find(tblBook.ID); if (book != null) { ViewBag.CreateMessage = "Book ID is already exist"; return(View()); } //pass check - create new //save file string textPath = "/Images/no_image.jpg"; //default is noimages if (file != null) { string path = Path.Combine(Server.MapPath("~/Images"), tblBook.ID + Path.GetFileName(file.FileName)); file.SaveAs(path); textPath = "/Images/" + tblBook.ID + file.FileName; } book = new tblBook() { Name = tblBook.Name, AddDate = DateTime.Now, Author = tblBook.Author, Catagories = tblBook.Catagories, Description = tblBook.Description, ID = tblBook.ID, Place = tblBook.Place, Status = tblBook.Status, CoverPicture = textPath }; db.tblBooks.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblBook)); }
public ActionResult Create([Bind(Include = "id,title,author,isbn,image,year,description,wishlist,finish,taken,rating")] book book) { try { if (ModelState.IsValid) { db.books.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException) { ModelState.AddModelError("", "Error. Could not save book"); } return(View(book)); }
// Allow User to Extend Book Borrow Transaction public ActionResult Extend(int?id) { //check params if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tblBorrow tblBorrow = db.tblBorrows.Find(id); if (tblBorrow == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //get USER EMAIL if (Session["USER"] == null) { return(RedirectToAction("Index", "Init")); } tblAccount tblAccount = (tblAccount)Session["USER"]; string email = tblAccount.Email; //check User have permission to change if (!tblBorrow.BorrowerEmail.Equals(email)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //check Book Transaction have right condition to change if (tblBorrow.ExtendNumber <= 0) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } int date = (int)Math.Round((tblBorrow.ReturnDate - DateTime.Now).TotalDays); if (date < -3 || date > 1) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //pass check - extend borrow tblBorrow.ReturnDate = tblBorrow.ReturnDate.AddDays(10); tblBorrow.ExtendNumber -= 1; db.Entry(tblBorrow).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewCategories_DeleteItem(int id) { BookLibraryEntities db = new BookLibraryEntities(); Book book = db.Books.FirstOrDefault(c => c.Id == id); try { db.Books.Remove(book); db.SaveChanges(); this.GridViewBooks.PageIndex = 0; ErrorSuccessNotifier.AddInfoMessage("Book successfully deleted!"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
public ActionResult Create(string BookID, string SubID) { if (!isStaff()) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (ModelState.IsValid) { //check param if (BookID == null || SubID == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tblBook tblBook = db.tblBooks.Find(BookID); if (tblBook == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewBag.BookName = tblBook.Name; //Check Instance ID already exist string ID = BookID + SubID; tblBookDetail tblBookDetail = db.tblBookDetails.Find(ID); if (tblBookDetail != null) { ViewBag.CreateMessage = "This ID Instance already exist!"; return(View()); } //Success Add ! Return to Add New tblBookDetail = new tblBookDetail(); tblBookDetail.BookID = BookID; tblBookDetail.SubID = ID; tblBookDetail.Status = "Free"; //Default is Free db.tblBookDetails.Add(tblBookDetail); db.SaveChanges(); ViewBag.CreateMessage = "Successfully!"; return(View()); } return(View()); }
protected void LinkButtonCreateOrEdit_Click(object sender, EventArgs e) { Book book; int bookId = 0; BookLibraryEntities db = new BookLibraryEntities(); if (!String.IsNullOrEmpty(this.LabelBookId.Text)) { bookId = Convert.ToInt32(this.LabelBookId.Text); book = db.Books.FirstOrDefault(c => c.Id == bookId); } else { book = new Book(); db.Books.Add(book); } try { if (String.IsNullOrEmpty(this.TextBoxTitle.Text)) { throw new ArgumentException("Title must be filled!"); } if (String.IsNullOrEmpty(this.TextBoxAuthors.Text)) { throw new ArgumentException("Author must be filled!"); } book.Title = this.TextBoxTitle.Text; book.Author = this.TextBoxAuthors.Text; book.ISBN = this.TextBoxISBN.Text; book.Website = this.TextBoxWebsite.Text; book.Description = this.TextBoxDescription.Text; book.CategoryId = Convert.ToInt32(this.DropDownListCategories.SelectedItem.Value); db.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Book successfully " + (bookId == 0 ? "created!" : "edited!")); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("EditBooks.aspx", false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void LinkButtonCreateOrEdit_Click(object sender, EventArgs e) { Category category; int categoryId = 0; BookLibraryEntities db = new BookLibraryEntities(); if (!String.IsNullOrEmpty(this.LabelCategoryId.Text)) { categoryId = Convert.ToInt32(this.LabelCategoryId.Text); category = db.Categories.FirstOrDefault(c => c.Id == categoryId); } else { category = new Category(); db.Categories.Add(category); } string categoryName = this.TextBoxCreateOrEdit.Text; try { if (String.IsNullOrEmpty(categoryName)) { throw new ArgumentException("Category name is required!"); } category.Name = categoryName; db.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Category " + (categoryId == 0 ? "created!" : "edited!")); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("EditCategories.aspx", false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
public ActionResult Create(BookViewModel model) { if (ModelState.IsValid) { try { Book book = new Book(); book.Title = model.Title; book.AuthorId = model.AuthorId; book.Price = model.Price; book.Editorial = model.Editorial; db.Books.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } catch (DbUpdateException dbEx) { SqlException sqlEx = dbEx.GetBaseException() as SqlException; if (sqlEx.Errors[0].Number == 2601) { TempData["ErrorMessage"] = "Duplicated code"; } else { TempData["ErrorMessage"] = sqlEx.Errors[0].Message; } ViewBag.AuthorId = new SelectList(db.Authors, "AuthorId", "AuthorName", model.AuthorId); return(View(model)); } } ViewBag.AuthorId = new SelectList(db.Authors, "AuthorId", "AuthorName", model.AuthorId); return(View(model)); }
// POST: BorrowsManage/Create // Create new transaction for Borrowing public ActionResult Create(string BookID, string SubID, string Email) { if (!isStaff()) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (ModelState.IsValid) { //check param if (BookID == null || SubID == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tblBook tblBook = db.tblBooks.Find(BookID); if (tblBook == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewBag.BookName = tblBook.Name; //neu book khong available thi return va bao loi if (!tblBook.Status.Equals("Available")) { ViewBag.BookID = BookID; ViewBag.CreateMessage = "This Book is not available to borrow!"; return(View()); } //neu Book Instance ID khong ton tai thi return ve View va bao loi // Book InstanceID = BookID + SubID string InstanceID = BookID + SubID; tblBookDetail tblBookDetail = db.tblBookDetails.Find(InstanceID); if (tblBookDetail == null) { ViewBag.BookID = BookID; ViewBag.CreateMessage = "This Instance ID does not exist!"; return(View()); } //BookInstance is Lost or Borrowed if (tblBookDetail.Status != "Free") { ViewBag.BookID = BookID; ViewBag.CreateMessage = "This Book Instance does not available!"; return(View()); } //neu Email khong ton tai thi return View va bao loi tblAccount tblAccount = db.tblAccounts.Find(Email); if (tblAccount == null) { ViewBag.BookID = BookID; ViewBag.CreateMessage = "This Email does not exist"; return(View()); } //end validate //Add tblBookDetail.Status = "Borrowed"; db.Entry(tblBookDetail).State = EntityState.Modified; tblBorrow tblBorrow = new tblBorrow(); tblBorrow.BorrowerEmail = Email; tblBorrow.BookSubID = InstanceID; tblBorrow.ExtendNumber = 3; tblBorrow.CreateDate = DateTime.Now; tblBorrow.ReturnDate = DateTime.Now.AddDays(10); tblBorrow.IsEnd = false; db.tblBorrows.Add(tblBorrow); db.SaveChanges(); return(RedirectToAction("Index")); } return(View()); }