def add_client(self, the_id, name, from_undo=False): ''' Creates a client object that will be added to the list of clients from the repository :param from_undo: False if the function eas not called from the undo option, True otherwise :param the_id: the client id (int) :param name: the client name (string) ''' if self.find_by_id(the_id) is not None: raise IdError("Id already exists !") self._repository.add(Client(the_id, name)) if not from_undo: function = FunctionCall(self.add_client, the_id, name, True) reverse_function = FunctionCall(self.delete_client, the_id, True) operation = Operation(function, reverse_function) self._undo_service.record_operation(operation)
def update_client(self, the_id, name, from_undo=False): ''' Creates a new client object and calls the update method from the repository for updating the client in the repo :param from_undo: False if the function eas not called from the undo option, True otherwise :param the_id: the client's id :param name: the client's name ''' if self.find_by_id(the_id) is None: raise IdError("Id does not exists") old_client = self.search_by_id(the_id) old_client.name = name if not from_undo: function = FunctionCall(self.update_client, the_id, name, True) reverse_function = FunctionCall(self.update_client, old_client.id, old_client.name, True) operation = Operation(function, reverse_function) self._undo_service.record_operation(operation)
def delete_client(self, the_id, from_undo=False): ''' Calls the delete method from the repository in order to delete the client, by the id :param from_undo: False if the function eas not called from the undo option, True otherwise :param the_id: the client's id ''' if self.find_by_id(the_id) is None: raise IdError("Id does not exists") client = self.find_by_id(the_id) self._repository.delete(client) if not from_undo: function = FunctionCall(self.delete_client, the_id, True) reverse_function = FunctionCall(self.add_client, the_id, client.name, True) operation = Operation(function, reverse_function) cascaded_operation = CascadedOperation() cascaded_operation.add(operation) self._undo_service.record_operation(cascaded_operation)
def add_movie(self, the_id, title, description, genre, from_undo=False): ''' Creates a movie object that will be added to the list of movies from the repository :param from_undo: False if the function eas not called from the undo option, True otherwise :param the_id: the movie id :param title: the movie title :param description: the movie description :param genre: the movie genre ''' if self.find_by_id(the_id) is not None: raise IdError("Id already exists !") self._repository.add(Movie(the_id, title, description, genre)) if not from_undo: function = FunctionCall(self.add_movie, the_id, title, description, genre, True) reverse_function = FunctionCall(self.delete_movie, the_id, True) operation = Operation(function, reverse_function) self._undo_service.record_operation(operation)
def delete_movie(self, the_id, from_undo=False): ''' Calls the delete method from the repository in order to delete the movie, by the id :param from_undo: False if the function eas not called from the undo option, True otherwise :param the_id: the movie id ''' if self.find_by_id(the_id) is None: raise IdError("Id does not exists") movie = self.search_by_id(the_id) self._repository.delete(movie) if not from_undo: function = FunctionCall(self.delete_movie, the_id, True) reverse_function = FunctionCall(self.add_movie, the_id, movie.title, movie.description, movie.genre, True) operation = Operation(function, reverse_function) cascaded_operation = CascadedOperation() cascaded_operation.add(operation) self._undo_service.record_operation(cascaded_operation)
def update_movie(self, the_id, title, description, genre, from_undo=False): ''' Creates a new movie object and calls the update method from the repository for updating the movie in the repo :param from_undo: False if the function eas not called from the undo option, True otherwise :param the_id: the movie id :param title: the movie title :param description: the movie description :param genre: the movie genre ''' if self.find_by_id(the_id) is None: raise IdError("Id does not exists") movie = self.search_by_id(the_id) movie.title = title movie.genre = genre movie.description = description if not from_undo: function = FunctionCall(self.update_movie, movie.id, movie.title, movie.description, movie.genre, True) reverse_function = FunctionCall(self.update_movie, movie.id, movie.title, movie.description, movie.genre, True) operation = Operation(function, reverse_function) self._undo_service.record_operation(operation)
def exists(service, the_id, msg): if service.find_by_id(the_id) is None: raise IdError("Invalid id for " + msg)