public ActionResult Create(BookRentViewModel bookRentVM)
        {
            if (ModelState.IsValid)
            {
                var email = bookRentVM.Email;
                var book  = db.Books.Where(b => b.ISBN.Equals(bookRentVM.ISBN)).SingleOrDefault();

                var userDetails = from u in db.Users
                                  where u.Email.Equals(email)
                                  select new
                {
                    u.Id,
                    u.FirstName,
                    u.LastName,
                    u.Email
                };

                var chargeRate = from u in db.Users
                                 join m in db.MembershipTypes on u.MembershipTypeId equals m.Id
                                 where u.Email.Equals(email)
                                 select new
                {
                    m.ChargeRateOneMonth,
                    m.ChargeRateSixMonth
                };
                var rentalPrice = 0.0;

                if (bookRentVM.RentalDuration.Equals(SD.OneMonthValue))
                {
                    rentalPrice = Convert.ToDouble(book.Price) * Convert.ToDouble(chargeRate.ToList()[0].ChargeRateOneMonth) / 100;
                }

                if (bookRentVM.RentalDuration.Equals(SD.SixMonthValue))
                {
                    rentalPrice = Convert.ToDouble(book.Price) * Convert.ToDouble(chargeRate.ToList()[0].ChargeRateSixMonth) / 100;
                }

                BookRent bookRentToAddToDb = new BookRent
                {
                    UserId           = userDetails.ToList()[0].Id,
                    BookId           = book.Id,
                    RentalDuration   = bookRentVM.RentalDuration,
                    RentalPrice      = rentalPrice,
                    Status           = BookRent.StatusEnum.Approved,
                    StartDate        = bookRentVM.StartDate,
                    ActualEndDate    = bookRentVM.ActualEndDate,
                    AdditionalCharge = bookRentVM.AdditionalCharge,
                    ScheduledEndDate = bookRentVM.ScheduledEndDate
                };

                db.BookRents.Add(bookRentToAddToDb);
                book.Availibility -= 1;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(bookRentVM));
        }
        // GET: BookDetail
        public ActionResult Index(int id)
        {
            var bookSelected = db.Books.FirstOrDefault(b => b.Id == id);
            var userId       = User.Identity.GetUserId();

            if (bookSelected == null)
            {
                return(HttpNotFound());
            }

            var user = db.Users.SingleOrDefault(u => u.Id.Equals(userId));

            var chargeRate = from u in db.Users
                             join m in db.MembershipTypes on u.MembershipTypeId equals m.Id
                             where u.Id.Equals(userId)
                             select new
            {
                m.ChargeRateOneMonth,
                m.ChargeRateSixMonth
            };

            var rentalPrice        = 0.0;
            var chargeRateOneMonth = 0.0;
            var chargeRateSixMonth = 0.0;

            if (user != null && !User.IsInRole(SD.AdminUserRole))
            {
                chargeRateOneMonth = Convert.ToDouble(bookSelected.Price) * Convert.ToDouble(chargeRate.ToList()[0].ChargeRateOneMonth) / 100;
                chargeRateSixMonth = Convert.ToDouble(bookSelected.Price) * Convert.ToDouble(chargeRate.ToList()[0].ChargeRateSixMonth) / 100;
            }


            var bookRentVM = new BookRentViewModel
            {
                UserId              = userId,
                BookId              = bookSelected.Id,
                RentalPrice         = rentalPrice,
                ImageUrl            = bookSelected.ImageUrl,
                Title               = bookSelected.Title,
                Author              = bookSelected.Author,
                ISBN                = bookSelected.ISBN,
                Price               = bookSelected.Price,
                Description         = bookSelected.Description,
                rentalPriceOneMonth = chargeRateOneMonth,
                rentalPriceSixMonth = chargeRateSixMonth,
                GenreId             = bookSelected.GenreId,
                Genre               = db.Genres.Where(g => g.Id == bookSelected.GenreId).FirstOrDefault(),
                DateAdded           = bookSelected.DateAdded,
                ProductDimensions   = bookSelected.ProductDimensions,
                Availibility        = bookSelected.Availibility
            };

            return(View(bookRentVM));
        }
 public ActionResult Approve(BookRentViewModel bookRentVMModel)
 {
     if (ModelState.IsValid)
     {
         var book     = db.Books.Find(bookRentVMModel.BookId);
         var bookRent = db.BookRents.Find(bookRentVMModel.Id);
         bookRent.Status = BookRent.StatusEnum.Approved;
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
 public ActionResult Decline(BookRentViewModel bookRentVMModel)
 {
     if (ModelState.IsValid)
     {
         var book     = db.Books.Find(bookRentVMModel.BookId);
         var bookRent = db.BookRents.Find(bookRentVMModel.Id);
         bookRent.Status    = BookRent.StatusEnum.Rejected;
         book.Availibility += 1;
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
        public ActionResult Create(string isbn = null, string title = null)
        {
            if (isbn != null && title != null)
            {
                var model = new BookRentViewModel
                {
                    Title = title,
                    ISBN  = isbn
                };
                return(View(model));
            }

            return(View(new BookRentViewModel()));
        }
        private BookRentViewModel getVMFromBookRent(BookRent bookRent)
        {
            var userDetails = from u in db.Users
                              where u.Id.Equals(bookRent.UserId)
                              select new
            {
                u.FirstName,
                u.LastName,
                u.Email,
                u.Birthday,
                u.Phone,
                u.MembershipTypeId,
            };

            var book = db.Books.Find(bookRent.BookId);


            var model = new BookRentViewModel
            {
                Id               = bookRent.Id,
                UserId           = bookRent.UserId,
                BookId           = bookRent.BookId,
                StartDate        = bookRent.StartDate,
                ActualEndDate    = bookRent.ActualEndDate,
                ScheduledEndDate = bookRent.ScheduledEndDate,
                AdditionalCharge = bookRent.AdditionalCharge,
                RentalPrice      = bookRent.RentalPrice,
                RentalDuration   = bookRent.RentalDuration,
                Pages            = book.Pages,
                Price            = book.Price,
                Status           = bookRent.Status.ToString(),
                FirstName        = userDetails.ToList()[0].FirstName,
                LastName         = userDetails.ToList()[0].LastName,
                Email            = userDetails.ToList()[0].Email,
                Birthday         = userDetails.ToList()[0].Birthday,
                Phone            = userDetails.ToList()[0].Phone,
                Author           = book.Author,
                Availibility     = book.Availibility,
                Description      = book.Description,
                GenreId          = book.GenreId,
                DateAdded        = book.DateAdded,
                ImageUrl         = book.ImageUrl,
                ISBN             = book.ISBN,
                PublicationDate  = book.PublicationDate,
                Title            = book.Title,
                Genre            = db.Genres.FirstOrDefault(g => g.Id == book.GenreId)
            };

            return(model);
        }
 public ActionResult Return(BookRentViewModel bookRentVMModel)
 {
     if (ModelState.IsValid)
     {
         var book     = db.Books.Find(bookRentVMModel.BookId);
         var bookRent = db.BookRents.Find(bookRentVMModel.Id);
         bookRent.Status           = BookRent.StatusEnum.Closed;
         bookRent.ActualEndDate    = DateTime.Now;
         bookRent.AdditionalCharge = bookRentVMModel.AdditionalCharge;
         book.Availibility        += 1;
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
        public ActionResult Reserve(BookRentViewModel bookRentVM)
        {
            string userId = User.Identity.GetUserId();
            var    user   = db.Users.Find(userId);

            if (user != null)
            {
                var book = db.Books.Find(bookRentVM.BookId);

                var rentalPrice = 0.0;

                var chargeRate = from u in db.Users
                                 join m in db.MembershipTypes on u.MembershipTypeId equals m.Id
                                 where u.Id.Equals(userId)
                                 select new
                {
                    m.ChargeRateOneMonth,
                    m.ChargeRateSixMonth
                };

                if (bookRentVM.RentalDuration.Equals(SD.OneMonthValue))
                {
                    rentalPrice = Convert.ToDouble(book.Price) * Convert.ToDouble(chargeRate.ToList()[0].ChargeRateOneMonth) / 100;
                }
                else
                {
                    rentalPrice = Convert.ToDouble(book.Price) * Convert.ToDouble(chargeRate.ToList()[0].ChargeRateSixMonth) / 100;
                }

                var bookRentToAddToDb = new BookRent
                {
                    UserId           = userId,
                    BookId           = book.Id,
                    ActualEndDate    = bookRentVM.ActualEndDate,
                    StartDate        = bookRentVM.StartDate,
                    AdditionalCharge = bookRentVM.AdditionalCharge,
                    RentalDuration   = bookRentVM.RentalDuration,
                    RentalPrice      = rentalPrice,
                    ScheduledEndDate = bookRentVM.ScheduledEndDate,
                    Status           = BookRent.StatusEnum.Requested
                };

                book.Availibility -= 1;
                db.BookRents.Add(bookRentToAddToDb);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(bookRentVM));
        }
 public ActionResult PickUp(BookRentViewModel bookRentVMModel)
 {
     if (ModelState.IsValid)
     {
         var book     = db.Books.Find(bookRentVMModel.BookId);
         var bookRent = db.BookRents.Find(bookRentVMModel.Id);
         bookRent.Status    = BookRent.StatusEnum.Rented;
         bookRent.StartDate = DateTime.Now;
         if (bookRent.RentalDuration.Equals(SD.SixMonthValue))
         {
             bookRent.ScheduledEndDate = DateTime.Now.AddMonths(Convert.ToInt32(SD.SixMonthValue));
         }
         else
         {
             bookRent.ScheduledEndDate = DateTime.Now.AddMonths(Convert.ToInt32(SD.OneMonthValue));
         }
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
Example #10
0
 public IActionResult ViewBookRent(BookRentViewModel bookview)
 {
     return(View());
 }