-
Notifications
You must be signed in to change notification settings - Fork 72
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
Ivana - Scissors #66
Open
i-vs
wants to merge
1
commit into
AdaGold:main
Choose a base branch
from
i-vs:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+292
−4
Open
Ivana - Scissors #66
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,253 @@ | ||
import requests | ||
import json | ||
from resources.frontdesk import show_options, print_inventory, print_accounts | ||
|
||
|
||
def get_path(path): | ||
endpoints = { | ||
1:'videos/', | ||
2: 'customers/', | ||
3: 'rentals/check-out/', | ||
4: 'rentals/check-in/' | ||
} | ||
|
||
return f'https://loucadoura.herokuapp.com/{endpoints[path]}' | ||
|
||
|
||
#=========================HELPERS========================= | ||
|
||
|
||
def valid_video(title, release, inventory): | ||
|
||
if not(title and release and inventory): | ||
return False | ||
|
||
return {"title": title, "release_date": release, "total_inventory": inventory} | ||
|
||
|
||
def valid_customer(name, postal_code, phone): | ||
|
||
if not(name and postal_code and phone): | ||
return False | ||
|
||
return {"name": name, "postal_code": postal_code, "phone": phone} | ||
|
||
|
||
#=========================EMPLOYEE/STOREROOM========================= | ||
|
||
all_videos = {} | ||
# would be lazy-loading inside its class but no time | ||
|
||
|
||
def get_all_videos(): | ||
|
||
if len(all_videos) == 0: | ||
path = get_path(1) | ||
videos_list = requests.get(path).json() | ||
|
||
for vhs in videos_list: | ||
all_videos[vhs["id"]] = vhs | ||
|
||
return all_videos | ||
|
||
def get_video(video_id): | ||
|
||
path = get_path(1) | ||
video = requests.get(path+f"{video_id}/").json() | ||
|
||
return video | ||
|
||
|
||
def post_video(new_title, new_release, inventory): | ||
body = valid_video(new_title, new_release, inventory) | ||
|
||
if not body: | ||
print("\n Invalid Data. Please try again\n") | ||
return get_options() | ||
|
||
path = get_path(1) | ||
response = requests.post(path, json=body) | ||
return response | ||
|
||
def edit_video(video_id, new_title, release_date, inventory): | ||
|
||
body = valid_video(new_title, release_date, inventory) | ||
if not body: | ||
print("\n Invalid Data. Please try again\n") | ||
return get_options() | ||
|
||
path = get_path(1) | ||
response = requests.put(path+f"{video_id}/", json=body) #.json() | ||
return response | ||
|
||
|
||
def delete_video(video_id): | ||
|
||
to_delete = get_video(video_id) | ||
path = get_path(1) | ||
response = requests.delete(path+f"{video_id}/").json() | ||
|
||
return response | ||
|
||
|
||
#=======================CASHIER============================= | ||
|
||
|
||
all_customers = {} | ||
|
||
|
||
def get_all_customers(): | ||
if len(all_customers) == 0: | ||
path = get_path(2) | ||
customer_list = requests.get(path).json() | ||
for account in customer_list: | ||
all_customers[account["id"]] = account | ||
|
||
return all_customers | ||
|
||
|
||
def get_customer_info(customer_id): | ||
path = get_path(2) | ||
customer = requests.get(path+f"{customer_id}/").json() | ||
return customer | ||
|
||
|
||
def add_customer(new_customer, postal_code, phone): | ||
body = valid_customer(new_customer, postal_code, phone) | ||
|
||
if not body: | ||
print("\n Invalid Data. Please try again\n") | ||
return get_options() | ||
|
||
path = get_path(2) | ||
response = requests.post(path,json=body) | ||
|
||
return response | ||
|
||
|
||
def edit_customer(customer_id, new_profile, new_postal_code, new_phone): | ||
to_update = get_customer_info(customer_id) | ||
body = valid_customer(new_profile, new_postal_code, new_phone) | ||
|
||
if not body: | ||
print("\n Invalid Data. Please try again\n") | ||
return get_options() | ||
|
||
path = get_path(2) | ||
response = requests.put(path+f"{customer_id}/", json=body) | ||
|
||
return response | ||
|
||
|
||
def delete_customer(customer_id): | ||
to_delete = get_customer_info(customer_id) | ||
|
||
path = get_path(2) | ||
response = requests.delete(path+f"{customer_id}/").json() | ||
|
||
return response | ||
|
||
|
||
def check_out_video(customer_id, video_id): | ||
path = get_path(3) | ||
body = {"customer_id": customer_id, "video_id": video_id} | ||
response = requests.post(path, json=body) | ||
return response | ||
|
||
|
||
def check_in_video(customer_id, video_id): | ||
path = get_path(4) | ||
body = {"customer_id": customer_id, "video_id": video_id} | ||
response = requests.post(path, json=body) | ||
return response | ||
|
||
|
||
# ======================MAIN=============================== | ||
|
||
|
||
def get_options(): | ||
inventory = get_all_videos() | ||
accounts = get_all_customers() | ||
|
||
show_options() | ||
choice = int(input()) | ||
|
||
if choice == 0: | ||
print("Quitting.") | ||
|
||
elif choice == 1: | ||
new_title = input("Title to add: ") | ||
new_release = input("Release date: ") | ||
inventory = input("Total Inventory: ") | ||
|
||
print(post_video(new_title, new_release, inventory)) | ||
|
||
elif choice == 2: | ||
video_id = input("To edid a video, insert its ID: ") | ||
new_title = input("New Title: ") | ||
release_date = input("Release date: ") | ||
inventory = input("Total Inventory: ") | ||
|
||
to_edit = get_video(video_id) | ||
|
||
print(f"Video to edit: {to_edit}") # TODO: update available_inventory | ||
print(edit_video(video_id, new_title, release_date, inventory)) | ||
|
||
elif choice == 3: | ||
video_id = input("Insert the video ID to be deleted: ") | ||
print(delete_video(video_id)) | ||
|
||
elif choice == 4: | ||
print_inventory(inventory) | ||
|
||
elif choice == 5: | ||
video_id = input("Insert the video ID to get its information: ") | ||
print(get_video(video_id)) | ||
|
||
elif choice == 6: | ||
new_customer = input("Name: ") | ||
postal_code = input("Postal Code: ") | ||
phone = input("Phone: ") | ||
|
||
print(add_customer(new_customer, postal_code, phone)) | ||
|
||
elif choice == 7: | ||
customer_id = input("Customer ID to edit pls: ") | ||
new_profile = input("New Profile Name: ") | ||
new_postal_code = input("New Postal Code: ") | ||
new_phone = input("New Phone: ") | ||
|
||
print(edit_customer(customer_id, new_profile, new_postal_code, new_phone)) | ||
|
||
elif choice == 8: | ||
customer_id = input("Insert the customer's ID to be removed: ") | ||
print(delete_customer(customer_id)) | ||
|
||
elif choice == 9: | ||
customer_id = input("Customer ID to check pls: ") | ||
print(get_customer_info(customer_id)) | ||
|
||
elif choice == 10: | ||
print_accounts(accounts) | ||
|
||
elif choice == 11: | ||
video_id = input("Insert video ID to check-out: ") | ||
customer_id = input("Insert customer ID to check-out: ") | ||
|
||
print(check_out_video(customer_id, video_id)) | ||
|
||
elif choice == 12: | ||
video_id = input("Insert video ID to check-in: ") | ||
customer_id = input("Insert customer ID to check-in: ") | ||
|
||
print(check_in_video(customer_id, video_id)) | ||
|
||
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 | ||
print("Choose an option:") | ||
get_options() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
main() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
OPTIONS = { | ||
0: "Quit", | ||
1: "Add a new video", | ||
2: "Edit a video", | ||
3: "Delete a video", | ||
4: "View inventory", | ||
5: "Get video information", | ||
6: "Add a new customer", | ||
7: "Edit a customer", | ||
8: "Remove a customer", | ||
9: "Get customer information", | ||
10: "View all registered customers", | ||
11: "Check-out a video to a customer", | ||
12: "Check-in a video from a customer", | ||
} | ||
|
||
def show_options(): | ||
for choice, option in OPTIONS.items(): | ||
print(f"Option #{choice}: {option}") | ||
|
||
|
||
|
||
def print_inventory(inventory): | ||
|
||
for entry, value in inventory.items(): | ||
|
||
print("-"*40) | ||
print() | ||
print(f"\tID: {entry}") | ||
print(f"Title: {value['title']}") | ||
print(f"Release: {value['release_date']}") | ||
print(f"Available to rent: {value['available_inventory']}") | ||
print(f"Total Inventory: {value['total_inventory']}\n") | ||
|
||
|
||
def print_accounts(accounts): | ||
|
||
for entry, value in accounts.items(): | ||
|
||
print("-"*30) | ||
print() | ||
print(f"Customer ID: {entry}") | ||
print(f"Name: {value['name']}") | ||
print(f"Phone: {value['phone']}") | ||
print(f"Postal Code: {value['postal_code']}") | ||
print(f"Videos checked-out: {value['videos_checked_out_count']}\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This results in a crazy error message when there's a problem with the user input (such as an invalid id).
See my general feedback for more context on how this could be improved :)
For example:
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.
Thanks Jared this is super helpful! I wasn't sure about how I should handle bad requests on this end. I wanted to alter the API code so errors like these would be handled by the original code instead of my local front end 😅 and this would definitely come in handy! thanks a lot :)
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.
Yeah! We can talk more about this in our 1:1 this week if it'd be useful!