diff --git a/main.py b/main.py index ed3f1a77..2b5c37b6 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,321 @@ import requests +from video_store import VideoStore -URL = "http://127.0.0.1:5000" -BACKUP_URL = "https://retro-video-store-api.herokuapp.com" +VIDEO_STORE = VideoStore() -def main(): - print("WELCOME TO RETRO VIDEO STORE") - pass +#---------------------# INITIAL OPTIONS #---------------------# + +def handle_options(display=False, choice=False): + options = { + "0": ["Display Options"], + "1": ["List all customers", customer_list], + "2": ["List all videos", video_list], + "3": ["Add new customer to system", add_customer], + "4": ["Add new video to system", add_video], + "5": ["Select a specific customer", customer_selection], + "6": ["Select a specific video", video_selection], + "7": ["Check out a video", check_out_rental], + "8": ["Check in a video", check_in_rental], + "9": ["List overdue rentals", list_overdue_rentals], + "10": ["Exit video store"] + } + if display: + print("Here is a list of possible actions:") + print("") + for num in options: + print(f"{num}. {options[num][0]}") + return options + if choice: + options[choice][1]() + +def customer_list(): + for customer in VIDEO_STORE.list_customers(): + display_customer(customer) + +def video_list(): + for video in VIDEO_STORE.list_videos(): + display_video(video) + +def add_customer(): + print("Please enter the following information about the new customer:") + name = input("Name: ") + postal_code = input("Postal Code: ") + phone = input("Phone: ") + response = VIDEO_STORE.create_customer(name, postal_code, phone) + print_pattern + print(f"New customer created: {response['name']}") + +def add_video(): + print("Please enter the following information about the new video:") + title = input("Title: ") + release_date = input("Release Date: ") + total_inventory = input("Total Inventory: ") + response = VIDEO_STORE.create_video(title, release_date, total_inventory) + print_pattern() + print(f"New video created: {response['title']}") + +def customer_selection(): + if select_customer(): + print_pattern() + print("Here is your selected customer:") + display_customer(VIDEO_STORE.current_customer) + second_choice = make_choice(handle_more_options(display=True)) + if second_choice == "0": + make_choice(handle_more_options(display=True)) + else: + handle_more_options(customer=True, choice=second_choice) + else: + print_pattern() + print("Sorry. Customer not found.") + +def video_selection(): + if select_video(): + print_pattern() + print("Here is your selected video:") + display_video(VIDEO_STORE.current_video) + second_choice = make_choice(handle_more_options(display=True)) + if second_choice == "0": + make_choice(handle_more_options(display=True)) + else: + handle_more_options(video=True, choice=second_choice) + else: + print_pattern() + print("Sorry. Video not found.") + print + +def check_out_rental(): + # Could do some cleanup here + print("Which video would you like to check-out?") + video = select_video() + print("Which customer should this video be checked out to?") + customer = select_customer() + if customer == None or video == None: + print_pattern() + print("I'm sorry. The customer or video could not be found.") + else: + rental = VIDEO_STORE.checkout_video(customer["id"], video["id"]) + print_pattern() + if "details" in rental: + print(rental["details"]) + else: + print("Video rented successfully!") + print("") + display_rental(rental, customer, video) + +def check_in_rental(): + print("Which video would you like to check-in?") + video = select_video() + print("Which customer is returning this video?") + customer = select_customer() + if customer == None or video == None: + print_pattern() + print("I'm sorry. The customer or video could not be found.") + else: + rental = VIDEO_STORE.checkin_video(customer["id"], video["id"]) + display_rental(rental, customer, video) + +#Optional Enhancement +def list_overdue_rentals(): + print("Here is a list of all overdue rentals:") + overdue_rentals = VIDEO_STORE.get_overdue_rentals() + for rental in overdue_rentals: + display_rental(rental, rental, rental) + + +#---------------------# SECONDARY OPTIONS #---------------------# + +def handle_more_options(display=False, customer=False, video=False, choice="5"): + options = { + "0":["Display Options"], + "1": ["Update selection", update_customer, update_video], + "2": ["Delete Selection", delete_customer, delete_video], + "3": ["Display current rentals", current_customer_rentals, current_video_rentals], + "4": ["Display rental history", customer_rental_history, video_rental_history] + } + if display: + print("") + print("Choose the action you wish to perform on your selection:") + print("") + for num in options: + print(f"{num}. {options[num][0]}") + return options + elif customer: + options[choice][1]() + elif video: + options[choice][2]() + +def update_video(): + print("Let's update this video! (Enter 'None' for any info you do not want to update)") + title = input("Enter new video title: ") + if title.lower() == "none": + title = None + release_date = input("Enter new release date: ") + if release_date.lower() == "none": + release_date = None + total_inventory = input("Enter new total inventory: ") + if total_inventory.lower() == "none": + total_inventory = None + print_pattern() + print("Video successfully modified:") + display_video(VIDEO_STORE.update_video(title, release_date, total_inventory)) + +def update_customer(): + print("Let's update this customer! (Enter 'None' for any info you do not want to update)") + name = input("Enter new customer name: ") + if name.lower() == "none": + name = None + postal_code = input("Enter new postal code: ") + if postal_code.lower() == "none": + postal_code = None + phone = input("Enter new phone #: ") + if phone.lower() == "none": + phone = None + print_pattern() + print("Customer successfully modified:") + display_customer(VIDEO_STORE.update_customer(name, postal_code, phone)) +def delete_video(): + ans = input("Are you sure you want to delete this video permenantly? (y/n) ") + if ans == "y": + print_pattern() + print(VIDEO_STORE.delete_video()['details']) + else: + print("Okay. Let's wait on deleting that.") + +def delete_customer(): + ans = input("Are you sure you want to delete this customer permenantly? (y/n) ") + if ans == "y": + print_pattern() + print(VIDEO_STORE.delete_customer()["details"]) + else: + print("Okay. Let's wait on deleting that.") + +#Optional Enhancement +def current_customer_rentals(): + current_rentals = VIDEO_STORE.list_customer_rentals() + print(f"Videos currently rented to {VIDEO_STORE.current_customer['name']}:") + for rental in current_rentals: + print("") + print(rental["title"]) + print(f"Due Date: {rental['due_date']}") + +#Optional Enhancement +def current_video_rentals(): + current_rentals = VIDEO_STORE.list_video_rentals() + print(f"Customers currently renting '{VIDEO_STORE.current_video['title']}':") + for rental in current_rentals: + print("") + print(rental["name"]) + print(f"Due Date: {rental['due_date']}") + +#Optional Enhancement +def customer_rental_history(): + rental_history = VIDEO_STORE.get_customer_rental_history() + print(f"Videos previously rented to {VIDEO_STORE.current_customer['name']}:") + for rental in rental_history: + print("") + print(rental["title"]) + print(f"Due Date: {rental['due_date']}") + print(f"Return Date: {rental['check_in_date']}") + +#Optional Enhancement +def video_rental_history(): + rental_history = VIDEO_STORE.get_video_rental_history() + print(f"Customers who previously rented '{VIDEO_STORE.current_video['title']}':") + for rental in rental_history: + print("") + print(rental["name"]) + print(f"Due Date: {rental['due_date']}") + print(f"Return Date: {rental['check_in_date']}") + +#---------------------# HELPER FUNCTIONS #---------------------# + +def print_pattern(): + print("") + print("-----------------------------------------------") + print("") + +def make_choice(options): + valid_choices = options.keys() + choice = None + while choice not in valid_choices: + print_pattern() + print("What action would you like to take?") + print("(Select '0' to view current options again)") + choice = input("Please make your selection: ") + print_pattern() + return choice + +def display_customer(customer): + print("") + print(customer['name']) + print(f"Postal Code: {customer['postal_code']}") + print(f"Phone #: {customer['phone']}") + print(f"Date Registered: {customer['registered_at']}") + print(f"Videos Out Count: {customer['videos_checked_out_count']}") + print(f"ID: {customer['id']}") + +def display_video(video): + print("") + print(video['title']) + print(f"Release Date: {video['release_date']}") + print(f"Total Inventory: {video['total_inventory']}") + print(f"Available Inventory: {video['available_inventory']}") + print(f"ID: {video['id']}") + +def display_rental(rental, customer, video): + print("") + print(f"Video Title: {video['title']}") + print(f"Customer Name: {customer['name']}") + print(f"Due Date: {rental['due_date']}") + print(f"Return Date: {rental['check_in_date']}") + +def select_video(): + sort = input("Would you like to search by video id or video title?: ").lower() + valid_sort = False + while valid_sort == False: + if sort == "id": + id = input("Please enter video id: ") + return VIDEO_STORE.get_video(id=id) + elif sort == "title": + title = input("Please enter video title: ") + return VIDEO_STORE.get_video(title=title) + else: + sort = input("Please enter vaild sorting method (id or title): ") + +def select_customer(): + sort = input("Would you like to search by customer id, name or phone #?: ") + sort = sort.lower() + valid_sort = False + while valid_sort == False: + if sort == "id": + id = input("Please enter customer id: ") + return VIDEO_STORE.get_customer(id=id,) + elif sort == "name": + name = input("Please enter customer name: ") + return VIDEO_STORE.get_customer(name=name) + elif sort == "phone": + phone = input("Please enter customer phone: ") + return VIDEO_STORE.get_customer(phone=phone) + else: + sort = input("Please enter vaild sorting method (id, name, or phone #): ") + + +#---------------------# RUN CLI #---------------------# +def main(play=True): + print_pattern() + print("WELCOME TO RETRO VIDEO STORE") + print_pattern() + option_list = handle_options(display=True) + while play == True: + choice = make_choice(option_list) + if choice == "0": + handle_options(display=True) + elif choice == "10": + play = False + print("Thanks for using the Video Store Interface!") + else: + handle_options(choice=choice) if __name__ == "__main__": main() \ No newline at end of file diff --git a/video_store.py b/video_store.py new file mode 100644 index 00000000..c931105b --- /dev/null +++ b/video_store.py @@ -0,0 +1,154 @@ +import requests +import datetime + +class VideoStore: + def __init__(self, url="https://nanditas-video-store-api.herokuapp.com", current_customer=None, current_video=None, current_rental=None): + self.url = url + self.current_customer = current_customer + self.current_video = current_video + + + #---------------------# CUSTOMER METHODS #---------------------# + + def create_customer(self, name="Default Name", postal_code="00000", phone="000-000-0000"): + 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, id=None, name=None, phone=None): + self.current_customer = None + for customer in self.list_customers(): + if id and str(customer["id"]) == id: + self.current_customer = customer + elif name and customer["name"] == name: + id = customer["id"] + self.current_customer = customer + elif phone and customer["phone"] == phone: + id = customer["id"] + self.current_customer = customer + if self.current_customer == None: + return None + 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.current_customer["name"] + if not postal_code: + postal_code = self.current_customer["postal_code"] + if not phone: + phone = self.current_customer["phone"] + query_params = { + "name": name, + "postal_code": postal_code, + "phone": phone + } + response = requests.put(self.url+f"/customers/{self.current_customer['id']}", + json=query_params) + self.current_customer = response.json() + return response.json() + + def delete_customer(self): + response = requests.delete(self.url+f"/customers/{self.current_customer['id']}") + return response.json() + + + #---------------------# VIDEO METHODS #---------------------# + + def create_video(self, title="Default Title", release_date=None, total_inventory=1): + query_params = { + "title": title, + "release_date": release_date, + "total_inventory": total_inventory + } + response = requests.post(self.url+"/videos",json=query_params) + self.current_customer = response.json() + return response.json() + + def list_videos(self): + response = requests.get(self.url+"/videos") + return response.json() + + def get_video(self, id=None, title=None): + self.current_video = None + for video in self.list_videos(): + if id and str(video["id"]) == id: + self.current_video = video + elif title and video["title"] == title: + id = video["id"] + self.current_video = video + if self.current_video == None: + return None + 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.current_video["title"] + if not release_date: + release_date = self.current_video["release_date"] + if not total_inventory: + total_inventory = self.current_video["total_inventory"] + query_params = { + "title": title, + "release_date": release_date, + "total_inventory": total_inventory + } + response = requests.put(self.url+f"/videos/{self.current_video['id']}", + json=query_params) + self.current_video = response.json() + return response.json() + + def delete_video(self): + response = requests.delete(self.url+f"/videos/{self.current_video['id']}") + return response.json() + + +#---------------------# RENTAL METHODS #---------------------# + + def checkout_video(self, customer_id, video_id): + query_params = { + "customer_id": customer_id, + "video_id": video_id + } + response = requests.post(self.url+f"/rentals/check-out", json=query_params) + return response.json() + + def checkin_video(self, customer_id, video_id): + query_params = { + "customer_id": customer_id, + "video_id": video_id + } + response = requests.post(self.url+f"/rentals/check-in", json=query_params) + return response.json() + + +#---------------------# OPTIONAL METHODS #---------------------# + + def list_customer_rentals(self): + response = requests.get(self.url+f"/customers/{self.current_customer['id']}/rentals") + return response.json() + + def list_video_rentals(self): + response = requests.get(self.url+f"/videos/{self.current_video['id']}/rentals") + return response.json() + + def get_customer_rental_history(self): + response = requests.get(self.url+f"/customers/{self.current_video['id']}/history") + return response.json() + + def get_video_rental_history(self): + response = requests.get(self.url+f"/videos/{self.current_video['id']}/history") + return response.json() + + def get_overdue_rentals(self): + response = requests.get(self.url+f"/rentals/overdue") + return response.json() \ No newline at end of file