diff --git a/.gitignore b/.gitignore index b4d70396..9e9f4a26 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,117 @@ # Ada Library Information *.ali +.vscode +.DS_Store +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class +# C extensions +*.so +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec +# Installer logs +pip-log.txt +pip-delete-this-directory.txt +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ +# Translations +*.mo +*.pot +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal +# Flask stuff: +instance/ +.webassets-cache +# Scrapy stuff: +.scrapy +# Sphinx documentation +docs/_build/ +# PyBuilder +.pybuilder/ +target/ +# Jupyter Notebook +.ipynb_checkpoints +# IPython +profile_default/ +ipython_config.py +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ +# Celery stuff +celerybeat-schedule +celerybeat.pid +# SageMath parsed files +*.sage.py +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +# Spyder project settings +.spyderproject +.spyproject +# Rope project settings +.ropeproject +# mkdocs documentation +/site +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json +# Pyre type checker +.pyre/ +# pytype static type analyzer +.pytype/ +# Cython debug symbols +cython_debug/ \ No newline at end of file diff --git a/customer.py b/customer.py new file mode 100644 index 00000000..24e3b6d0 --- /dev/null +++ b/customer.py @@ -0,0 +1,77 @@ +import requests +import datetime + +class Customer: + def __init__(self, url="http://localhost:5000", selected_customer=None): + self.url = url + self.selected_customer = selected_customer + + def create_customer(self,name="Default Name",postal_code="postal code",phone="Default Phone"): + query_params = { + "name": name, + "postal_code": postal_code, + "phone": phone + } + response = requests.post(self.url+"/customers",json=query_params) + return response.json() + + def list_customers(self): + response = requests.get(self.url+"/customers") + return response.json() + + def get_customer(self, name=None, id=None): + + for customer in self.list_customers(): + if customer: + if customer["name"]==name: + id = customer["id"] + self.selected_customer = customer + elif 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/{id}") + return response.json() + + def update_customer(self,name=None,postal_code=None,phone=None): + if not name: + name = self.selected_customer["name"] + if not postal_code: + postal_code = self.selected_customer["postal_code"] + if not phone: + phone=self.phone["phone"] + + query_params = { + "name": name, + "postal_code": postal_code, + "phone":phone + #"completed_at": self.selected_task["is_complete"] + } + response = requests.put( + self.url+f"/customers/{self.selected_customer['id']}", + json=query_params + ) + print("response:", response) + self.selected_customer = response.json() + return response.json() + + def delete_customer(self): + response = requests.delete(self.url+f"/customers/{self.selected_customer['id']}") + self.selected_customer = None + return response.json() + + # def mark_complete(self): + # response = requests.patch(self.url+f"/tasks/{self.selected_task['id']}/mark_complete") + # self.selected_task = response.json()["task"] + # return response.json() + + # def mark_incomplete(self): + # response = requests.patch(self.url+f"/tasks/{self.selected_task['id']}/mark_incomplete") + # self.selected_task = response.json()["task"] + # return response.json() + + def print_selected(self): + if self.selected_customer: + print(f"Customer with id {self.selected_customer['id']} is currently selected\n") \ No newline at end of file diff --git a/main.py b/main.py index ed3f1a77..8e0efadc 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,7 @@ import requests +from video import Video +from customer import Customer +from rental import Rental URL = "http://127.0.0.1:5000" BACKUP_URL = "https://retro-video-store-api.herokuapp.com" @@ -9,4 +12,322 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() + + +def print_stars(): + print("\n**************************\n") + +def list_options(): + + options = { + "1": "add a video", #done + "2": "edit a video", #done + "3": "delete a video", #done + "4": "get information about all videos", #done + "5": "get information about one video", #select a video #done + "6": "add a customer", #done + "7": "edit a customer", #done + "8": "Delete a customer", #done + "9": "get information about one customer", #select a customer # done + "10": "get information about all customers", # done + "11": "check out a video to a customer", + "12": "check in a video from a customer", + "13": "List all options",#done + "14": "Quit" #done + } + + print_stars() + print("Welcome to the Video Store CLI") + print("These are the actions you can perform") + print_stars() + + for choice_num in options: + print(f"Option {choice_num}. {options[choice_num]}") + + print_stars() + + return options + + +def make_choice(options, video,customer,rental): + valid_choices = options.keys() + choice = None + + while choice not in valid_choices: + print("What would you like to do? Select 13 to see all options again") + choice = input("Make your selection using the option number 1-14: ") + + if choice in ['2','3'] and video.selected_video == None: + print("You must get information about one video before updating it or deleting it.") + print("Let's get information about one video") + choice = "5" + + if choice in ['7','8'] and customer.selected_customer == None: + print("You must get information about a customer before updating it or deleting it.") + print("Let's get information about one customer") + choice = "9" + + return choice + +def run_cli(play=True): + video=Video(url="https://retro-video-store-api.herokuapp.com/") + customer=Customer(url="https://retro-video-store-api.herokuapp.com/") + rental=Rental(url="https://retro-video-store-api.herokuapp.com/") + # print choices + options = list_options() + + while play==True: + + # get input and validate + choice = make_choice(options, video,customer,rental) + + #options.print_selected() + video.print_selected() + customer.print_selected() + #rental.print_selected() + + if choice=='4': #get information about all videos + print_stars() + for video in video.list_videos(): + print(f"Id: {video['id']},Title: {video['title']}") + #print(video) + elif choice=='1': #add a video + print("Great! Let's add a video.") + title=input("What is the title of your video? ") + release_date=input("Enter release date ") + total_inventory=input("Enter total inventory for this video ") + response = video.create_video(title=title, release_date=release_date,total_inventory=total_inventory) + video_info = video.get_video(title=title, id = response['id']) + print_stars() + print("New video:", video_info) + + elif choice=='5': #select a video + select_by = input("What would you like to select by? Enter 'title' or 'id': ") + if select_by=="title": + + title = input("Which video title would you like to select? ") + + video.get_video(title=title) + + elif select_by=="id" or select_by == "ID" or select_by == "Id": + id = input("Which video id would you like to select? ") + if id.isnumeric(): + id = int(id) + video.get_video(id=id) + else: + print("Could not select. Please enter the words 'id' or 'title'.") + # if selected_video: + if video.selected_video: + print_stars() + print("Selected video: ", video.selected_video) + else: + print("\nInvalid id or title entry,\nplease check video list to make sure video is in inventory.\nSelect option 4 for inventory info\nOr select option 1 to add this video to inventory") + + + elif choice=='2': #update a video + print(f"Great! Let's update the video information: {video.selected_video}") + title=input("What is the new title of your video? ") + release_date=input("Enter Release Date: ") + total_inventory=input("Enter total inventory: ") + response = video.update_video(title=title, release_date=release_date, total_inventory=total_inventory) + + print_stars() + print("Updated response:", response) + + elif choice=='3':#delete a video + video.delete_video() + + print_stars() + print("Video has been deleted.") + + print_stars() + print(video.list_videos()) + + elif choice=='10': + print_stars() + for customer in customer.list_customers(): + print(f"Id: {customer['id']}, Name: {customer['name']}") + #print(customer) + + elif choice=='6': + print("Great! Let's add a customer. ") + name=input("What is the name of the customer? ") + postal_code=input("Enter postal_code: ") + phone=input("Enter phone: ") + response = customer.create_customer(name=name, postal_code=postal_code, phone=phone) + customer_info = customer.get_customer(name = name, id = response['id']) + + print_stars() + print("New customer:", customer_info) + + elif choice=='9': + select_by = input("Would you like to select by? Enter 'name' or 'id': ") + if select_by=="name": + name = input("Which customer name would you like to select? ") + customer.get_customer(name=name) + elif select_by=="id" or select_by == "ID" or select_by == "Id": + id = input("Which customer id would you like to select? ") + if id.isnumeric(): + id = int(id) + customer.get_customer(id=id) + else: + print("Could not select. Please enter id or name.") + + if customer.selected_customer: + print_stars() + print("Selected customer: ", customer.selected_customer) + + else: + print("\nInvalid id or name entry,\nplease check customer list to make sure customer exists.\nSelect option 10 for customer list info\nOr select option 6 to add this customer") + + elif choice=='7': + print(f"Great! Let's update the customer information: {customer.selected_customer}") + name=input("What is the new name of your customer? ") + postal_code=input("Enter Postal Code: ") + phone=input("phone: ") + response = customer.update_customer(name=name, postal_code=postal_code, phone=phone) + + print_stars() + print("Updated resonse:", response) + + elif choice=='8': + customer.delete_customer() + + print_stars() + print("Customer has been deleted.") + + print_stars() + print(customer.list_customers()) + + elif choice == '11': #check out video to customer + print_stars() + print("Let's checkout a video! ") + + print("Here are all of the available videos: ") + all_videos = video.list_videos() + + for choices in all_videos: + print(f"Id: {choices['id']},Title: {choices['title']}") + + select_by = input("What would you like to select by? Enter either 'title' or 'id': ") + + if select_by == "title": + title = input("which video title would you like to select? ") + video.get_video(title=title) + + elif select_by == 'id': + video_id = input("Which video ID would you like to select? ") + if video_id.isnumeric(): + video_id = int(video_id) + #video.selected_video = video.get_video(id=video_id) + video.get_video(id=video_id) + + else: + print("please enter a valid video ID: ") + + if video.selected_video: + print_stars() + print("The chosen video information is: ", video.selected_video) + print(video.selected_video) + + print("Here are all of the customers: ") + + all_customers = customer.list_customers() + + for person in all_customers: + print(f"Id: {person['id']}, name: {person['name']}") + + select_by = input("What would you like to select by? Enter either 'name' or 'id': ") + if select_by == "name": + customer_name = input("Which customer name would you like to select? ") + selected_customer=customer.get_customer(name=customer_name) + print(video.selected_video) + customer_id=selected_customer["id"] + video_id=video.selected_video["id"] + # video_id = selected_customer["video_id"] + elif select_by == "id": + customer_id = input("Which customer ID would you like to select? ") + if customer_id.isnumeric(): + customer_id = int(customer_id) + #customer.selected_customer = customer.get_customer(id=customer_id) + customer.get_customer(id=customer_id) + + else: + print("Please enter valid customer ID: ") + + if customer.get_customer(id=customer_id): + print_stars() + #print("Selected customer: ", customer.get_customer(id=customer_id)) + response = rental.check_out(customer_id,video_id) + print("Video successfully checked out! Rental info: ", response) + + elif choice == '12': #check in a video from customer + print_stars() + print("Let's return a video! ") + + print("Here are all of the available videos: ") + all_videos = video.list_videos() + + for choices in all_videos: + print(f'Id: {choices["id"]}, Title:{choices["title"]}') + + select_by = input("What would you like to select by? Enter either 'title' or 'id'. ") + if select_by == "title": + title = input("which video title would you like to select? ") + video.get_video(title=title) + + elif select_by == "id": + video_id = input("Which video ID would you like to select? ") + if video_id.isnumeric(): + video_id = int(video_id) + #video.selected_video = video.get_video(id=video_id) + video.get_video(id=video_id) + else: + print("Please enter a valid video ID: ") + + if video.selected_video: + print_stars() + print("Selected Video Information is: ", video.selected_video) + #print(video.selected_video) + + print("Here are all of the customers: ") + all_customers = customer.list_customers() + + for person in all_customers: + print(f'id:{person["id"]},name: {person["name"]}') + #print(f'ID: {customer['id']}, name: {customer['name']}') + + select_by = input("What would you like to select by? Enter either 'name' or 'id': ") + if select_by == "name": + customer_name = input("Which customer name would you like to select? ") + selected_customer = customer.get_customer(name=customer_name) + #print(selected_customer) + customer_id=selected_customer['id'] + video_id=video.selected_video['id'] + elif select_by == "id": + customer_id = input("Which customer ID would you like to select? ") + if customer_id.isnumeric(): + customer_id = int(customer_id) + #customer.selected_customer = customer.get_customer(id=customer_id) + customer.get_customer(id=customer_id) + else: + print("Please enter valid Customer ID: ") + + if customer.get_customer(id=customer_id): + print_stars() + #print("Selected customer: ", customer.selected_customer) + response = rental.check_in(customer_id, video_id) + print("Video sucessfully checked in! Rental Info: ", response) + + + + elif choice=='13': + list_options() + elif choice=='14': + play=False + print("\nThanks for using the Retro Video CLI!") + + print_stars() + +run_cli() \ No newline at end of file diff --git a/rental.py b/rental.py new file mode 100644 index 00000000..7e89a1e7 --- /dev/null +++ b/rental.py @@ -0,0 +1,108 @@ +import requests +import datetime + +class Rental: + def __init__(self, url="http://localhost:5000", selected_rental=None): + self.url = url + self.selected_rental = selected_rental + #start + 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() + # def check_in(self): + # response = requests.get(self.url+"/rentals/check-in") + # return response.json() + + # def check_out(self): + # response = requests.get(self.url+"/rentals/check-out") + # return response.json() + + # def list_rentals(self): + # response=requests.get(self.url+"/rentals") + # return response.json() + + # def get_rental(self,customer_id=None,video_id=None): + # for rental in self.list_rentals(): + # if rental: + # if rental["customer_id"]==customer_id and rental["video_id"]==video_id: + # id=rental["id"] + # self.selected_rental=rental + # elif id==rental["id"]: + # self.selected_rental=rental + + # if self.selected_rental == None: + # return "Could not find rental by that rental id, customer id and video id" + + # response = requests.get(self.url+f"/rentals/check-in") #route ??? + # return response.json() + + # def get_customer(self, name=None, id=None): + + # for customer in self.list_customers(): + # if customer: + # if customer["name"]==name: + # id = customer["id"] + # self.selected_customer = customer + # elif 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/{id}") + # return response.json() + + # def update_customer(self,name=None,postal_code=None,phone=None): + # if not name: + # name = self.selected_customer["name"] + # if not postal_code: + # postal_code = self.selected_customer["postal_code"] + # if not phone: + # phone=self.phone["phone"] + + # query_params = { + # "name": name, + # "postal_code": postal_code, + # "phone":phone + # #"completed_at": self.selected_task["is_complete"] + # } + # response = requests.put( + # self.url+f"/customers/{self.selected_customer['id']}", + # json=query_params + # ) + # print("response:", response) + # self.selected_customer = response.json()["customer"] + # return response.json() + + # def delete_customer(self): + # response = requests.delete(self.url+f"/customers/{self.selected_customer['id']}") + # self.selected_customer = None + # return response.json() + + # # def mark_complete(self): + # # response = requests.patch(self.url+f"/tasks/{self.selected_task['id']}/mark_complete") + # # self.selected_task = response.json()["task"] + # # return response.json() + + # # def mark_incomplete(self): + # # response = requests.patch(self.url+f"/tasks/{self.selected_task['id']}/mark_incomplete") + # # self.selected_task = response.json()["task"] + # # return response.json() + + # def print_selected(self): + # if self.selected_video: + # print(f"Customer with id {self.selected_customer['id']} is currently selected\n") \ No newline at end of file diff --git a/video.py b/video.py new file mode 100644 index 00000000..b620f3f3 --- /dev/null +++ b/video.py @@ -0,0 +1,79 @@ +import requests +import datetime + +class Video: + def __init__(self, url="http://localhost:5000", selected_video=None): + self.url = url + self.selected_video = selected_video + + def create_video(self,title="Default Video",release_date="Default date",total_inventory=None): + 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 list_videos(self): + response = requests.get(self.url+"/videos") + return response.json() + + def get_video(self, title=None, id=None): + + for video in self.list_videos(): + if video: + if video["title"]==title: + id = video["id"] + self.selected_video = video + elif id == video["id"]: + self.selected_video = video + + if self.selected_video == None: + return "Could not find video by that name or id" + + response = requests.get(self.url+f"/videos/{id}") + return response.json() + + def update_video(self,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["relase_date"] + if not total_inventory: + total_inventory=self.total_inventory["total_inventory"] + + query_params = { + "title": title, + "release_date": release_date, + "total_inventory":total_inventory + #"completed_at": self.selected_task["is_complete"] + } + response = requests.put( + self.url+f"/videos/{self.selected_video['id']}", + json=query_params + ) + print("response:", response) + # self.selected_video = response.json()["video"] + self.selected_video = response.json() + + return response.json() + + def delete_video(self): + response = requests.delete(self.url+f"/videos/{self.selected_video['id']}") + self.selected_video = None + return response.json() + + # def mark_complete(self): + # response = requests.patch(self.url+f"/tasks/{self.selected_task['id']}/mark_complete") + # self.selected_task = response.json()["task"] + # return response.json() + + # def mark_incomplete(self): + # response = requests.patch(self.url+f"/tasks/{self.selected_task['id']}/mark_incomplete") + # self.selected_task = response.json()["task"] + # return response.json() + + def print_selected(self): + if self.selected_video: + print(f"Video with id {self.selected_video['id']} is currently selected\n") \ No newline at end of file