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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions python/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"name": "jacket",
"category": "fashion"
},
{
"name": "Shirt",
"category": "fashion"
},
{
"name": "Shoes",
"category": "Sports"
},
{
"name": "Bottle",
"category": "Electric",
"image_name": "ad55d25f2c10c56522147b214aeed7ad13319808d7ce999787ac8c239b24f71d"
}
]
76 changes: 73 additions & 3 deletions python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import sqlite3
from pydantic import BaseModel
from contextlib import asynccontextmanager
import json
import hashlib


# Define the path to the images & sqlite3 database
Expand Down Expand Up @@ -69,13 +71,49 @@ class AddItemResponse(BaseModel):
@app.post("/items", response_model=AddItemResponse)
def add_item(
name: str = Form(...),
category: str = Form(...),
image_name: str = Form(None),
db: sqlite3.Connection = Depends(get_db),
):
if not name:
raise HTTPException(status_code=400, detail="name is required")

if not category:
raise HTTPException(status_code=400, detail="category is required")

insert_item(Item(name=name))
return AddItemResponse(**{"message": f"item received: {name}"})
if not image_name or image_name.strip() == "":
image_name = "images/default.jpg"


image_hash = hash_image(image_name)+".jpg"
insert_item(Item(name=name, category=category ,image_name=image_hash))
return AddItemResponse(**{"message": f"item received:name= {name}, category= {category}, image={image_hash}"})


@app.get("/items")
def get_items():
with open('items.json', 'r') as json_file:
try:
data = json.load(json_file)
except json.JSONDecodeError:
data = {}
return data


@app.get("/item/{item_id}")
def get_item(item_id):
with open('items.json', 'r') as json_file:
try:
data = json.load(json_file)
except json.JSONDecodeError:
data = {}

if not data:
raise HTTPException(Status_code=400, detail="Item does not exist")

item = data[int(item_id) - 1]

return item


# get_image is a handler to return an image for GET /images/{filename} .
Expand All @@ -96,8 +134,40 @@ async def get_image(image_name):

class Item(BaseModel):
name: str
category: str
image_name: str


def insert_item(item: Item):
# STEP 4-2: add an implementation to store an item
pass
# pass

with open('items.json', 'r') as json_file:
try:
data = json.load(json_file)
except json.JSONDecodeError:
data = []

new_data = {
"name": item.name,
"category": item.category,
"image_name":item.image_name
}

# if 'items' not in data:
# data = []

data.append(new_data)

with open('items.json', 'w') as json_file:
json.dump(data, json_file, indent=4)


def hash_image(image):
with open(image, "rb") as f:
try:
image_bytes = f.read()
except:
raise HTTPException(status_code=400, detail="Image not found")
image_hash = hashlib.sha256(image_bytes).hexdigest()
return image_hash