Example #1
0
    def add_rental(self, book_id, client_id):
        """
        Function to add a rental to the rental repository with the book and client IDs given by the parameters. The
        function creates a new 'Rental' entity which then passes through the 'RentalValidator'. In case any of the
        rental's parameters are incorrect, the rental is not added but an error is raised instead. Otherwise, the
        function checks whether the book is available for rent. If it is not, then no rental is added but an error is
        raised instead. Otherwise, the rental is added to the rental repository.
        :param book_id: integer, holds the ID value of the book that is rented.
        :param client_id: integer, holds the ID value of the client that rents the book.
        """
        self._rental_validator.validate_book_and_client_ids(book_id, client_id)

        book_id = int(book_id)
        client_id = int(client_id)

        is_valid_book = self.is_book_id_in_repository(book_id)
        if is_valid_book:
            is_valid_client = self.is_client_id_in_repository(client_id)
            if is_valid_client:
                is_book_available = self.is_book_available_by_book_id(book_id)
                if is_book_available:
                    rental_id = self._rental_repository.get_next_rental_id()
                    rented_date = date.today()
                    returned_date = None
                    rental = Rental(rental_id, book_id, client_id, rented_date,
                                    returned_date)
                    self._rental_validator.validate_rental(rental)
                    self._rental_repository.add_rental(rental)
                else:
                    raise RepoError("Book is currently rented. ")
            else:
                raise RepoError("Client ID not found. ")
        else:
            raise RepoError("Book ID not found. ")
Example #2
0
 def remove_client_by_name(self, client_name_as_string):
     """
     Function to remove the client from the client repository with the name given by the parameter. The function
     tries to find the positional index of the client in the repository. If no client is found, no client is removed
     but an error is raised instead. Otherwise, the client is removed from the client repository.
     :param client_name_as_string: string, holds the name of the client to be removed from the repository.
     """
     client_index, _ = self.find_client_by_name(client_name_as_string)
     if client_index is None:
         raise RepoError("Name not found. ")
     self._client_repository.remove_client_by_index(client_index)
Example #3
0
    def return_rental_by_book_id(self, book_id):
        """
        Function to return the rental by the 'book_id'. The function verifies if the 'book_id' is valid. If it is not,
        then no rental is returned but an error is raised instead. Otherwise, the function searches for the book in the
        book repository. If no book is found, no rental is returned but an error is raised instead. Otherwise, the ID of
        rental of the book is looked for. If no rental ID is found active for the book at the moment, no rental is
        returned but an error is raised instead. Otherwise, the rental is marked as returned with the 'returned_date' of
        today.
        :param book_id: integer, holds the ID of the book whose rental will be returned.
        """
        self._rental_validator.validate_book_and_client_ids(book_id, 5)
        book_id = int(book_id)

        is_valid_book = self.is_book_id_in_repository(book_id)
        if not is_valid_book:
            raise RepoError("Book ID not found. ")

        rental_id = self.find_rental_id_by_book_id(book_id)
        if rental_id is None:
            raise RepoError("Book is not rented. ")

        self.return_rental_by_id(rental_id)
Example #4
0
    def __ui_search_client_by_name(self):
        client_name = input("Client name=")
        list_of_matching_clients = self._client_service.find_all_clients_matching_name(
            client_name.lower())

        number_of_matching_clients = len(list_of_matching_clients)
        if number_of_matching_clients == 0:
            raise RepoError(f"No clients found by name: {client_name}")

        for client in list_of_matching_clients:
            client_current_rentals = self._rental_service.get_client_active_rentals(
                client.id)
            print(f"{client.id} - {client.name}: {client_current_rentals}")
Example #5
0
 def return_rental_by_id(self, rental_id):
     """
     Function to return a rental by the 'rental_id'. The function verifies if the 'rental_id' is valid. If it is not,
     then no rental is returned but an error is raised instead. Otherwise, the function searches for the rental in
     the rental repository. If no rental is found, no rental is returned but an error is raised instead. Otherwise,
     the rental is marked as returned with the current date as the 'returned_date'.
     :param rental_id: integer, holds the ID of the rental to be returned.
     """
     try:
         rental_id = int(rental_id)
     except ValueError:
         raise ValidError("Rental ID must have natural number value. ")
     rental_index = self.find_rental_index_by_id(rental_id)
     if rental_index is None:
         raise RepoError("Rental ID not found. ")
     self._rental_repository.return_rental_by_index(rental_index)
Example #6
0
 def update_client_by_name(self, old_name_as_string, new_name_as_string):
     """
     Function to update the details of an already existing client from the client repository. The function tries to
     find the client in the client repository. If no client is found, no client is updated but an error is raised
     instead. Otherwise, the client's new details are passed through a 'ClientValidator'. If the validation fails,
     no client is updated but an error is raised instead. Otherwise, the client is updated with the new details.
     :param old_name_as_string: string, holds the name of the client to be updated in the repository.
     :param new_name_as_string: string, holds the name the client will be updated with.
     """
     client_index, client = self.find_client_by_name(old_name_as_string)
     if client is None:
         raise RepoError("Name not found. ")
     client_id = client.id
     new_client = Client(client_id, new_name_as_string)
     self._client_validator.validate_client(new_client)
     self._client_repository.update_client_by_name(client_index, new_client)
Example #7
0
    def __ui_search_client_by_id(self):
        client_id = input("Client ID=")
        try:
            client_id = int(client_id)
        except ValueError:
            raise ValueError("Client ID must have a natural integer value.")

        list_of_matching_clients = self._client_service.find_all_clients_matching_id(
            client_id)
        number_of_matching_clients = len(list_of_matching_clients)

        if number_of_matching_clients == 0:
            raise RepoError(f"No clients found by ID: {client_id}")

        for client in list_of_matching_clients:
            client_current_rentals = self._rental_service.get_client_active_rentals(
                client.id)
            print(f"{client.id} - {client.name}: {client_current_rentals}")
Example #8
0
    def __ui_search_book_by_author(self):
        book_author = input("Book author=")
        list_of_matching_books = self._book_service.find_all_books_matching_author(
            book_author.lower())

        number_of_matching_books = len(list_of_matching_books)
        if number_of_matching_books == 0:
            raise RepoError(f"No books found by title: {book_author}")

        for book in list_of_matching_books:
            book_currently_rented = self._rental_service.get_book_rental_status(
                book.id)
            print(f"{book.id} - '{book.title}' by {book.author}: ", end="")
            if book_currently_rented is None:
                book_currently_rented = "AVAILABLE"
                print_green(book_currently_rented, "\n")
            else:
                book_currently_rented = "UNAVAILABLE"
                print_red(book_currently_rented, "\n")
Example #9
0
 def update_book(self, old_title_as_string, old_author_name_as_string,
                 new_title_as_string, new_author_name_as_string):
     """
     Function to update the details of an already existing book from the book repository that has the title and
     author name as the given parameters. The book is checked if it exists in the book repository. If no book is
     found, then no book updates but an error is raised instead. Otherwise, the updated book goes through a book
     validator. If it does not pass the validation, no book is updated but an error is raised instead. Otherwise,
     the book gets updated in the book repository.
     :param old_title_as_string: string, holds the title of the book to be updated.
     :param old_author_name_as_string: string, holds the name of the author of the book to be updated
     :param new_title_as_string: string, holds the new title the book will be updated with.
     :param new_author_name_as_string: holds the new author name the book will be updated with.
     """
     book_index, book_to_update = self.find_book_by_title_and_author(
         old_title_as_string, old_author_name_as_string)
     if book_to_update is None:
         raise RepoError("Book not found. ")
     book_id = book_to_update.id
     updated_book = Book(book_id, new_title_as_string,
                         new_author_name_as_string)
     self._book_validator.validate_book(updated_book)
     self._book_repository.update_book(book_index, updated_book)
Example #10
0
    def __ui_search_book_by_id(self):
        book_id = input("Book ID=")
        try:
            book_id = int(book_id)
        except ValueError:
            raise ValueError("Book ID must have a natural integer value.")

        list_of_matching_books = self._book_service.find_all_books_matching_id(
            book_id)

        number_of_matching_books = len(list_of_matching_books)
        if number_of_matching_books == 0:
            raise RepoError(f"No books found by ID: {book_id}")

        for book in list_of_matching_books:
            book_currently_rented = self._rental_service.get_book_rental_status(
                book.id)
            print(f"{book.id} - '{book.title}' by {book.author}: ", end="")
            if book_currently_rented is None:
                book_currently_rented = "AVAILABLE"
                print_green(book_currently_rented, "\n")
            else:
                book_currently_rented = "UNAVAILABLE"
                print_red(book_currently_rented, "\n")