from fastapi import APIRouter from bson.objectid import ObjectId from server.mock.schemas import Menu from server.mock.model import Menu_helper from server.service.database import get_connection menu = APIRouter() collection = get_connection('Menu') @menu.get('/menu/{sid}') async def get_menu(sid: str): return [Menu_helper(i) for i in collection.find({'shop_id': sid})] @menu.get('/menu/{sid}/{id}') async def get_menu_by_id(sid: str, id: str): menu = collection.find_one({"_id": ObjectId(id), 'shop_id': sid}) return Menu_helper(menu) @menu.post('/menu/') async def create_menu(menu: Menu): payload = { 'shop_id' : menu.shop_id, 'status': menu.status, 'name': menu.name, 'category': menu.category, 'price': menu.price, 'images': menu.images, } collection.insert_one(payload) return [Menu_helper(i) for i in collection.find({'shop_id': menu.shop_id})] @menu.patch('/menu/{sid}/{id}') async def update_menu(sid: str, id: str, menu: Menu): payload = { 'name': menu.name, 'price': menu.price, 'category': menu.category, } collection.update_one({"_id": ObjectId(id)}, {"$set": payload}) return [Menu_helper(i) for i in collection.find({'shop_id': sid})] @menu.patch('/menu_status/{sid}/{id}') async def update_menu_status(sid: str, id: str, menu: Menu): payload = { 'status': menu.status, } collection.update_one({"_id": ObjectId(id)}, {"$set": payload}) return [Menu_helper(i) for i in collection.find({'shop_id': sid})] @menu.delete('/menu/{sid}/{id}') async def delete_menu(sid: str, id: str): collection.delete_one({"_id": ObjectId(id)}) return [Menu_helper(i) for i in collection.find({'shop_id': sid})]