def retur(self,rid, returdate): """ Sets the returdate of the rent with bid and cid corresponding to the given ones Input: bid, cid - positive integer returdate - strings representing dates in format = "dd.mm.YYYY" Output: Exceptions: LibraryError - if the book has been returned already """ rentals = self.__rental_repo.get_rentals() for i in range(0,len(rentals)): if rentals[i].get_id() == rid : if rentals[i].get_returned_date() == None: r = rentals[i] r.set_returned_date(returdate) self.__rental_repo.update(r) elif rentals[i].get_returned_date() != None: raise LibraryError("! This book has already been returned by this client") redo = FunctionCall(self.retur, rid, returdate) undo = FunctionCall(self.undo_retur, rid) oper = Operations(undo, redo) self.__undo_ser.add(oper)
def remove_book(self, bid ): """ Removes the book with the given id from the repository Input: bid - positive integer """ book = Book(bid, None, None, None) b = self.__book_repo.remove(book) """ delete the rentals\ """ rentals = self.__rental_service.filter_rentals(bid, None) for r in rentals: self.__rental_service.remove_rental(r.get_id()) undo = FunctionCall(self.add_book,b.get_id(), b.get_title(), b.get_author(), b.get_description()) redo = FunctionCall(self.remove_book, bid) oper1 = Operations(undo, redo) co = CascadeOperations() co.add(oper1) if len(rentals)!= 0 : for r in rentals: rdate = date_to_str_format(r.get_rent_date()) ddate = date_to_str_format( r.get_due_date()) if r.get_returned_date() != None: retdate = date_to_str_format(r.get_returned_date()) undo = FunctionCall(self.__rental_service.rent,r.get_id(),r.get_b_id(), r.get_c_id(), rdate, ddate, retdate) else: undo = FunctionCall(self.__rental_service.rent,r.get_id(),r.get_b_id(), r.get_c_id(), rdate, ddate) redo = FunctionCall(self.__rental_service.remove_rental, r.get_id()) oper = Operations(undo, redo) co.add(oper) self.__undoService.add(co)
def remove_client(self, cid): """ Removes the client with the given id from the repository Input: cid - positive integer """ client = Client(cid, None) c = self.__client_repo.remove(client) """ Need to remove all rentals with this client! """ rentals = self.__rental_service.filter_rentals(None, cid) for r in rentals: self.__rental_service.remove_rental(r.get_id()) redo = FunctionCall(self.remove_client, cid) undo = FunctionCall(self.add_client, c.get_id(), c.get_name()) oper1 = Operations(undo, redo) co = CascadeOperations() co.add(oper1) for r in rentals: rdate = date_to_str_format(r.get_rent_date()) ddate = date_to_str_format(r.get_due_date()) if r.get_returned_date() != None: retdate = date_to_str_format(r.get_returned_date()) undo = FunctionCall(self.__rental_service.rent, r.get_id(), r.get_b_id(), r.get_c_id(), rdate, ddate, retdate) else: undo = FunctionCall(self.__rental_service.rent, r.get_id(), r.get_b_id(), r.get_c_id(), rdate, ddate) redo = FunctionCall(self.__rental_service.remove_rental, r.get_id()) oper = Operations(undo, redo) co.add(oper) self.__undo_service.add(co)
def update_client(self, cid, name): """ Creates a client with the given attributes and updates the client, from the repository, with the given id Input: cid - positive integer name - string """ client = Client(cid, name) self.__client_valid.valid_client(client) c = self.__client_repo.update(client) redo = FunctionCall(self.update_client, cid, name) undo = FunctionCall(self.update_client, c.get_id(), c.get_name()) oper = Operations(undo, redo) self.__undo_service.add(oper)
def update_book (self, bid, title, author, description = None): """ Creates a book with the given attributes and updates the book with the same id, already existing in the repository Input: bid - positive integer (already existing in one of the books from the repository) title, author - string description - string (default value the empty string) """ book = Book(bid, title, author, description) self.__book_valid.valid_book(book) b = self.__book_repo.update(book) redo = FunctionCall(self.update_book, bid, title, author, description) undo = FunctionCall(self.update_book, bid, b.get_title(), b.get_author(), b.get_description()) oper = Operations(undo, redo) self.__undoService.add(oper)
def add_client(self, cid, name): """ Creates a client with the given attributes and adds it to the repository Input: cid - positive integer name - string """ client = Client(cid, name) self.__client_valid.valid_client(client) self.__client_repo.add(client) """ If everything goes right we add the operation to the undo stack """ redo = FunctionCall(self.add_client, cid, name) undo = FunctionCall(self.remove_client, cid) oper = Operations(undo, redo) self.__undo_service.add(oper)
def add_book(self, bid, title, author, description = None): """ Creates a book with the given attributes and adds it in the repository Input: bid - positive integer title, author - string description - string (default value the empty string) """ book = Book(bid, title, author, description) self.__book_valid.valid_book(book) self.__book_repo.add(book) """ If everything works fine, we try to implement undo and redo command """ redo = FunctionCall(self.add_book, bid, title, author, description) undo = FunctionCall(self.remove_book, bid) oper = Operations(undo, redo) self.__undoService.add(oper)
def rent(self, rid, bid, cid, rentdate, duedate, returdate=None): """ Adds the rent, created using the given attributes, in the repository Input: rid, bid, cid - positive integer rentdate, duedate, returdate - string representing dates in format = "dd.mm.YYYY" the default value for returdate is None Exceptions: LibraryError - if the rent is not possible """ if not self.__can_rent(bid, cid): raise LibraryError("!!! The book or the client does not exist!") rent = Rental(rid, bid, cid, rentdate, duedate, returdate) self.__rental_valid.valid_rent(rent) self.__rental_repo.available_rent(rent) self.__rental_repo.add(rent) redo = FunctionCall(self.rent, rid, bid, cid, rentdate, duedate, returdate) undo = FunctionCall(self.remove_rental, rid) oper = Operations(undo, redo) self.__undo_ser.add(oper)