Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 245 additions & 4 deletions main.py
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()

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:

Traceback (most recent call last):
  File "/Users/jared/Developer/c15/video-store-cli/main.py", line 253, in <module>
    main()
  File "/Users/jared/Developer/c15/video-store-cli/main.py", line 249, in main
    get_options()
  File "/Users/jared/Developer/c15/video-store-cli/main.py", line 198, in get_options
    print(delete_video(video_id))
  File "/Users/jared/Developer/c15/video-store-cli/main.py", line 86, in delete_video
    to_delete = get_video(video_id)
  File "/Users/jared/Developer/c15/video-store-cli/main.py", line 56, in get_video
    video = requests.get(path+f"{video_id}/").json()
  File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/Cellar/[email protected]/3.9.0_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/[email protected]/3.9.0_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/[email protected]/3.9.0_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Copy link
Author

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 :)

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!


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 added resources/__init__.py
Empty file.
46 changes: 46 additions & 0 deletions resources/frontdesk.py
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")
1 change: 1 addition & 0 deletions resources/storeroom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#