public ActionResult BookRents(string id) { ObjectId bookId = new ObjectId(id); Book book = BookRepository.GetBookById(bookId); List <Rent> rents = RentRepository.GetAllSameBookRented(book); return(View(rents)); }
/// <summary> /// Checks if book if free to rent between entered start and end rent's date /// </summary> /// <param name="book"></param> /// <returns>True if book if free to rent and false if not</returns> private bool BookIsFreeToRent(Rent rent) { List <Rent> allSameBookRented = RentRepository.GetAllSameBookRented(rent.RentedBook); int rentedCopies = 0; for (int i = 0; i < allSameBookRented.Count; i++) { if (rent.EndDate >= allSameBookRented[i].StartDate && rent.StartDate <= allSameBookRented[i].EndDate) { rentedCopies++; } } if (rentedCopies >= rent.RentedBook.Copies) { return(false); } return(true); }
/// <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(); }
/// <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 = DateTime.Now; DateTime endDate = DateTime.Now; while (true) { int startDateResultat = 2; try { Console.Write("\nEnter rent's START DATE(yyyy,mm,dd): "); startDate = DateTime.Parse(Console.ReadLine()).Date; startDateResultat = DateTime.Compare(DateTime.Now.Date, startDate);//Checks to not enter a date before today's date } catch (Exception) { Console.WriteLine("You entered a WRONG DATE FORMAT. Must be yyyy/mm/dd"); } if (startDateResultat <= 0)//checks if date is in right format and start date is not before today's date { Console.WriteLine($"Rent start date is {startDate}"); break; } else if (startDateResultat > 0) { Console.WriteLine("Don't Entere a date before Today's date"); } } while (true) { int endDateResultat = 2; try { Console.Write("\nEnter rent's END DATE(yyyy,mm,dd): "); endDate = DateTime.Parse(Console.ReadLine()).Date; endDateResultat = DateTime.Compare(endDate, startDate);//Checks to not enter an end date before start date } catch (Exception) { Console.WriteLine("You entered a WRONG DATE FORMAT. Must be yyyy/mm/dd"); } if (endDateResultat > 0)//checks if date is in right format and end date is not before start date { Console.WriteLine($"Rent end date is {endDate}"); break; } else { Console.WriteLine("Don't Entere a date before rent's start date"); } } Rent newRent = new Rent(rentingMember, rentingBook, null, startDate, endDate); //Creates a new rent object if (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 = DateTime.Now; DateTime endDate = DateTime.Now; while (true) { int startDateResultat = 2; try { Console.Write("\nEnter rent's START DATE(yyyy,mm,dd): "); startDate = DateTime.Parse(Console.ReadLine()).Date; startDateResultat = DateTime.Compare(DateTime.Now.Date, startDate);//Checks to not enter a date before today's date } catch (Exception) { Console.WriteLine("You entered a WRONG DATE FORMAT. Must be yyyy/mm/dd"); } if (startDateResultat <= 0)//checks if date is in right format and start date is not before today's date { Console.WriteLine($"Rent start date is {startDate}"); break; } else { Console.WriteLine("Don't entere a date before Today's date"); } } while (true) { int endDateResultat = 2; try { Console.Write("\nEnter rent's END DATE(yyyy,mm,dd): "); endDate = DateTime.Parse(Console.ReadLine()).Date; endDateResultat = DateTime.Compare(endDate, startDate);//Checks to not enter an end date before start date } catch (Exception) { Console.WriteLine("You entered a WRONG DATE FORMAT. Must be yyyy/mm/dd"); } if (endDateResultat > 0)//checks if date is in right format and end date is not before start date { Console.WriteLine($"Rent end date is {endDate}"); break; } else { Console.WriteLine("Don't Entere a date before rent's start date"); } } Rent newRent = new Rent(rentingMember, null, rentingFilm, startDate, endDate); if (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(); }