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
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})]