feat(sqlalchemy): Replace peewee with sqlalchemy
This commit is contained in:
@@ -6,6 +6,7 @@ from fastapi import APIRouter
|
||||
from pydantic import BaseModel
|
||||
import json
|
||||
|
||||
from apps.webui.internal.db import get_db
|
||||
from apps.webui.models.documents import (
|
||||
Documents,
|
||||
DocumentForm,
|
||||
@@ -25,7 +26,7 @@ router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[DocumentResponse])
|
||||
async def get_documents(user=Depends(get_current_user)):
|
||||
async def get_documents(user=Depends(get_current_user), db=Depends(get_db)):
|
||||
docs = [
|
||||
DocumentResponse(
|
||||
**{
|
||||
@@ -33,7 +34,7 @@ async def get_documents(user=Depends(get_current_user)):
|
||||
"content": json.loads(doc.content if doc.content else "{}"),
|
||||
}
|
||||
)
|
||||
for doc in Documents.get_docs()
|
||||
for doc in Documents.get_docs(db)
|
||||
]
|
||||
return docs
|
||||
|
||||
@@ -44,10 +45,12 @@ async def get_documents(user=Depends(get_current_user)):
|
||||
|
||||
|
||||
@router.post("/create", response_model=Optional[DocumentResponse])
|
||||
async def create_new_doc(form_data: DocumentForm, user=Depends(get_admin_user)):
|
||||
doc = Documents.get_doc_by_name(form_data.name)
|
||||
async def create_new_doc(
|
||||
form_data: DocumentForm, user=Depends(get_admin_user), db=Depends(get_db)
|
||||
):
|
||||
doc = Documents.get_doc_by_name(db, form_data.name)
|
||||
if doc == None:
|
||||
doc = Documents.insert_new_doc(user.id, form_data)
|
||||
doc = Documents.insert_new_doc(db, user.id, form_data)
|
||||
|
||||
if doc:
|
||||
return DocumentResponse(
|
||||
@@ -74,8 +77,10 @@ async def create_new_doc(form_data: DocumentForm, user=Depends(get_admin_user)):
|
||||
|
||||
|
||||
@router.get("/doc", response_model=Optional[DocumentResponse])
|
||||
async def get_doc_by_name(name: str, user=Depends(get_current_user)):
|
||||
doc = Documents.get_doc_by_name(name)
|
||||
async def get_doc_by_name(
|
||||
name: str, user=Depends(get_current_user), db=Depends(get_db)
|
||||
):
|
||||
doc = Documents.get_doc_by_name(db, name)
|
||||
|
||||
if doc:
|
||||
return DocumentResponse(
|
||||
@@ -106,8 +111,12 @@ class TagDocumentForm(BaseModel):
|
||||
|
||||
|
||||
@router.post("/doc/tags", response_model=Optional[DocumentResponse])
|
||||
async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_current_user)):
|
||||
doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
|
||||
async def tag_doc_by_name(
|
||||
form_data: TagDocumentForm, user=Depends(get_current_user), db=Depends(get_db)
|
||||
):
|
||||
doc = Documents.update_doc_content_by_name(
|
||||
db, form_data.name, {"tags": form_data.tags}
|
||||
)
|
||||
|
||||
if doc:
|
||||
return DocumentResponse(
|
||||
@@ -130,9 +139,12 @@ async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_current_u
|
||||
|
||||
@router.post("/doc/update", response_model=Optional[DocumentResponse])
|
||||
async def update_doc_by_name(
|
||||
name: str, form_data: DocumentUpdateForm, user=Depends(get_admin_user)
|
||||
name: str,
|
||||
form_data: DocumentUpdateForm,
|
||||
user=Depends(get_admin_user),
|
||||
db=Depends(get_db),
|
||||
):
|
||||
doc = Documents.update_doc_by_name(name, form_data)
|
||||
doc = Documents.update_doc_by_name(db, name, form_data)
|
||||
if doc:
|
||||
return DocumentResponse(
|
||||
**{
|
||||
@@ -153,6 +165,8 @@ async def update_doc_by_name(
|
||||
|
||||
|
||||
@router.delete("/doc/delete", response_model=bool)
|
||||
async def delete_doc_by_name(name: str, user=Depends(get_admin_user)):
|
||||
result = Documents.delete_doc_by_name(name)
|
||||
async def delete_doc_by_name(
|
||||
name: str, user=Depends(get_admin_user), db=Depends(get_db)
|
||||
):
|
||||
result = Documents.delete_doc_by_name(db, name)
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user