from sqlalchemy import select from src.database.models import Blog, Comment, Reply from src.database import db from src.services.reply_creation_callback import ReplyCreationCallbacks class CreateReplyService: def __init__(self, reply_data, comment_id): self._comment_id = comment_id self._content = reply_data.get('content') self._email = reply_data.get('email') self._author = reply_data.get('author') def call(self): if not self._comment_exists(): return 404 reply = ReplyCreationCallbacks( Reply(comment_id=self._comment_id, content=self._content, email=self._email, author=self._author) ).run_callbacks() db.session.add(reply) db.session.commit() return 200 def _comment_exists(self): search_comment_statement = select(Comment).where( Comment.id == self._comment_id ) return db.session.execute(search_comment_statement) \ .scalars().first() is not None