Exemplo n.º 1
0
        public ActionResult AddLoan(LoanModel loanModel)
        {
            try
            {
                LoanRepository          loanRepository          = new LoanRepository();
                copiesRepository        copiesRepository        = new copiesRepository();
                BookRepository          bookRepository          = new BookRepository();
                ContentRatingRepository contentRatingRepository = new ContentRatingRepository();
                MemberRepository        memberRepository        = new MemberRepository();
                MembershipRepository    membershipRepository    = new MembershipRepository();

                List <MemberModel> memberList   = new List <MemberModel>();
                LoanModel          newLoanModel = new LoanModel();
                newLoanModel.MemberList = memberRepository.GetAllMember();

                CopiesModel copiesModel = new CopiesModel();
                copiesModel = copiesRepository.SearchCopyById(loanModel.CopiesId);

                BookModel bookModel = new BookModel();
                bookModel = bookRepository.SearchBookById(copiesModel.BookId);

                if (bookModel.BookType.Equals("Reference Book"))
                {
                    ViewBag.Message = "Reference Books Can't be Loaned";
                    return(View(newLoanModel));
                }

                ContentRatingModel contentRatingModel = new ContentRatingModel();
                contentRatingModel = contentRatingRepository.GetByContentId(bookModel.ContentRatingId);

                MemberModel memberModel = new MemberModel();
                memberModel = memberRepository.SearchMemberById(loanModel.MembershipId);

                if (contentRatingModel.ContentRatingName.Equals("18+"))
                {
                    var today    = DateTime.Today;
                    var age      = Convert.ToDateTime(memberModel.Dob);
                    var todayAge = today.Year - age.Year;
                    if (todayAge < 18)
                    {
                        ViewBag.Message = "Age Restricted Books Are Not Allowed";
                        return(View(newLoanModel));
                    }
                }

                MembershipModel membershipModel = new MembershipModel();
                membershipModel = membershipRepository.GetMembershipByID(memberModel.MembershipId);

                BookCopies bookCopiesModel = new BookCopies();
                bookCopiesModel = loanRepository.GetAllLoanByMemberID(memberModel.MemberId);

                if (bookCopiesModel.Quantities >= membershipModel.NoOfBooks)
                {
                    ViewBag.Message = "No More Books Can be Loaned";
                    return(View(newLoanModel));
                }

                DateTime returnedDate = Convert.ToDateTime(loanModel.LoanDate);
                loanModel.ReturnDate = returnedDate.AddDays(14).ToString("yyyy MMMM dd");
                loanRepository.AddLoan(loanModel);
                copiesRepository.UpdateCopiesStatus(loanModel.CopiesId);

                var profileData = Session["UserProfile"] as UserSession;
                var logModel    = new LogModel
                {
                    UserId    = profileData.UserID,
                    TableName = "Loan",
                    Activity  = "Added Loan",
                    LogDate   = DateTime.Now
                };
                var logRepository = new logRepository();
                logRepository.AddLog(logModel);
                return(RedirectToAction("Dashboard", "Home"));
            }
            catch
            {
                return(RedirectToAction("Index", "Home"));
            }
        }