bsition/api/components/document.py

52 lines
1.3 KiB
Python

import json
from fastapi import APIRouter, Response, status
from pydantic import BaseModel
from pydantic.types import Optional
import backend.elastic as elastic
import backend.mongo as mongo
from api.middleware.verify_token import VerifyTokenRoute
documents_routes = APIRouter(route_class=VerifyTokenRoute)
class Document(BaseModel):
name: str
access: list
data: str
owner: str
class DocumentUpdate(BaseModel):
name: Optional[str]
access: Optional[list]
data: Optional[str]
@documents_routes.post("/documents")
def create(aux: Document, response: Response):
mongo.create_document(json.loads(json.dumps(aux.__dict__)))
response.status_code = status.HTTP_201_CREATED
@documents_routes.get("/documents/{id}")
def get_by_id(id: str):
return mongo.get_document_by_id(id)
@documents_routes.put("/documents/{id}")
def edit_data(aux: DocumentUpdate, id: str, response: Response):
if aux.data is not None:
mongo.edit_data(id, aux.data)
if aux.access is not None:
mongo.edit_access(id, aux.access)
if aux.name is not None:
mongo.edit_name(id, aux.name)
response.status_code = status.HTTP_202_ACCEPTED
@documents_routes.get("/documents")
def search(query: str):
return elastic.search("test-index", query)