def init_rentals(self): ''' Initializes the repository of rentals with 3 default entities ''' self.repo_rentals.add_new_entity(Rental(1, 3, 2, datetime.date(2019, 5, 23), datetime.date(2019, 10, 2), datetime.date(2019, 11, 20))) self.repo_rentals.add_new_entity(Rental(2, 4, 1, datetime.date(2019, 7, 14), datetime.date(2019, 10, 14), datetime.date(2000, 1, 1))) self.repo_rentals.add_new_entity(Rental(3, 3, 5, datetime.date(2019, 5, 30), datetime.date(2019, 12, 30), datetime.date(2000, 1, 1)))
def rent_movie(self, id, client_id, movie_id, due_year, due_month, due_day): # parameters validations id = Validator.validate_positive_integer(id) client_id = Validator.validate_positive_integer(client_id) movie_id = Validator.validate_positive_integer(movie_id) #print(client_id) try: due_date = datetime.date(int(due_year), int(due_month), int(due_day)) except: raise InvalidDateError() # after these validations, there are still other validations to be performed # the following conditions are to be checked: # 1. A movie that was rented cannot be rented again, unless it was returned # 2. A client can rent a movie only if they have no rented movies that passed their due date for return repo_copy = self.__repo_rentals.get_repo_copy() Validator.validate_rental(repo_copy, client_id, movie_id) # All conditions being fulfilled, rental can be performed self.__repo_rentals.add_new_entity( Rental(id, client_id, movie_id, datetime.date.today(), due_date, datetime.date(2000, 1, 1)))
def return_movie(self, client_id, movie_id): client_id = Validator.validate_positive_integer(client_id) movie_id = Validator.validate_positive_integer(movie_id) # Validations to be performed: # 1. There exist a rental with this client and movie # 2. The movie has not been returned yet repo_copy = self.__repo_rentals.get_repo_copy() rental = Validator.validate_return(repo_copy, client_id, movie_id) self.repo_rentals.update_entity( rental.id, Rental(rental.id, client_id, movie_id, rental.rented_date, rental.due_date, datetime.date.today())) return rental
def rentBook(self, rentalId, clientId, bookID, rentedDate, dueDate, returnedDate): """ book to return a book parameters: """ clt = self.searchClient(clientId) bk = self.searchBook(bookID) if bk in [rental.getBook() for rental in self.getRentals()]: raise LibraryException("Book already rented to somebody!") for r in self.getRentals(): if r.getRentalID() == rentalId: raise LibraryException( "Already existing rental ID, choose another.") self._rentals.append( Rental(rentalId, bookID, clientId, rentedDate, dueDate, False))
def deepCopy(self, other): ''' Function to deepCopy another LibraryRepository to this (self) one ;copies all the data from another Repository to this one with no references of the objects (so that the states do not depend at all) params: other: another LibraryRepository ''' self._books = [ Book(book.getID(), book.getTitle(), book.getDescription(), book.getAuthor()) for book in other.getBooks() ] self._clients = [ Client(client.getID(), client.getName()) for client in other.getClients() ] self._rentals = [ Rental(rental.getRentalID(), rental.getBookID(), rental.getClientId(), rental.getRentedDate(), rental.getDueDate(), rental.getReturnedDate()) for rental in other.getRentals() ]
def read_data_from_repository(self): if self.repository_type == "file_based": clients = self.repo_clients.read_data_from_file() for c in clients: c = c.strip() c = c.split(";") print(c) self.repo_clients.add_new_entity(Client(int(c[0]), c[1])) movies = self.repo_movies.read_data_from_file() for m in movies: m = m.strip() m = m.split(";") self.repo_movies.add_new_entity(Movie(int(m[0]), m[1], m[2], m[3])) rentals = self.repo_rentals.read_data_from_file() for r in rentals: r = r.strip() r = r.split(";") rented_date = parse(r[3]) due_date = parse(r[4]) return_date = parse(r[5]) self.repo_rentals.add_new_entity(Rental(int(r[0]), int(r[1]), int(r[2]), rented_date.date(), due_date.date(), return_date.date())) if self.repository_type == "binary_file_based": self.repo_clients.repo = self.repo_clients.read_from_binary_file() self.repo_movies.repo = self.repo_movies.read_from_binary_file() self.repo_rentals.repo = self.repo_rentals.read_from_binary_file() if self.repository_type == "JSON_based": clients = self.repo_clients.read_data_from_json() for c in clients["clients"]: self.repo_clients.add_new_entity(Client(int(c["id"]), c["name"])) movies = self.repo_movies.read_data_from_json() for m in movies["movies"]: self.repo_movies.add_new_entity(Movie(int(m["id"]), m["title"], m["description"], m["genre"])) print("adasdasd") rentals = self.repo_rentals.read_data_from_json() for r in rentals["rentals"]: rented_date = parse(r["rented_date"]) due_date = parse(r["due_date"]) return_date = parse(r["return_date"]) self.repo_rentals.add_new_entity(Rental(int(r["id"]), int(r["client_id"]), int(r["movie_id"]), rented_date.date(), due_date.date(), return_date.date())) if self.repository_type == "SQL_based": clients = self.repo_clients.read_data_from_mysql() for c in clients: self.repo_clients.add_new_entity(Client(int(c[0]), c[1])) movies = self.repo_movies.read_data_from_mysql() for m in movies: self.repo_movies.add_new_entity(Movie(int(m[0]), m[1], m[2], m[3])) rentals = self.repo_rentals.read_data_from_mysql() for r in rentals: # rented_date = parse(r[3]) # due_date = parse(r[4]) # return_date = parse(r[5]) self.repo_rentals.add_new_entity(Rental(int(r[0]), int(r[1]), int(r[2]), r[3], r[4], r[5]))
def add_rental(self, id, client_id, movie_id, rented_date, due_date, return_date): self.repo_rentals.add_new_entity( Rental(int(id), int(client_id), int(movie_id), rented_date, due_date, return_date))