From 6545cd517fa02b989e2ef321468c92fcf98c9d15 Mon Sep 17 00:00:00 2001 From: Nandita Gilroy Date: Wed, 26 May 2021 13:48:12 -0700 Subject: [PATCH 1/4] creates methods for listing, creating, updating and deleting customer information --- video_store.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 video_store.py diff --git a/video_store.py b/video_store.py new file mode 100644 index 00000000..63038e76 --- /dev/null +++ b/video_store.py @@ -0,0 +1,82 @@ +import requests +import datetime + +class VideoStore: + def __init__(self, url="http://localhost:5000", current_customer=None, current_video=None): + self.url = url + self.current_customer = current_customer + self.current_video = current_video + + 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): + for customer in self.list_customers(): + if id and 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 "I'm sorry, we could not find a customer by that id, name, or phone" + 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) + # print("response:", response) + self.current_customer = response.json()["customer"] + return response.json() + + def delete_customer(self): + response = requests.delete(self.url+f"/customers/{self.current_customer['id']}") + self.current_customer = None + return response.json() + + + # def create_video(self,): + + # def get_video(self, name=None, id=None): + + # def update_video(self): + + # def delete_video(self): + + # def list_videos(self): + + + #def checkout_video(self): + + #def checkin_video(self): + + + # Optional: + + # def list_customers_checked_out_videos(self): + + # def list_videos_current_customers(self): \ No newline at end of file From 50bf0309807ec0d147ac03bfb757a59c678418e3 Mon Sep 17 00:00:00 2001 From: Nandita Gilroy Date: Wed, 26 May 2021 23:24:03 -0700 Subject: [PATCH 2/4] adds videos methods to video_store.py and creates options for user in main.py --- main.py | 162 ++++++++++++++++++++++++++++++++++++++++++++++++- video_store.py | 59 +++++++++++++++--- 2 files changed, 210 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index ed3f1a77..97b25b0d 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,168 @@ import requests +from video_store import VideoStore + +# URL = "http://127.0.0.1:5000" +# BACKUP_URL = "https://retro-video-store-api.herokuapp.com" + +def print_pattern(): + print("////////////////////////////////////////////////") + +def list_options(): + # Maybe just have every possible option in the first options list + options = { + "1": "List all videos", + "2": "List all customers", + "3": "Add new video to system", + "4": "Add new customer to system", + "5": "Select a specific video", + "6": "Select a specific customer", + "7": "Check out a video", + "8": "Check in a video" + } + print("Here is a list of possible actions:") + print("") + for num in options: + print(f"{num}. {options[num]}") + print_pattern() + return options + +def list_more_options(): + options = { + "1": "Update selection", + "2": "Delete Selection", + "3": "List current rentals" + } + print("What would you like to do with your selection?") + print("") + for num in options: + print(f"{num}. {options[num]}") + print_pattern() + return options + +def make_choice(options): + valid_choices = options.keys() + choice = None + while choice not in valid_choices: + choice = input("Please select which action you would like to take: ") + return choice + +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_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']}") + -URL = "http://127.0.0.1:5000" -BACKUP_URL = "https://retro-video-store-api.herokuapp.com" def main(): + video_store = VideoStore() + print("WELCOME TO RETRO VIDEO STORE") - pass + options = list_options() + choice = make_choice(options) + if choice == "1": + print_pattern() + for video in video_store.list_videos(): + display_video(video) + + elif choice == "2": + print_pattern() + for customer in video_store.list_customers(): + display_customer(customer) + + elif choice == "3": + print("Please enter the following information about the new video:") + title = input("Title: ") + release_date = input("Release Date: ") + total_inventory = input("Total Inventory: ") + video_store.create_video(title, release_date, total_inventory) + print("New video created!") + + elif choice == "4": + print("Please enter the following information about the new customer:") + name = input("Name: ") + postal_code = input("Postal Code: ") + phone = input("Phone: ") + video_store.create_customer(name, postal_code, phone) + print("New customer created!") + + elif choice =="5": + sort = input("Would you like to search by video id or video title?: ") + sort = sort.lower() + valid_sort = False + while valid_sort == False: + if sort == "id": + id = input("Please enter video id: ") + video_store.get_video(id=id) + valid_sort = True + elif sort == "title": + title = input("Please enter video title: ") + video_store.get_video(title=title) + valid_sort = True + else: + sort = input("Please enter vaild sorting method (id or title): ") + if video_store.current_video: + print_pattern() + print("Here is your selected video:") + display_video(video_store.current_video) + second_choice = make_choice(list_more_options()) + if second_choice == "1": + #What if they don't want to update everything? Maybe give choiced? + print("Cool! Let's update this video:") + title = input("Enter new video title: ") + release_date = input("Enter new release date: ") + total_inventory = input("Enter new total inventory: ") + print("Video successfully created:") + display_video(video_store.update_video(title, release_date, total_inventory)) + elif second_choice == "2": + #Deletion doesn't really work. Must fix! + ans = input("Are you sure you want to delete this video permenantly? (y/n)") + if ans == "y": + video_store.delete_video() + else: + print("Okay. Let's wait on deleting that.") + else: + pass + #get rental from vid + else: + print("Sorry, video not found.") + + elif choice == "6": + 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: ") + video_store.get_customer(id=id,) + valid_sort = True + elif sort == "name": + name = input("Please enter customer name: ") + video_store.get_customer(name=name) + valid_sort = True + elif sort == "phone": + phone = input("Please enter customer phone: ") + video_store.get_customer(phone=phone) + valid_sort = True + else: + sort = input("Please enter vaild sorting method (id, name, or phone #): ") + if video_store.current_customer: + display_customer(video_store.current_customer) + else: + print("Sorry, customer not found.") + if __name__ == "__main__": main() \ No newline at end of file diff --git a/video_store.py b/video_store.py index 63038e76..ce5de582 100644 --- a/video_store.py +++ b/video_store.py @@ -7,6 +7,9 @@ def __init__(self, url="http://localhost:5000", current_customer=None, current_v 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, @@ -49,8 +52,8 @@ def update_customer(self, name=None, postal_code=None, phone=None): } response = requests.put(self.url+f"/customers/{self.current_customer['id']}", json=query_params) - # print("response:", response) - self.current_customer = response.json()["customer"] + print("response:", response) + self.current_customer = response.json() return response.json() def delete_customer(self): @@ -59,15 +62,55 @@ def delete_customer(self): return response.json() - # def create_video(self,): - - # def get_video(self, name=None, id=None): + #---------------------# VIDEO METHODS #---------------------# - # def update_video(self): + 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) + return response.json() - # def delete_video(self): + def list_videos(self): + response = requests.get(self.url+"/videos") + return response.json() - # def list_videos(self): + def get_video(self, id=None, title=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 "I'm sorry, we could not find a video by that id, or title" + 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) + #print("response:", response) + self.current_video = response.json() + return response.json() + + def delete_video(self): + response = requests.delete(self.url+f"/videos/{self.current_video['id']}") + self.current_video = None + return response.json() #def checkout_video(self): From 1c874e15c6ae4a560212815fc6b52ad46032deee Mon Sep 17 00:00:00 2001 From: Nandita Gilroy Date: Fri, 28 May 2021 19:36:14 -0700 Subject: [PATCH 3/4] completely reorganizing cli --- main.py | 417 +++++++++++++++++++++++++++++++++---------------- video_store.py | 55 +++++-- 2 files changed, 328 insertions(+), 144 deletions(-) diff --git a/main.py b/main.py index 97b25b0d..f9560795 100644 --- a/main.py +++ b/main.py @@ -1,51 +1,262 @@ 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 print_pattern(): - print("////////////////////////////////////////////////") +#---------------------# INITIAL OPTIONS #---------------------# -def list_options(): - # Maybe just have every possible option in the first options list +def handle_options(display=False, choice=False): options = { - "1": "List all videos", - "2": "List all customers", - "3": "Add new video to system", - "4": "Add new customer to system", - "5": "Select a specific video", - "6": "Select a specific customer", - "7": "Check out a video", - "8": "Check in a video" + "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"] } - print("Here is a list of possible actions:") - print("") - for num in options: - print(f"{num}. {options[num]}") + 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(): + print_pattern() + for customer in VIDEO_STORE.list_customers(): + display_customer(customer) + +def video_list(): print_pattern() - return options + 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) -def list_more_options(): +#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 = { - "1": "Update selection", - "2": "Delete Selection", - "3": "List current rentals" + "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] } - print("What would you like to do with your selection?") - print("") - for num in options: - print(f"{num}. {options[num]}") + 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() - return options + 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: - choice = input("Please select which action you would like to take: ") + 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']) @@ -54,115 +265,59 @@ def display_video(video): print(f"Available Inventory: {video['available_inventory']}") print(f"ID: {video['id']}") -def display_customer(customer): +def display_rental(rental, customer, video): 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']}") + 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 #): ") -def main(): - video_store = VideoStore() +#---------------------# RUN CLI #---------------------# +def main(play=True): + print_pattern() print("WELCOME TO RETRO VIDEO STORE") - options = list_options() - choice = make_choice(options) - - if choice == "1": - print_pattern() - for video in video_store.list_videos(): - display_video(video) - - elif choice == "2": - print_pattern() - for customer in video_store.list_customers(): - display_customer(customer) - - elif choice == "3": - print("Please enter the following information about the new video:") - title = input("Title: ") - release_date = input("Release Date: ") - total_inventory = input("Total Inventory: ") - video_store.create_video(title, release_date, total_inventory) - print("New video created!") - - elif choice == "4": - print("Please enter the following information about the new customer:") - name = input("Name: ") - postal_code = input("Postal Code: ") - phone = input("Phone: ") - video_store.create_customer(name, postal_code, phone) - print("New customer created!") - - elif choice =="5": - sort = input("Would you like to search by video id or video title?: ") - sort = sort.lower() - valid_sort = False - while valid_sort == False: - if sort == "id": - id = input("Please enter video id: ") - video_store.get_video(id=id) - valid_sort = True - elif sort == "title": - title = input("Please enter video title: ") - video_store.get_video(title=title) - valid_sort = True - else: - sort = input("Please enter vaild sorting method (id or title): ") - if video_store.current_video: - print_pattern() - print("Here is your selected video:") - display_video(video_store.current_video) - second_choice = make_choice(list_more_options()) - if second_choice == "1": - #What if they don't want to update everything? Maybe give choiced? - print("Cool! Let's update this video:") - title = input("Enter new video title: ") - release_date = input("Enter new release date: ") - total_inventory = input("Enter new total inventory: ") - print("Video successfully created:") - display_video(video_store.update_video(title, release_date, total_inventory)) - elif second_choice == "2": - #Deletion doesn't really work. Must fix! - ans = input("Are you sure you want to delete this video permenantly? (y/n)") - if ans == "y": - video_store.delete_video() - else: - print("Okay. Let's wait on deleting that.") - else: - pass - #get rental from vid - else: - print("Sorry, video not found.") - - elif choice == "6": - 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: ") - video_store.get_customer(id=id,) - valid_sort = True - elif sort == "name": - name = input("Please enter customer name: ") - video_store.get_customer(name=name) - valid_sort = True - elif sort == "phone": - phone = input("Please enter customer phone: ") - video_store.get_customer(phone=phone) - valid_sort = True - else: - sort = input("Please enter vaild sorting method (id, name, or phone #): ") - if video_store.current_customer: - display_customer(video_store.current_customer) + 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: - print("Sorry, customer not found.") - + handle_options(choice=choice) if __name__ == "__main__": main() \ No newline at end of file diff --git a/video_store.py b/video_store.py index ce5de582..7df17f3a 100644 --- a/video_store.py +++ b/video_store.py @@ -2,7 +2,7 @@ import datetime class VideoStore: - def __init__(self, url="http://localhost:5000", current_customer=None, current_video=None): + def __init__(self, url="http://localhost:5000", current_customer=None, current_video=None, current_rental=None): self.url = url self.current_customer = current_customer self.current_video = current_video @@ -24,8 +24,9 @@ def list_customers(self): 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 customer["id"] == id: + if id and str(customer["id"]) == id: self.current_customer = customer elif name and customer["name"] == name: id = customer["id"] @@ -34,7 +35,7 @@ def get_customer(self, id=None, name=None, phone=None): id = customer["id"] self.current_customer = customer if self.current_customer == None: - return "I'm sorry, we could not find a customer by that id, name, or phone" + return None response = requests.get(self.url+f"/customers/{id}") return response.json() @@ -52,13 +53,11 @@ def update_customer(self, name=None, postal_code=None, phone=None): } response = requests.put(self.url+f"/customers/{self.current_customer['id']}", json=query_params) - print("response:", response) self.current_customer = response.json() return response.json() def delete_customer(self): response = requests.delete(self.url+f"/customers/{self.current_customer['id']}") - self.current_customer = None return response.json() @@ -71,6 +70,7 @@ def create_video(self, title="Default Title", release_date=None, total_inventory "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): @@ -78,6 +78,7 @@ def list_videos(self): 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 @@ -85,7 +86,7 @@ def get_video(self, id=None, title=None): id = video["id"] self.current_video = video if self.current_video == None: - return "I'm sorry, we could not find a video by that id, or title" + return None response = requests.get(self.url+f"/videos/{id}") return response.json() @@ -103,23 +104,51 @@ def update_video(self, title=None, release_date=None, total_inventory=None): } response = requests.put(self.url+f"/videos/{self.current_video['id']}", json=query_params) - #print("response:", response) self.current_video = response.json() return response.json() def delete_video(self): response = requests.delete(self.url+f"/videos/{self.current_video['id']}") - self.current_video = None return response.json() - #def checkout_video(self): +#---------------------# RENTAL METHODS #---------------------# - #def checkin_video(self): + 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: +#---------------------# OPTIONAL METHODS #---------------------# - # def list_customers_checked_out_videos(self): + 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 list_videos_current_customers(self): \ No newline at end of file + def get_overdue_rentals(self): + response = requests.get(self.url+f"/rentals/overdue") + return response.json() \ No newline at end of file From eb1d8605312428adfc7559b192e45b4be49b1462 Mon Sep 17 00:00:00 2001 From: Nandita Gilroy Date: Fri, 28 May 2021 20:08:37 -0700 Subject: [PATCH 4/4] updates heroku link --- main.py | 2 -- video_store.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/main.py b/main.py index f9560795..2b5c37b6 100644 --- a/main.py +++ b/main.py @@ -29,12 +29,10 @@ def handle_options(display=False, choice=False): options[choice][1]() def customer_list(): - print_pattern() for customer in VIDEO_STORE.list_customers(): display_customer(customer) def video_list(): - print_pattern() for video in VIDEO_STORE.list_videos(): display_video(video) diff --git a/video_store.py b/video_store.py index 7df17f3a..c931105b 100644 --- a/video_store.py +++ b/video_store.py @@ -2,7 +2,7 @@ import datetime class VideoStore: - def __init__(self, url="http://localhost:5000", current_customer=None, current_video=None, current_rental=None): + 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