perf: makes update endpoint flexible and reduces updates

This commit is contained in:
sauravniraula 2025-09-03 21:35:18 +05:45
parent 6a5efc5c98
commit 0c5c53e24c
No known key found for this signature in database
GPG key ID: 60FCC1B5A5E83326
3 changed files with 26 additions and 46 deletions

View file

@ -293,35 +293,43 @@ async def stream_presentation(
return StreamingResponse(inner(), media_type="text/event-stream")
@PRESENTATION_ROUTER.put("/update", response_model=PresentationWithSlides)
@PRESENTATION_ROUTER.patch("/update", response_model=PresentationWithSlides)
async def update_presentation(
presentation_with_slides: Annotated[PresentationWithSlides, Body()],
id: Annotated[uuid.UUID, Body()],
n_slides: Annotated[Optional[int], Body()] = None,
title: Annotated[Optional[str], Body()] = None,
slides: Annotated[Optional[List[SlideModel]], Body()] = None,
sql_session: AsyncSession = Depends(get_async_session),
):
updated_presentation = presentation_with_slides.to_presentation_model()
updated_slides = presentation_with_slides.slides
presentation = await sql_session.get(PresentationModel, updated_presentation.id)
presentation = await sql_session.get(PresentationModel, id)
if not presentation:
raise HTTPException(status_code=404, detail="Presentation not found")
presentation.sqlmodel_update(updated_presentation)
presentation_update_dict = {}
if n_slides:
presentation_update_dict["n_slides"] = n_slides
if title:
presentation_update_dict["title"] = title
await sql_session.execute(
delete(SlideModel).where(SlideModel.presentation == updated_presentation.id)
)
if n_slides or title:
presentation.sqlmodel_update(presentation_update_dict)
# Just to make sure id is UUID
for slide in updated_slides:
slide.presentation = uuid.UUID(slide.presentation)
slide.id = uuid.UUID(slide.id)
if slides:
# Just to make sure id is UUID
for slide in slides:
slide.presentation = uuid.UUID(slide.presentation)
slide.id = uuid.UUID(slide.id)
await sql_session.execute(
delete(SlideModel).where(SlideModel.presentation == presentation.id)
)
sql_session.add_all(slides)
sql_session.add_all(updated_slides)
await sql_session.commit()
return PresentationWithSlides(
**presentation.model_dump(),
slides=updated_slides,
slides=slides or [],
)

View file

@ -4,46 +4,18 @@ import uuid
from pydantic import BaseModel
from models.presentation_layout import PresentationLayoutModel
from models.presentation_outline_model import PresentationOutlineModel
from models.presentation_structure_model import PresentationStructureModel
from models.sql.presentation import PresentationModel
from models.sql.slide import SlideModel
class PresentationWithSlides(BaseModel):
id: uuid.UUID
user: uuid.UUID
content: str
n_slides: int
language: str
file_paths: Optional[List[str]]
title: Optional[str] = None
outlines: Optional[PresentationOutlineModel] = None
created_at: datetime
updated_at: datetime
layout: Optional[PresentationLayoutModel]
structure: Optional[PresentationStructureModel] = None
instructions: Optional[str] = None
tone: Optional[str] = None
verbosity: Optional[str] = None
slides: List[SlideModel]
def to_presentation_model(self) -> PresentationModel:
return PresentationModel(
id=self.id,
content=self.content,
n_slides=self.n_slides,
language=self.language,
file_paths=self.file_paths,
title=self.title,
outlines=self.outlines.model_dump(mode="json") if self.outlines else None,
created_at=self.created_at,
updated_at=self.updated_at,
layout=self.layout.model_dump(mode="json") if self.layout else None,
structure=(
self.structure.model_dump(mode="json") if self.structure else None
),
instructions=self.instructions,
tone=self.tone,
verbosity=self.verbosity,
)

View file

@ -113,7 +113,7 @@ export class PresentationGenerationApi {
const response = await fetch(
`/api/v1/ppt/presentation/update`,
{
method: "PUT",
method: "PATCH",
headers: getHeader(),
body: JSON.stringify(body),
cache: "no-cache",