diff --git a/__pycache__/my_retro_store_operations.cpython-39.pyc b/__pycache__/my_retro_store_operations.cpython-39.pyc new file mode 100644 index 00000000..255e5ca8 Binary files /dev/null and b/__pycache__/my_retro_store_operations.cpython-39.pyc differ diff --git a/main.py b/main.py index ed3f1a77..6e76c3fa 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,273 @@ import requests +from my_retro_store_operations import CustomerOperations, RentalOperations +from my_retro_store_operations import VideoOperations +import time,sys +from datetime import date, datetime -URL = "http://127.0.0.1:5000" -BACKUP_URL = "https://retro-video-store-api.herokuapp.com" +def print_stars(): + print("\n**********\n") -def main(): +#progress bar loading animation +def progress_bar(count, total, status=''): + bar_len = 10 + filled_len = int(round(bar_len * count / float(total))) + + percents = round(100.0 * count / float(total), 1) + bar = '=' * filled_len + '-' * (bar_len - filled_len) + + sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status)) + sys.stdout.flush() + +def call_bar(): + total = 10 + i = 0 + while i < total: + i += 1 + progress_bar(i, total, status='Please wait. Loading options for you') + time.sleep(0.3) + progress_bar(i, total, status='Loading is complete. Thanks for waiting') + +def list_options(): + options = { + #Video + "1": "Create a video", + "2": "Update a video", + "3": "Delete a video", + "4": "List all videos", + "5": "Get one video", + #Customer + "6": "Create a customer", + "7": "Update a customer", + "8": "Delete a customer", + "9": "Select one customer", + "10": "List all customers", + #Rental + "11": "Check out a video to a customer", + "12": "Check in a video from a customer", + #Operations + "*": "List all options", + "#": "Quit" + } + print_stars() print("WELCOME TO RETRO VIDEO STORE") - pass + print("These are the actions you can perform") + + for choice_num in options: + print(f"Option {choice_num}. {options[choice_num]}") + print_stars() + return options + +def make_choice(options, customer_operations, video_operations, rental_operations): + valid_choices = options.keys() + choice = None + + while choice not in valid_choices: + choice = input("Please make your selection using the one of the listed option number: ") + + return choice + +def valid_release_date(date_string): + format = "%m-%d-%Y" + + try: + datetime.strptime(date_string, format) + return True + except ValueError: + print("Invalid entry. Please enter date as MM-DD-YYYY") + return False + +url="https://aida-retro-video-store-api.herokuapp.com" + +def main(play=True): + + print_stars() + print("... \N{smiling face with smiling eyes} ... WELCOME TO AIDA'S RETRO VIDEO STORE ... \N{hugging face}") + call_bar() + + customer_operations = CustomerOperations(url) + video_operations = VideoOperations(url) + rental_operations = RentalOperations(url) + options = list_options() + + while play==True: + + choice = make_choice(options, customer_operations, video_operations, rental_operations) + + if choice=='1': + print("Hashing it out! Let's create a new video!") + title=input("What is the name of the video: ") + release_date=input("Please enter a release date: ") + if valid_release_date(release_date): + release_date = release_date + else: + release_date=input("Please enter a date as (MM-DD-YYYY): ") + if valid_release_date(release_date): + release_date = release_date + else: + print("Oops, your entry is still invalid. Date will be stored as a default current date. You can update it later with option 2") + release_date = str(datetime.now()) + total_inventory=input("How many copies are there in total? ") + call_bar() + response = video_operations.create_video(title=title, release_date=release_date, total_inventory=total_inventory) + print_stars() + print(f"Here is the ID of new video record: {response['id']} ") + + elif choice=='2': + list_videos = video_operations.get_all_videos() + for video in list_videos: + print(f"Video Id: {video['id']}, Video Title: {video['title']}") + video_id = input("Which video would you like to update? Please enter ID: ") + if video_id.isnumeric(): + video_id = int(video_id) + print(f"Great, let's update the video with ID: {video_id}") + title=input("What is the new title of the movie? ") + release_date=input("Please enter a new release date: ") + if valid_release_date(release_date): + release_date = release_date + else: + release_date=input("Please enter a valid new date: ") + if valid_release_date(release_date): + release_date = release_date + else: + print("Oops, your entry is still invalid. Date will be stored as a default current date. You can update it later with option 2") + release_date = str(datetime.now()) + total_inventory=input("How many copies are there total? ") + response = video_operations.update_video(video_id, title=title, release_date=release_date,total_inventory=total_inventory) + print_stars() + print(f"Successfully updated the video with ID: {response['id']} - title: {response['title']} - release date: {response['release_date']} - inventory: {response['total_inventory']}") + else: + print("Id type is integer. Please enter valid id.") + + elif choice=='3': + list_videos = video_operations.get_all_videos() + for video in list_videos: + print(f"Id:{video['id']}, Title:{video['title']}") + video_id = input("Which video would you like to delete? Please enter ID: ") + if video_id.isnumeric(): + video_id = int(video_id) + video_operations.delete_video(video_id) + print_stars() + print(f"Success! Video with ID {video_id} has been deleted") + else: + print("Id type is integer. Please enter valid id.") + + elif choice=='4': + print_stars() + for video in video_operations.get_all_videos(): + print(video) + + elif choice=='5': + print("Here are the videos:") + list_videos = video_operations.get_all_videos() + for video in list_videos: + print(f"Id: {video['id']}, name: {video['title']}") + video_id = input("Which video id would you like to select? ") + if video_id.isnumeric(): + video_id = int(video_id) + video_operations.selected_video = video_operations.get_one_video(video_id=video_id) + if video_operations.selected_video: + print(f"Selected video: {video_operations.selected_video}") + else: + print("Id type is integer. Please enter valid id.") + + elif choice=='6': + print("Splendid! New customer more money! ") + name=input("What is the name of the customer ") + postal_code=input("What is the postal code? ") + phone=input("Please write down a phone number to contact ") + call_bar() + response = customer_operations.create_customer(name=name, postal_code=postal_code,phone=phone) + print_stars() + print(f"Here is the ID of new customer: {response['id']}") + + elif choice=='7': + list_customer = customer_operations.get_all_customers() + for customer in list_customer: + print(f"Customer Id: {customer['id']}, name: {customer['name']}") + customer_id = input("Which customer would you like to update? Please enter ID: ") + if customer_id.isnumeric(): + customer_id = int(customer_id) + print(f"Great! Let's update the customer with ID: {customer_id}") + name=input("What is the new name of your customer? ") + postal_code=input("What is the new postal code of your customer? ") + phone=input("Please write down a new phone number : ") + response = customer_operations.update_customer(customer_id, name=name, postal_code=postal_code,phone=phone) + print_stars() + print(f"Successfully updated the customer with ID: {response['id']} - name: {response['name']} - postal code: {response['postal_code']} - phone: {response['phone']}") + else: + print("Id type is integer. Please enter valid id.") + + elif choice=='8': + list_customer = customer_operations.get_all_customers() + for customer in list_customer: + print(f"Customer Id: {customer['id']}, name: {customer['name']}") + customer_id = input("Which customer you would like to delete? Please enter ID: ") + if customer_id.isnumeric(): + customer_id = int(customer_id) + customer_operations.delete_customer(customer_id) + print_stars() + print(f"Customer with ID {customer_id} has been deleted") + else: + print("Id type is integer. Please enter valid id.") + + elif choice=='9': + print("Here are the customers:") + list_customers = customer_operations.get_all_customers() + for customer in list_customers: + print(f"Customer Id: {customer['id']}, name: {customer['name']}") + + customer_id = input("Which customer id would you like to select? ") + if customer_id.isnumeric(): + customer_id = int(customer_id) + customer_operations.selected_customer = customer_operations.get_one_customer(customer_id=customer_id) + if customer_operations.selected_customer: + print(f"Selected customer: {customer_operations.selected_customer}") + else: + print("Id type is integer. Please enter valid id.") + + elif choice=='10': + print_stars() + for customer in customer_operations.get_all_customers(): + print(customer) + + elif choice=='11': + customer_id=input("Starting a rental procedure. Please enter a customer id: ") + video_id=input("Please enter a video id: ") + if customer_id.isnumeric(): + customer_id = int(customer_id) + else: + print("Id type is integer. Please enter valid id.") + if video_id.isnumeric(): + video_id = int(video_id) + else: + print("Id type is integer. Please enter valid id.") + call_bar() + response = rental_operations.check_out(customer_id=customer_id, video_id=video_id) + print(f"*** Calling the shots! Selected video id {video_id} has been checked out! ") + + elif choice=='12': + customer_id=input("Starting a return procedure. Please enter a customer id: ") + video_id=input("Please enter a video id: ") + if customer_id.isnumeric(): + customer_id = int(customer_id) + else: + print("Id type is integer. Please enter valid id.") + if video_id.isnumeric(): + video_id = int(video_id) + else: + print("Id type is integer. Please enter valid id.") + call_bar() + response = rental_operations.check_in(customer_id=customer_id, video_id=video_id) + print("Here is the updated return report:", response) + print(f"*** Thank you customer id {customer_id}! Selected video id {video_id} has been checked in! ***") + + elif choice =='*': + list_options() + elif choice=='#': + play=False + print("... \N{smiling face with smiling eyes} ... Thanks for using the Retro Video store created by Aida ... \N{winking face}") + print_stars() if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/my_retro_store_operations.py b/my_retro_store_operations.py new file mode 100644 index 00000000..76744011 --- /dev/null +++ b/my_retro_store_operations.py @@ -0,0 +1,139 @@ +import requests + +class CustomerOperations: + def __init__(self, url="https://aida-retro-video-store-api.herokuapp.com", selected_customer=None): + self.url = url + self.selected_customer = selected_customer + + def create_customer(self,name="Default customer name",postal_code="Default postal code",phone="Default phone number"): + query_params = { + "name": name, + "postal_code": postal_code, + "phone": phone, + #"register_at": register_at + } + response = requests.post(self.url+"/customers",json=query_params) + return response.json() + + def get_all_customers(self): + response = requests.get(self.url+"/customers") + return response.json() + + def get_one_customer(self, name=None, customer_id=None): + for customer in self.get_all_customers(): + if name: + if name["name"]==name: + customer_id = customer["id"] + self.selected_customer = customer + elif customer_id == customer["id"]: + self.selected_customer = customer + + if self.selected_customer == None: + return "Could not find customer by that name or id" + + response = requests.get(self.url+f"/customers/{customer_id}") + return response.json() + + def update_customer(self,customer_id, name=None,phone=None, postal_code=None): + if not name: + name = self.selected_customer["name"] + if not phone: + phone = self.selected_customer["phone"] + if not postal_code: + postal_code = self. selected_customer["postal_code"] + + query_params = { + "name": name, + "phone": phone, + "postal_code": postal_code, + } + response = requests.put( + self.url+f"/customers/{customer_id}",json=query_params) + return response.json() + + def delete_customer(self, customer_id): + response = requests.delete(self.url+f"/customers/{customer_id}") + return response.json() + + def print_selected(self): + if self.selected_customer: + print(f"Customer with id {self.selected_customer['id']} is currently selected\n") + +class VideoOperations: + def __init__(self, url="https://aida-retro-video-store-api.herokuapp.com", selected_video=None): + self.url = url + self.selected_video = selected_video + + def create_video(self,title="Default video title",release_date="Default release date",total_inventory=0): + query_params = { + "title": title, + "release_date": release_date, + "total_inventory": total_inventory + } + response = requests.post(self.url+"/videos",json=query_params) + return response.json() + + def get_all_videos(self): + response = requests.get(self.url+"/videos") + return response.json() + + def get_one_video(self, title=None, video_id=None): + for video in self.get_all_videos(): + if title: + if title["title"]==title: + video_id = video["id"] + self.selected_video = video + elif video_id == video["id"]: + self.selected_video = video + + if self.selected_video == None: + return "Could not find video by that title or id" + + response = requests.get(self.url+f"/videos/{video_id}") + return response.json() + + def update_video(self,video_id, title=None,release_date=None, total_inventory=None): + if not title: + title = self.selected_video["title"] + if not release_date: + release_date = self.selected_video["release_date"] + if not total_inventory: + total_inventory = self. selected_video["total_inventory"] + + query_params = { + "title": title, + "release_date": release_date, + "total_inventory": total_inventory, + } + response = requests.put( + self.url+f"/videos/{video_id}",json=query_params) + return response.json() + + def delete_video(self, video_id): + response = requests.delete(self.url+f"/videos/{video_id}") + return response.json() + + def print_selected(self): + if self.selected_video: + print(f"Video with id {self.selected_video['id']} is currently selected\n") + +class RentalOperations: + def __init__(self, url="https://aida-retro-video-store-api.herokuapp.com", selected_rental=None): + self.url = url + self.selected_rental = selected_rental + + def check_out(self, customer_id=None, video_id=None): + query_params = { + "customer_id": customer_id, + "video_id": video_id + } + response = requests.post(self.url+"/rentals/check-out",json=query_params) + return response.json() + + def check_in(self, customer_id=None, video_id=None): + query_params = { + "customer_id": customer_id, + "video_id": video_id + } + response = requests.post(self.url+"/rentals/check-in",json=query_params) + return response.json() \ No newline at end of file