Untitled
unknown
plain_text
24 days ago
4.5 kB
3
Indexable
Never
from datetime import datetime from typing import List, Optional from sqlalchemy import update, select from strawberry import mutation, type from app.dependencies.get_db import get_db from app.graphql.schema import Info from app.graphql.types.content_type import ( CreateContentInput, DeleteContentInput, UpdateContentInput, ) from app.models import User from app.models.content import Content @type class UpdateMutationPayload: success: bool message: str = "" updated_contents: Optional[List[Content]] = None # Include updated contents @type class CreateMutationPayload: success: bool message: str = "" created_contents: Optional[List[Content]] = None # Include created contents @type class DeleteMutationPayload: success: bool message: str = "" deleted_ids: Optional[List[int]] = None # Include deleted content IDs @type class ContentMutation: @mutation def update_contents( self, info: Info, input: List[UpdateContentInput] ) -> UpdateMutationPayload: try: updated_contents = [] for content in input: stmt = ( update(Content) .where(Content.id == content.id) .values( type=content.type, text=content.text, link=content.link, order=content.order, updated_date=datetime.now(), # Automatically set to the current time ) ) info.context.session.execute(stmt) # Fetch the updated content from the database updated_content = info.context.session.execute( select(Content).where(Content.id == content.id) ).scalars().first() if updated_content: updated_contents.append(updated_content) info.context.session.commit() return UpdateMutationPayload( success=True, message="Content updated successfully", updated_contents=updated_contents ) except Exception as e: info.context.session.rollback() return UpdateMutationPayload( success=False, message=f"Update failed: {str(e)}" ) @mutation def create_contents( self, info: Info, input: List[CreateContentInput] ) -> CreateMutationPayload: try: created_contents = [] for content in input: new_content = Content( type=content.type, text=content.text, link=content.link, order=content.order, created_date=datetime.now(), updated_date=datetime.now(), ) info.context.session.add(new_content) created_contents.append(new_content) info.context.session.commit() return CreateMutationPayload( success=True, message="Content created successfully", created_contents=created_contents ) except Exception as e: info.context.session.rollback() return CreateMutationPayload( success=False, message=f"Creation failed: {str(e)}" ) @mutation def delete_contents( self, info: Info, input: List[DeleteContentInput] ) -> DeleteMutationPayload: try: deleted_ids = [] for content in input: # Mark the content as deleted stmt = ( update(Content) .where(Content.id == content.id) .values(is_deleted=True) ) info.context.session.execute(stmt) deleted_ids.append(content.id) info.context.session.commit() return DeleteMutationPayload( success=True, message=f"{len(deleted_ids)} content(s) deleted successfully", deleted_ids=deleted_ids ) except Exception as e: info.context.session.rollback() return DeleteMutationPayload( success=False, message=f"Deletion failed: {str(e)}" )
Leave a Comment