Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file added __pycache__/retro_video.cpython-39.pyc
Binary file not shown.
199 changes: 197 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,207 @@
import requests
from os import read, register_at_fork
from datetime import datetime
from retro_video import RetroVideo

URL = "http://127.0.0.1:5000"
BACKUP_URL = "https://retro-video-store-api.herokuapp.com"

def main():
print("WELCOME TO RETRO VIDEO STORE")
# run_cli()
pass

def print_stars():
print("\n**************************\n")

def list_options():

options = {
"1": "Add a Video",
"2": "Edit a Video",
"3": "Delete a Video",
"4": "Get information about all videos",
"5": "Get information about one video",
"6": "Add a customer",
"7": "Edit a customer",
"8": "Delete a customer",
"9": "Get information about one customer",
"10": "Get information about all customers",
"11": "Check out a video to a customer",
"12": "Check in a video to a customer",
"13": "Quit"
}

print_stars()
print("Welcome to the Retro Video Store CLI")
print("These are the actions you can perform")
print_stars()


for choice_num in options:
print(f"Option {choice_num}. {options[choice_num]}")

print_stars()

return options

def make_choice(options, retro_video):
valid_choices = options.keys()
choice = None

while choice not in valid_choices:
print("Please choose an option from the list above")
choice = input("Please enter you selection here: ")
if choice in ['2','3'] and retro_video.selected_video==None:
print("To edit or delete you must select a video")
choice = "5"

elif choice in ['7','8'] and retro_video.selected_customer==None:
print("To edit or delete you must select a customer")
choice = "9"

elif choice in valid_choices:
return choice



def run_cli(play=True):
retro_video = RetroVideo(url="https://retro-video-store-api.herokuapp.com")

options = list_options()

while play==True:

choice = make_choice(options, retro_video)


retro_video.print_selected()

# Create a video
if choice=='1':
print("Great! Let's add a new video.")
title=input("What is the title of your video? ")
release_date=input("What is the release date of your video? ")
total_inventory=input("What is the total inventory of your video? ")
available_inventory = input("what is the available inventory of your video?")
response = retro_video.create_video(title=title, total_inventory=total_inventory, release_date = release_date, available_inventory = available_inventory)

print_stars()
print("New video:", response)
# Edit a video
elif choice=='2':
print(f"Okay! We will be editing {retro_video.selected_video}")
title=input("What is the new title of your video? ")
release_date=input("What is the new release date of your video? ")
total_inventory=input("What is the new total inventory of your video? ")
available_inventory = input("What is the new available inventory of your video? ")
response = retro_video.update_video(title=title, release_date = release_date, total_inventory=total_inventory, available_inventory = available_inventory)

print_stars()
print("Updated Video:", response)
# Delete selected video
elif choice=='3':
retro_video.delete_video()

print_stars()
print("Video has been deleted.")

print_stars()
print(retro_video.list_videos())
# Get information about all videos
elif choice=='4':
print_stars()
for video in retro_video.list_videos():
print(video)
# Get information about one video
elif choice=='5':
title = input("Please enter the video title you would like to select: ")
retro_video.get_video(title=title)
if title == None:
print("Could not select. Please enter a video title.")

if retro_video.selected_video:
print_stars()
print("Selected video: ", retro_video.selected_video)
# ================================== Customer ========================================================================

# Create a customer
if choice=='6':
print("Great! Let's add a new customer.")
name=input("What is the name of your customer? ")
phone=input("What is the phone number of your customer? ")
postal_code=input("What is the postal_code of your customer? ")
registered_at = input("what is the time your customer was registered at?")
videos_checked_out = input("How many videos are check out for your customer?")
response = retro_video.create_customer(name=name, phone=phone, postal_code = postal_code, registered_at=registered_at, videos_checked_out = videos_checked_out)

print_stars()
print("New customer:", response)
# Edit a customer
elif choice=='7':
print(f"Okay! We will be editing {retro_video.selected_customer}")
name=input("What is the new name of your customer? ")
phone=input("What is the new phone number of your customer? ")
postal_code=input("What is the new postal_code of your customer? ")
registered_at= input("When was your customer registered at? ")
videos_checked_out = input("How many videos are check out for your customer?")
response = retro_video.update_customer(name=name, phone=phone, postal_code = postal_code, registered_at=registered_at, videos_checked_out= videos_checked_out)

print_stars()
print("Updated Customer:", response)
# Delete selected customer
elif choice=='8':
retro_video.delete_customer()

print_stars()
print("Customer has been deleted.")

print_stars()
print(retro_video.list_customers())
# Get information about all customers
elif choice=='9':
name = input("Please enter the customer name you would like to select: ")
retro_video.get_customer(name=name)
if name == None:
print("Could not select. Please enter a customer name.")

if retro_video.selected_customer:
print_stars()
print("Selected customer: ", retro_video.selected_customer)
# Get information about one customer
elif choice=='10':
print_stars()
for customer in retro_video.list_customers():
print(customer)
# ============================================ Rentals =====================================
# Check out a video to a customer
elif choice == "11":
print("Please enter the customer id and video id for the movie you would like to check out.")
customer_id = input("Customer id: ")
video_id = input("Video id: ")
print(customer_id, video_id)
response = retro_video.video_check_out(customer_id, video_id)
print(response)
print("Check out was successful")

# Check in a video to a customer
elif choice == "12":
print("Please enter the customer id and video id for the movie you would like to check in.")
customer_id = input("Customer id: ")
video_id = input("Video id: ")
response = retro_video.video_check_in(customer_id=customer_id, video_id=video_id)
if response == 200:
print("Check in was successful")


# ============================================ Quit =====================================
# Quit
elif choice == "13":
play=False
print("\nThanks for using the Retro Video Store CLI!")

print_stars()

run_cli()

if __name__ == "__main__":
main()
main()
174 changes: 174 additions & 0 deletions retro_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import requests
from datetime import datetime

from requests.models import Response

class RetroVideo:
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
self.selected_customer = selected_customer

# Videos

def create_video(self,title="Default Video",release_date="Default Release Date",total_inventory=None, available_inventory = None):
query_params = {
"title": title,
"release_date": release_date,
"total_inventory": total_inventory,
"available_inventory": available_inventory
}
response = requests.post(self.url+"/videos",json=query_params)
return response.json()

def list_videos(self):
response = requests.get(self.url+"/videos")
return response.json()

def get_video(self, title=None, id=None):
for video in self.list_videos():
if title:
if video["title"]==title:
id = video["id"]
print(video)
self.selected_video = video
elif id == video["id"]:
self.selected_video = video

if self.selected_video == None:
return "Could not find video by that name"

response = requests.get(self.url+f"/videos/{id}")
return response.json()

def update_video(self,title=None,release_date=None, total_inventory= None, available_inventory = None):
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.selected_video["total_inventory"]
if not available_inventory:
available_inventory= self.selected_video["available_inventory"]

query_params = {
"title": title,
"total_inventory": total_inventory,
"release_date": release_date,
"available inventory": available_inventory

}
response = requests.put(
self.url+f"/videos/{self.selected_video['id']}",
json=query_params
)
print("response:", response)
self.selected_video = response.json()
return response.json()

def delete_video(self):
response = requests.delete(self.url+f"/videos/{self.selected_video['id']}")
self.selected_video = None
return response.json()


def print_selected(self):
if self.selected_video:
print(f"Video with id {self.selected_video['id']} is currently selected\n")

# Customers

def create_customer(self,name="Default Name",phone="Default Phone",postal_code=None, registered_at = None, videos_checked_out=None ):
query_params = {
"name": name,
"phone": phone,
"postal_code": postal_code,
"registered_at": registered_at,
"videos_checked_out": videos_checked_out
}
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, name=None, id=None):
for customer in self.list_customers():
if name:
if customer["name"]==name:
id = customer["id"]
print(customer)
self.selected_customer = customer
elif id == customer["id"]:
self.selected_customer = customer

if self.selected_customer == None:
return "Could not find customer by that name"

response = requests.get(self.url+f"/customers/{id}")
return response.json()

def update_customer(self,name=None,phone=None, postal_code= None, registered_at = None, videos_checked_out = 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"]
if not registered_at:
registered_at = self.selected_customer["registered_at"]
if not videos_checked_out:
videos_checked_out = self.selected_customer["videos_checked_out"]
query_params = {
"name": name,
"phone": phone,
"postal_code": postal_code,
"registered_at": registered_at,
"videos_checked_out":videos_checked_out

}
response = requests.put(self.url+f"/customers/{self.selected_customer['id']}",json=query_params)
print("response:", response)
self.selected_customer = response.json()
return response.json()

def delete_customer(self):
response = requests.delete(self.url+f"/customers/{self.selected_customer['id']}")
self.selected_customer = None
return response.json()


def print_selected(self):
if self.selected_customer:
print(f"Customer with id {self.selected_customer['id']} is currently selected\n")

# Rentals
def video_check_out(self,customer_id= None,video_id= None):
query_params = {
"customer_id": customer_id,
"video_id": video_id
}
for video in self.list_videos():
if video_id == video["id"]:
self.selected_video = video
print(customer_id, video_id)
response = requests.post(self.url+"/rentals/check-out", json=query_params)
print(self.url)
print(response.__dict__)
return response

def video_check_in(self, customer_id= None, video_id=None):
query_params = {
"video_id": video_id,
"customer_id": customer_id
}

for video in self.list_videos():
if video_id == video["id"]:
self.selected_video = video
response = requests.post(self.url+"/rentals/check-in", json=query_params)
if response != 200:
print("Please enter a valid video id and customer id")

return response
Loading