Skip to content
Permalink
Browse files

[#17] Post API

  • Loading branch information...
JoMingyu committed Mar 12, 2019
1 parent a0421c0 commit 11f536628ae547f2764cbc9c465861e9cdf93da8
Showing with 92 additions and 1 deletion.
  1. +8 −0 app/models/post.py
  2. +2 −1 app/views/__init__.py
  3. +82 −0 app/views/board/post.py
@@ -15,3 +15,11 @@ class TblPosts(Base):

owner = relationship('TblUsers')
category = relationship('TblCategories')

@property
def json_for_list(self):
return {
'id': self.id,
'title': self.title,
'ownerNickname': self.owner.nickname
}
@@ -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
from app.views.board import category, post

handle_exception_func = flask_app.handle_exception
handle_user_exception_func = flask_app.handle_user_exception
@@ -24,6 +24,7 @@ def route(flask_app: Flask):
api_user__account.add_resource(refresh.RefreshAPI, '/refresh')

api_user__board.add_resource(category.CategoryAPI, '/categories')
api_user__board.add_resource(post.PostAPI, '/posts')

# - register blueprint
flask_app.register_blueprint(api_v1_blueprint)
@@ -0,0 +1,82 @@
from flask import abort
from flask_jwt_extended import jwt_required
from schematics.types import IntType, 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.post import TblPosts
from app.views.base import BaseResource


class PostAPI(BaseResource):
class Schema:
class Post(BaseModel):
category_id = IntType(
serialized_name='categoryID',
required=True
)

title = StringType(
serialized_name='title',
required=True,
max_length=TblPosts.title.type.length
)

content = StringType(
serialized_name='content',
required=True,
max_length=TblPosts.content.type.length
)

class Get(BaseModel):
category_id = IntType(
serialized_name='category_id',
required=True
)

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

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

try:
post = TblPosts(
title=payload.title,
content=payload.content,
owner_id=user.id,
category_id=payload.category_id
)

session.add(post)
session.commit()
session.refresh(post)

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

@validate_with_schematics(PayloadLocation.ARGS, Schema.Get)
@jwt_required
def get(self):
"""
게시글 목록 API
"""

session = main_db.session
payload: self.Schema.Get = context_property.request_payload_object

return {
'data': [
post.json_for_list for post in TblPosts.get_all(session, TblPosts.category_id == payload.category_id)
]
}

0 comments on commit 11f5366

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