Skip to content
Permalink
Browse files

[#18] Comment API

  • Loading branch information...
JoMingyu committed Mar 13, 2019
1 parent 877d198 commit 728722c3114293801295547164122a5e97cc5ba6
Showing with 71 additions and 1 deletion.
  1. +7 −0 app/models/comment.py
  2. +2 −1 app/views/__init__.py
  3. +62 −0 app/views/board/comment.py
@@ -14,3 +14,10 @@ class TblComments(Base):

owner = relationship('TblUsers')
post = relationship('TblPosts')

@property
def json(self):
return {
'id': self.id,
'content': self.content
}
@@ -4,7 +4,7 @@

def route(flask_app: Flask):
from app.views.user.account import auth, check_duplicate, refresh, signup
from app.views.board import category, post
from app.views.board import category, post, comment

handle_exception_func = flask_app.handle_exception
handle_user_exception_func = flask_app.handle_user_exception
@@ -26,6 +26,7 @@ def route(flask_app: Flask):
api_user__board.add_resource(category.CategoryAPI, '/categories')
api_user__board.add_resource(post.PostAPI, '/posts')
api_user__board.add_resource(post.PostItemAPI, '/posts/<int:post_id>')
api_user__board.add_resource(comment.CommentAPI, '/posts/<int:post_id>/comments')

# - register blueprint
flask_app.register_blueprint(api_v1_blueprint)
@@ -0,0 +1,62 @@
from flask import abort
from flask_jwt_extended import jwt_required
from schematics.types import StringType
from sqlalchemy.exc import IntegrityError

from app.context import context_property
from app.decorators.validation import PayloadLocation, BaseModel, validate_with_schematics
from app.extensions import main_db
from app.models.comment import TblComments
from app.views.base import BaseResource


class CommentAPI(BaseResource):
class Schema:
class Post(BaseModel):
content = StringType(
serialized_name='content',
required=True,
max_length=TblComments.content.type.length
)

@validate_with_schematics(PayloadLocation.JSON, Schema.Post)
@jwt_required
def post(self, post_id):
"""
댓글 작성 API
"""

payload: self.Schema.Post = context_property.request_payload_object
session = main_db.session
user = context_property.requested_user

try:
comment = TblComments(
content=payload.content,
owner_id=user.id,
post_id=post_id
)

session.add(comment)
session.commit()
session.refresh(comment)

return {
'id': comment.id
}
except IntegrityError:
abort(404, 'Post ID `{}` not found.'.format(payload.category_id))

@jwt_required
def get(self, post_id):
"""
댓글 목록 API
"""

session = main_db.session

return {
'data': [
comment.json for comment in TblComments.get_all(session, TblComments.post_id == post_id)
]
}

0 comments on commit 728722c

Please sign in to comment.
You can’t perform that action at this time.