menu.py 1.72 KB
Newer Older
Ai-Sasit's avatar
Ai-Sasit committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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})]