def ui_add_client(self):
     client_id = input("client id: ")
     name = input("Name: ")
     self.__client_service.add_client(client_id, name)
     RedoService.clear()
     UndoService.store_operation(self.__client_service,
                                 UndoHandler.UNDO_ADD_CLIENT, client_id)
def redo_remove_book(book_service, book_id):
    a = book_service.get_entities()
    title = a[book_id].title
    author = a[book_id].author
    book_service.remove_book(book_id)
    UndoService.store_operation(book_service, UndoHandler.UNDO_REMOVE_BOOK,
                                book_id, title, author)
 def ui_add_book(self):
     book_id = input("Book id: ")
     title = input("Title: ")
     author = input("Author: ")
     self.__book_service.add_book(book_id, title, author)
     RedoService.clear()
     UndoService.store_operation(self.__book_service,
                                 UndoHandler.UNDO_ADD_BOOK, book_id)
 def ui_add_rental(self):
     rental_id = input("Rental id: ")
     book_id = input("Book id: ")
     client_id = input("Client id: ")
     rented_date = input("Rented date in dd/mm/yyyy format: ")
     self.__rental_service.add_rental(rental_id, book_id, client_id,
                                      rented_date)
     RedoService.clear()
     UndoService.store_operation(self.__rental_service,
                                 UndoHandler.UNDO_ADD_RENTAL, rental_id)
def redo_update_client(client_service, client_id, name):
    """
    Handles redo update client operation
    """
    a = client_service.get_entities()
    old_name = a[client_id].name
    client_service.remove_client(client_id)
    client_service.add_client(client_id, name)
    UndoService.store_operation(client_service, UndoHandler.UNDO_UPDATE_CLIENT,
                                client_id, old_name)
def redo_update_book(book_service, book_id, title, author):
    """
    Handles redo update book operation
    """
    a = book_service.get_entities()
    old_title = a[book_id].title
    old_author = a[book_id].author
    book_service.remove_book(book_id)
    book_service.add_book(book_id, title, author)
    UndoService.store_operation(book_service, UndoHandler.UNDO_UPDATE_BOOK,
                                book_id, old_title, old_author)
 def ui_remove_client(self):
     client_id = input('Client id: ')
     try:
         a = self.__client_service.get_entities()
         name = a[client_id].name
         self.__client_service.remove_client(client_id)
         RedoService.clear()
         UndoService.store_operation(self.__client_service,
                                     UndoHandler.UNDO_REMOVE_CLIENT,
                                     client_id, name)
     except KeyError:
         print("There is no client with this id")
 def ui_remove_book(self):
     book_id = input('Book id: ')
     try:
         a = self.__book_service.get_entities()
         title = a[book_id].title
         author = a[book_id].author
         self.__book_service.remove_book(book_id)
         RedoService.clear()
         UndoService.store_operation(self.__book_service,
                                     UndoHandler.UNDO_REMOVE_BOOK, book_id,
                                     title, author)
     except KeyError:
         print("There is no book with this id")
 def ui_update_client(self):
     client_id = input("client id: ")
     name = input("New Name: ")
     try:
         a = self.__client_service.get_entities()
         old_name = a[client_id].name
         self.__client_service.remove_client(client_id)
         self.__client_service.add_client(client_id, name)
         RedoService.clear()
         UndoService.store_operation(self.__client_service,
                                     UndoHandler.UNDO_UPDATE_CLIENT,
                                     client_id, old_name)
     except KeyError:
         print("There is no such id")
 def ui_update_book(self):
     book_id = input("Book id: ")
     title = input("New Title: ")
     author = input("New Author: ")
     try:
         a = self.__book_service.get_entities()
         old_title = a[book_id].title
         old_author = a[book_id].author
         self.__book_service.remove_book(book_id)
         self.__book_service.add_book(book_id, title, author)
         RedoService.clear()
         UndoService.store_operation(self.__book_service,
                                     UndoHandler.UNDO_UPDATE_BOOK, book_id,
                                     old_title, old_author)
     except KeyError:
         print("There is no such id")
def redo_add_rental(rental_service, rental_id, book_id, client_id, rented_date,
                    return_date):
    rental_service.add_rental(rental_id, book_id, client_id, rented_date,
                              return_date)
    UndoService.store_operation(rental_service, UndoHandler.UNDO_ADD_RENTAL,
                                rental_id)
def redo_remove_client(client_service, client_id):
    a = client_service.get_entities()
    name = a[client_id].name
    client_service.remove_client(client_id)
    UndoService.store_operation(client_service, UndoHandler.UNDO_REMOVE_CLIENT,
                                client_id, name)
def redo_add_client(client_service, client_id, name):
    client_service.add_client(client_id, name)
    UndoService.store_operation(client_service, UndoHandler.UNDO_ADD_CLIENT,
                                client_id)
def redo_add_book(book_service, book_id, title, author):
    book_service.add_book(book_id, title, author)
    UndoService.store_operation(book_service, UndoHandler.UNDO_ADD_BOOK,
                                book_id)
class TestUndoService(unittest.TestCase):
    def setUp(self):
        """
        Prepare tests
        """
        self.rental_repository = Repository({})
        self.validator2 = RentalValidator()
        self.book_repository = Repository({})
        self.validator = BookValidator()
        self.client_repository = Repository({})
        self.validatorc = ClientValidator()
        self.undo_service = UndoService()
        self.redo_service = RedoService()
        self.book_service = BookService(self.book_repository, self.validator)
        self.client_service = ClientService(self.client_repository, self.validatorc)
        self.rental_service = RentalService(self.rental_repository, self.validator2, self.book_repository,
                                            self.client_repository)

    def test_undo_controller(self):
        """
        Tests for undo controller
        """
        self.assertRaises(UndoException, self.undo_service.do_undo)
        self.assertRaises(RedoException, self.redo_service.do_redo)

        self.book_service.add_book('12', "the name", 'By me')
        self.undo_service.store_operation(self.book_service, UndoHandler.UNDO_ADD_BOOK, '12')
        self.undo_service.do_undo()
        self.assertTrue(len(self.book_service.get_entities()) == 0)
        self.redo_service.store_operation(self.book_service, RedoHandler.REDO_ADD_BOOK, '12', "the name", "By me")

        self.redo_service.do_redo()
        self.assertTrue(len(self.book_service.get_entities()) == 1)
        self.book_service.add_book('2', "the name", 'By me')
        self.book_service.remove_book('2')
        self.undo_service.store_operation(self.book_service, UndoHandler.UNDO_REMOVE_BOOK, '2', "The Mandalorian", "Didu")
        self.undo_service.do_undo()

        self.undo_service.store_operation(self.book_service, UndoHandler.UNDO_UPDATE_BOOK, '2', "The Mandalorian","Didu")
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.book_service, RedoHandler.REDO_UPDATE_BOOK, '2', "The Mandalorian","Didu")
        self.redo_service.do_redo()

        self.client_service.add_client("10", "andrei")
        self.undo_service.store_operation(self.client_service, UndoHandler.UNDO_ADD_CLIENT, '10')
        self.undo_service.do_undo()
        self.assertTrue(len(self.client_service.get_entities()) == 0)
        self.redo_service.store_operation(self.client_service, RedoHandler.REDO_ADD_CLIENT, "10", "andrei")
        self.redo_service.do_redo()
        self.assertTrue(len(self.client_service.get_entities()) == 1)

        self.client_service.remove_client('10')
        self.undo_service.store_operation(self.client_service, UndoHandler.UNDO_REMOVE_CLIENT, '10', 'andrei')
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.client_service, RedoHandler.REDO_REMOVE_CLIENT, '10')
        self.redo_service.do_redo()

        self.client_service.add_client('2', "Didu")
        self.undo_service.store_operation(self.client_service, UndoHandler.UNDO_UPDATE_CLIENT, '2', "Didu")
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.client_service, RedoHandler.REDO_UPDATE_CLIENT, '2', 'Didu')
        self.redo_service.do_redo()

        self.rental_service.add_rental('1', '2', '2', '20/10/2020')
        self.undo_service.store_operation(self.rental_service, UndoHandler.UNDO_ADD_RENTAL, '1')
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.rental_service, RedoHandler.REDO_ADD_RENTAL, '1', '2', '2', '20/10/2020','')
        self.redo_service.do_redo()

        self.book_service.add_book('20', "book", 'By me')
        self.book_service.remove_book('20')
        self.undo_service.store_operation(self.book_service, UndoHandler.UNDO_REMOVE_BOOK, '20', "book", "By me")
        self.undo_service.do_undo()
        self.redo_service.store_operation(self.book_service, RedoHandler.REDO_REMOVE_BOOK, '20')
        self.redo_service.do_redo()

        self.redo_service.clear()