diff --git a/python/items.json b/python/items.json new file mode 100644 index 000000000..bd1ca8caa --- /dev/null +++ b/python/items.json @@ -0,0 +1,19 @@ +[ + { + "name": "jacket", + "category": "fashion" + }, + { + "name": "Shirt", + "category": "fashion" + }, + { + "name": "Shoes", + "category": "Sports" + }, + { + "name": "Bottle", + "category": "Electric", + "image_name": "ad55d25f2c10c56522147b214aeed7ad13319808d7ce999787ac8c239b24f71d" + } +] \ No newline at end of file diff --git a/python/main.py b/python/main.py index 8dc3c0e62..970a0edd1 100644 --- a/python/main.py +++ b/python/main.py @@ -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 @@ -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} . @@ -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 \ No newline at end of file