from fastapi import APIRouter from bson.objectid import ObjectId from server.mock.schemas import Category from server.mock.model import Category_helper from server.service.database import get_connection cat = APIRouter() collection = get_connection('category') @cat.get('/category/{sid}') async def get_category(sid: str): return [Category_helper(i) for i in collection.find({'shop_id': sid})] @cat.get('/category/{sid}/{id}') async def get_category_by_id(sid: str, id: str): category = collection.find_one({"_id": ObjectId(id), "shop_id": sid}) return Category_helper(category) @cat.post('/category') async def create_category(category: Category): payload = { 'shop_id': category.shop_id, 'name': category.name } collection.insert_one(payload) return [Category_helper(i) for i in collection.find({'shop_id': category.shop_id})] @cat.patch('/category/{sid}/{id}') async def update_category(sid: str, id: str, category: Category): payload = { 'name': category.name, } collection.update_one({"_id": ObjectId(id)}, {"$set": payload}) return [Category_helper(i) for i in collection.find({"shop_id": sid})] @cat.delete('/category/{sid}/{id}') async def delete_category(sid:str, id: str): collection.delete_one({"_id": ObjectId(id)}) return [Category_helper(i) for i in collection.find({"shop_id": sid})]