-
Notifications
You must be signed in to change notification settings - Fork 70
YuliaP_Rock_Final Version. #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import datetime | ||
| import requests | ||
|
|
||
|
|
||
| class Customers: | ||
| def __init__(self, url="https://retro-video-store-api.herokuapp.com", selected_customer= None): | ||
| self.url = url | ||
| self.selected_customer = selected_customer | ||
|
|
||
|
|
||
| # def customer_not_found(): | ||
| # return "No customer with that id or name has been found." | ||
|
|
||
| def add_customer(self, name=None, postal_code=None, phone=None): | ||
|
|
||
| request_body = { | ||
| "name": name, | ||
| "postal_code": postal_code, | ||
| "phone": phone | ||
| } | ||
|
|
||
| url = self.url+"/videos" | ||
| response = requests.post(url, json=query_params) | ||
| 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, | ||
| "postal_code": postal_code, | ||
| "phone": phone | ||
| } | ||
|
|
||
| response = requests.put(self.url+f"/customers/{customer_id}", | ||
| json=query_params) | ||
| print("response:", response) | ||
| self.selected_customer = response.json() | ||
| return response.json() | ||
|
|
||
|
|
||
| def delete_customer(self, customer_id=None): | ||
| response = requests.delete(self.url+f"/customers/{customer_id}") | ||
| if None: | ||
| return "Customer not found" | ||
| print(response) | ||
| return response.json() | ||
|
|
||
|
|
||
|
|
||
| def get_one_customer(self, customer_id=None): | ||
| response = requests.get(self.url+f"/customers/{customer_id}") | ||
| if None: | ||
| return "Customer not found" | ||
| return response.json() | ||
|
|
||
|
|
||
| def all_customers(self): | ||
| response = requests.get(self.url+"/customers") | ||
| return response.json() | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,171 @@ | ||
| import requests | ||
| import datetime | ||
| from customers import Customers | ||
| from videos import Videos | ||
| from rentals import Rentals | ||
|
|
||
| URL = "http://127.0.0.1:5000" | ||
| BACKUP_URL = "https://retro-video-store-api.herokuapp.com" | ||
|
|
||
| def main(): | ||
| print("WELCOME TO RETRO VIDEO STORE") | ||
| pass | ||
|
|
||
| # URL = "http://127.0.0.1:5000" | ||
| URL = "https://retro-video-store-api.herokuapp.com" | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
||
| def print_stars(): | ||
| print("\n**************************\n") | ||
|
|
||
|
|
||
|
|
||
| def list_options(): | ||
| options = { | ||
| "1": "Add a video", | ||
| "2": "Edit a video", | ||
| "3": "Delete a video", | ||
| "4": "List all videos", | ||
| "5": "Get a video", | ||
| "6": "Add a customer", | ||
| "7": "Edit a customer", | ||
| "8": "delete a customer", | ||
| "9": "Get a customer", | ||
| "10": "Get all customers", | ||
| "11": "Check out a video", | ||
| "12": "Check in a video", | ||
| "00" : "Quit", | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A future refactor could involve putting customer, video, and rental options in different menus. |
||
|
|
||
| print_stars() | ||
| print("Welcome to the Retro Video Store!") | ||
| print("These are your options:\n") | ||
|
|
||
|
|
||
| for choice_num in options: | ||
| print(f"{choice_num}, {options[choice_num]}") | ||
| return options | ||
|
|
||
| def make_choice(options, videos): | ||
| valid_choices = options.keys() | ||
| choice = input("\nSelect option * ") | ||
|
|
||
| while choice not in valid_choices: | ||
| print("What would you like to do next? Select from the options above.") | ||
| choice = input("Make your selection using the option number: ") | ||
|
|
||
| return choice | ||
|
|
||
|
|
||
| def run_cli(play=True): | ||
|
|
||
| videos = Videos(url=URL) | ||
| customers = Customers(url=URL) | ||
| rentals = Rentals(url=URL) | ||
|
|
||
| options = list_options() | ||
|
|
||
| while play==True: | ||
|
|
||
| choice = make_choice(options, videos) | ||
|
|
||
| if choice=='1': | ||
| title=input("Please enter a title: ") | ||
| release_date=input("Please enter a release date: ") | ||
| total_inventory=input("How many copies do you have on hand? ") | ||
| response = videos.add_video(title=title, release_date=release_date, | ||
| total_inventory=total_inventory) | ||
|
|
||
| print(f"New video '{title}' created") | ||
|
|
||
| # update a video | ||
| elif choice=='2': | ||
| video_id = input("Please enter a ID: ") | ||
| title=input("New title: ") | ||
| release_date=input("New release date: ") | ||
| total_inventory=input("How many copies? ") | ||
|
|
||
| response = videos.update_video(video_id=video_id, title=title, | ||
| release_date=release_date, total_inventory=total_inventory) | ||
|
|
||
|
|
||
| print(f"Updated video id {video_id}") | ||
|
|
||
| # delete a video | ||
| elif choice=='3': | ||
| video_id = input("Please enter an ID: ") | ||
| videos.delete_video(video_id) | ||
| print(f"Video {video_id} has been deleted") | ||
|
|
||
| # list all videos | ||
| elif choice=='4': | ||
| for video in videos.list_videos(): | ||
| print(video) | ||
|
|
||
| # get one video | ||
| elif choice=='5': | ||
| video_id = input("Please enter an ID: ") | ||
| selected_video = videos.get_video(video_id=video_id) | ||
| print(selected_video) | ||
|
|
||
| # add a customer | ||
| elif choice=='6': | ||
| name=input("Please enter a name: ") | ||
| postal_code=input("Please enter a postal code: ") | ||
| phone=input("Please enter a phone number: ") | ||
| response = customers.add_customer(name=name, postal_code=postal_code, | ||
| phone=phone) | ||
| print(f"Customer {name} created") | ||
|
|
||
| # update a customer | ||
| elif choice=='7': | ||
| customer_id = input("Please enter an ID: ") | ||
| name=input("Please enter a new name: ") | ||
| postal_code=input("Please enter a new postal code: ") | ||
| phone=input("Please enter a new phone: ") | ||
|
|
||
| response = customers.update_customer(customer_id=customer_id, | ||
| name=name, postal_code=postal_code, phone=phone) | ||
| print(f"Updated customer id {customer_id}") | ||
|
|
||
| # delete a customer | ||
| elif choice=='8': | ||
| customer_id = input("Please enter an ID: ") | ||
| response = customers.delete_customer(customer_id) | ||
| print(f"Customer number {customer_id} has been deleted") | ||
|
|
||
| # get info about one customer | ||
|
|
||
| elif choice=='9': | ||
| customer_id = input("Please enter an ID: ") | ||
| selected_customer = customers.get_one_customer(customer_id=customer_id) | ||
| if selected_customer: | ||
| print(selected_customer) | ||
|
|
||
| # get info about all customers | ||
| elif choice=='10': | ||
| for customers in customers.all_customers(): | ||
| print(customers) | ||
|
|
||
| # check out video | ||
| elif choice=='11': | ||
| customer_id=input("Please enter a customer id: ") | ||
| video_id=input("Please enter a video id: ") | ||
| response = rentals.check_out(customer_id=customer_id, | ||
| video_id=video_id) | ||
| print("Video successfully checked out") | ||
| print(f"Video id {video_id} checked out to {customer_id}") | ||
|
|
||
| # check in video | ||
| elif choice=='12': | ||
| customer_id=input("Please enter a customer id: ") | ||
| video_id=input("Please enter a video id: ") | ||
| response = rentals.check_in(customer_id=customer_id, | ||
| video_id=video_id) | ||
| print("Video successfully checked in") | ||
| print(f"Video {video_id} checked in from {customer_id}") | ||
|
|
||
|
|
||
| # end | ||
| elif choice=='00': | ||
| play=False | ||
| print("\n Thank you for visiting our store. Come again soon.") | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job separating the customer, rental, and video requests from the menu logic! |
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A future enhancement would involve handling 404s and empty responses from requests. Currently, your CLI crashes whenever invalid ids or invalid inputs are being processed. Preventing the app from crashing using try/excepts or even while loops would definitely improve the overall user experience of the CLI. |
||
| run_cli() | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||
| import requests | ||||||||||||||
| from datetime import datetime | ||||||||||||||
|
|
||||||||||||||
| class Rentals: | ||||||||||||||
| def __init__(self, url="https://retro-video-store-api.herokuapp.com"): | ||||||||||||||
| self.url = url | ||||||||||||||
|
|
||||||||||||||
| def check_out(self, customer_id=None, video_id=None): | ||||||||||||||
| response_body = { | ||||||||||||||
| "customer_id": customer_id, | ||||||||||||||
| "video_id": video_id | ||||||||||||||
| } | ||||||||||||||
| response = requests.post(self.url+"/rentals/check-out", | ||||||||||||||
| json=query_params) | ||||||||||||||
| return response.json() | ||||||||||||||
|
Comment on lines
+13
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| def check_in(self, customer_id=None, video_id=None): | ||||||||||||||
| response_body = { | ||||||||||||||
| "customer_id": customer_id, | ||||||||||||||
| "video_id": video_id | ||||||||||||||
| } | ||||||||||||||
| response = requests.post(self.url+"/rentals/check-in", | ||||||||||||||
| json=query_params) | ||||||||||||||
| return response.json() | ||||||||||||||
|
Comment on lines
+22
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you meant to use
Suggested change
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import requests | ||
| import datetime | ||
|
|
||
| class Videos: | ||
| def __init__(self, url="https://retro-video-store-api.herokuapp.com", selected_video=None, selected_customer=None): | ||
| self.url = url | ||
| self.selected_video = selected_video | ||
|
|
||
|
|
||
| def add_video(self, title=None,release_date=None,total_inventory=None): | ||
| query_params = { | ||
| "title": title, | ||
| "release_date": release_date, | ||
| "total_inventory": total_inventory | ||
| } | ||
| url = self.url+"/videos" | ||
| response = requests.post(url, json=query_params) | ||
| return response.json() | ||
|
|
||
|
|
||
| def update_video(self, video_id=None, title=None,release_date=None, | ||
| total_inventory=0): | ||
| 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.total_inventory["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) | ||
| print("response:", response) | ||
| return response.json() | ||
|
|
||
|
|
||
|
|
||
| def delete_video(self, video_id=None): | ||
| response = requests.delete(self.url+f"/videos/{video_id}") | ||
| print(response) | ||
| return response.json() | ||
|
|
||
|
|
||
|
|
||
| def list_videos(self): | ||
| response = requests.get(self.url+"/videos") | ||
| return response.json() | ||
|
|
||
| def get_video(self, video_id=None): | ||
| response = requests.get(self.url+f"/videos/{video_id}") | ||
| return response.json() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good job handling a missing/invalid customer!