summaryrefslogtreecommitdiff
path: root/src/services/create_reply_service.py
blob: 12600dd5a56e848be5070f5a6b831dce7796fd85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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