Exemple #1
0
        public ActionResult Create(string memberId, string bookId, string movieId, DateTime startDate, DateTime endDate)
        {
            ObjectId memberObjectId = new ObjectId(memberId);
            Member   member         = MemberRepository.GetMemberById(memberObjectId);


            try
            {
                if (movieId == "Empty")
                {
                    Book book      = GetBook(bookId);
                    Loan loan      = new Loan(startDate, endDate, member, book);
                    bool hasCopies = BookRepository.BookIsFreeToLoan(loan);

                    if (hasCopies)
                    {
                        LoanRepository.LoanBook(book, member, startDate, endDate);
                    }
                    else
                    {
                        ListModel viewModel = new ListModel();
                        viewModel.ErrorMessage = "Choose another date and is not available for loan";
                        return(View(viewModel));
                    }
                }


                else if (bookId == "Empty")
                {
                    Movie movie     = GetMovie(movieId);
                    Loan  loan      = new Loan(startDate, endDate, member, movie);
                    bool  hasCopies = MovieRepository.MovieIsFreeToLoan(loan);

                    if (hasCopies)
                    {
                        LoanRepository.LoanMovie(movie, member, startDate, endDate);
                    }
                    else
                    {
                        ListModel listModel = new ListModel();
                        listModel.ErrorMessage = "Choose another date and is not available for loan";
                        return(View(listModel));
                    }
                }

                else
                {
                    Book  book  = GetBook(bookId);
                    Movie movie = GetMovie(movieId);

                    Loan bookLoan  = new Loan(startDate, endDate, member, book);
                    Loan movieLoan = new Loan(startDate, endDate, member, movie);

                    bool hasBookCopies  = BookRepository.BookIsFreeToLoan(bookLoan);
                    bool hasMovieCopies = MovieRepository.MovieIsFreeToLoan(movieLoan);

                    if (hasBookCopies && hasMovieCopies)
                    {
                        LoanRepository.LoanBook(book, member, startDate, endDate);
                        LoanRepository.LoanMovie(movie, member, startDate, endDate);
                    }
                    else if (!hasBookCopies && !hasMovieCopies)
                    {
                        ListModel listModel = new ListModel();
                        listModel.ErrorMessage = "Both Movie and Book are unavaliable";

                        return(View(listModel));
                    }
                    else if (!hasBookCopies)
                    {
                        ListModel listModel = new ListModel();
                        listModel.ErrorMessage = "Book is not avaliable";

                        return(View(listModel));
                    }
                    else if (!hasMovieCopies)
                    {
                        ListModel listModel = new ListModel();
                        listModel.ErrorMessage = "Movie is not avaliable";

                        return(View(listModel));
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }