public async Task <ActionResult> Edit([Bind(Include = "AuthorId,AuthorName,Nacionality")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Entry(author).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(author));
        }
        // 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"));
        }
Esempio n. 3
0
 public ActionResult Edit([Bind(Include = "id,title,author,isbn,image,year,description,wishlist,finish,taken,rating")] book book)
 {
     try {
         if (ModelState.IsValid)
         {
             db.Entry(book).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     catch (DataException)
     {
         ModelState.AddModelError("", "Error. Could not save book");
     }
     return(View(book));
 }
        public ActionResult Edit(tblBook tblBook, HttpPostedFileBase file)
        {
            if (!isStaff())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string path = Path.Combine(Server.MapPath("~/Images"), tblBook.ID + Path.GetFileName(file.FileName));
                    file.SaveAs(path);
                    tblBook.CoverPicture = "/Images/" + tblBook.ID + file.FileName;
                }

                db.Entry(tblBook).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(tblBook));
        }
Esempio n. 5
0
        public async Task <ActionResult> Edit(BookViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Book book = await db.Books.FindAsync(model.Id);

                    book.Title           = model.Title;
                    book.AuthorId        = model.AuthorId;
                    book.Price           = model.Price;
                    book.Editorial       = model.Editorial;
                    db.Entry(book).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    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));
        }
Esempio n. 6
0
        // 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());
        }
Esempio n. 7
0
        public async Task <ActionResult> Edit(CategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (BooksAdded != null)
                {
                    model.Books = BooksAdded;
                }
                else
                {
                    model.Books = new List <BookViewModel>();
                }
                using (DbContextTransaction dbTran = db.Database.BeginTransaction())
                {
                    try
                    {
                        Category category = await db.Categories.FindAsync(model.Id);

                        category.CategoryName    = model.Name;
                        db.Entry(category).State = EntityState.Modified;

                        category.Books.Clear();

                        if (BooksAdded != null)
                        {
                            foreach (var book in BooksAdded)
                            {
                                Book b = await db.Books.FindAsync(book.Id);

                                category.Books.Add(b);
                            }
                        }
                        await db.SaveChangesAsync();

                        dbTran.Commit();
                        BooksAdded = null;
                        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;
                        }

                        dbTran.Rollback();

                        List <BookViewModel> pendingBooks = await GetPendingBooks();

                        ViewBag.BookId = new SelectList(pendingBooks, "Id", "Title");

                        return(View(model));
                    }
                }
            }

            return(View(model));
        }