Ejemplo n.º 1
0
        public ActionResult RentFilmToMember(string id, string memberId, DateTime startDate, DateTime endDate)
        {
            List <Member>    members          = MemberRepository.GetMembers();
            SortMemberByName sortMemberByName = new SortMemberByName();

            members.Sort(sortMemberByName);

            if (RentRepository.IsStartDateCorrect(startDate))
            {
                if (RentRepository.IsEndDateCorrect(endDate, startDate))
                {
                    ObjectId RentingMemberId = new ObjectId(memberId);
                    Member   member          = MemberRepository.GetMemberById(RentingMemberId);

                    ObjectId rentingFilmId = new ObjectId(id);
                    Film     film          = FilmRepository.GetFilmById(rentingFilmId);

                    Rent rent = new Rent(member, null, film, startDate, endDate);

                    if (FilmRepository.FilmIsFreeToRent(rent))
                    {
                        RentRepository.CreateRent(rent);
                        return(Redirect($"/Rents/MemberRents/{memberId}"));
                    }
                    else
                    {
                        TempData["textmsg"] = "<script>alert('This book is not free to Rent in this entered date period. Please try another dates');</script>";
                        return(View(members));
                    }
                }
                else
                {
                    TempData["textmsg"] = "<script>alert('You entered a date before rent start date. Please try a date after rent start date');</script>";
                    return(View(members));
                }
            }
            else
            {
                TempData["textmsg"] = "<script>alert('You entered a date before today date. Please try a date after today date');</script>";
                return(View(members));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates an rent object and saves in data base
        /// </summary>
        private void RentItem()
        {
            Console.Clear();
            Console.WriteLine("Rent an ITEM");
            Console.WriteLine("------------\n");

            List <Member> members = ShowMembers();
            int           id;

            while (true)
            {
                Console.Write("\nEnter member's ID to start rent: ");
                string inputedMemberId = Console.ReadLine();

                if (inputedMemberId.Length != 0 && IsDigitsOnly(inputedMemberId) && int.Parse(inputedMemberId) > 0 && int.Parse(inputedMemberId) <= members.Count)
                {
                    id = int.Parse(inputedMemberId) - 1;
                    break;
                }
            }

            Member rentingMember = members[id]; //Creates renting member to rent

            Console.WriteLine("\n** Select ITEM category **");

            string itemType = "";

            while (itemType.ToLower() != "b" && itemType.ToLower() != "f")
            {
                Console.Write("Select (b)ook or (f)ilm: ");
                itemType = Console.ReadLine();
            }

            if (itemType.ToLower() == "b")//If item to rent is a book
            {
                Console.Clear();
                Console.WriteLine($"You are goting to rent a BOOK to '{rentingMember.Name}'\n");

                List <Book> books = ShowBooks();

                if (books.Count == 0)
                {
                    PressKeyToGoBackToRentMenu();
                }

                int bookId;
                while (true)
                {
                    Console.Write("\nEnter BOOK ID you want to rent: ");
                    string inputBookId = Console.ReadLine();
                    if (inputBookId.Length != 0 && IsDigitsOnly(inputBookId) && int.Parse(inputBookId) > 0 && int.Parse(inputBookId) <= books.Count)
                    {
                        bookId = int.Parse(inputBookId) - 1;
                        break;
                    }
                }

                Book rentingBook = books[bookId];//Creates renting book to rent

                List <Rent> AllrentedThisBook = RentRepository.GetAllSameBookRented(rentingBook);

                foreach (Rent thisBookRent in AllrentedThisBook)
                {
                    Console.WriteLine($"{thisBookRent.RentedBook.Title} is rented from {thisBookRent.StartDate} to {thisBookRent.EndDate}");
                }

                DateTime startDate = GetStartDate();
                DateTime endDate   = GetEndDate(startDate);

                Rent newRent = new Rent(rentingMember, rentingBook, null, startDate, endDate); //Creates a new rent object

                if (BookRepository.BookIsFreeToRent(newRent))
                {
                    RentRepository.CreateRent(newRent);//Inserts a rent in db
                    Console.WriteLine($"\n'{rentingMember.Name}' rented '{rentingBook.Title}' SUCCESSFULLY");
                }
                else
                {
                    Console.WriteLine("\n** All copies of this book are rented out between these entered dates **");
                }
            }



            else if (itemType.ToLower() == "f")//If item to rent is a film
            {
                Console.Clear();
                Console.WriteLine($"You are goting to rent a Film to '{rentingMember.Name}'\n");

                List <Film> films = ShowFilms();

                if (films.Count == 0)
                {
                    PressKeyToGoBackToRentMenu();
                }

                int filmId;
                while (true)
                {
                    Console.Write("\nEnter FILM ID you want to rent: ");
                    string inputFilmId = Console.ReadLine();
                    if (inputFilmId.Length != 0 && IsDigitsOnly(inputFilmId) && int.Parse(inputFilmId) > 0 && int.Parse(inputFilmId) <= films.Count)
                    {
                        filmId = int.Parse(inputFilmId) - 1;
                        break;
                    }
                }

                Film rentingFilm = films[filmId];//Creates renting book to rent

                List <Rent> AllrentedThisFilm = RentRepository.GetAllSameFilmRented(rentingFilm);

                foreach (Rent thisFilmRent in AllrentedThisFilm)
                {
                    Console.WriteLine($"{thisFilmRent.RentedFilm.Title} is rented from {thisFilmRent.StartDate} to {thisFilmRent.EndDate}");
                }

                DateTime startDate = GetStartDate();
                DateTime endDate   = GetEndDate(startDate);

                Rent newRent = new Rent(rentingMember, null, rentingFilm, startDate, endDate);

                if (FilmRepository.FilmIsFreeToRent(newRent))
                {
                    RentRepository.CreateRent(newRent);
                    Console.WriteLine($"\n'{rentingMember.Name}' rented '{rentingFilm.Title}' SUCCESSFULLY");
                }
                else
                {
                    Console.WriteLine("\n** All copies of this book are rented out between these entered dates **");
                }
            }
            PressKeyToGoBackToRentMenu();
        }