Skip to content
Permalink
Browse files

[#16] Category API

  • Loading branch information...
JoMingyu committed Mar 11, 2019
1 parent dcc0eb8 commit f7968a5a67e4a665a6afffc24d5a5c9a03bad575
Showing with 70 additions and 1 deletion.
  1. +7 −0 app/models/category.py
  2. +5 −1 app/views/__init__.py
  3. 0 app/views/board/__init__.py
  4. +58 −0 app/views/board/category.py
@@ -13,3 +13,10 @@ class TblCategories(Base):
author_id = Column(String(256), ForeignKey('tbl_users.id'))

author = relationship('TblUsers')

@property
def json(self):
return {
'id': self.id,
'name': self.name
}
@@ -3,7 +3,8 @@


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

handle_exception_func = flask_app.handle_exception
handle_user_exception_func = flask_app.handle_user_exception
@@ -14,13 +15,16 @@ def route(flask_app: Flask):
# - blueprint, api object initialize
api_v1_blueprint = Blueprint('api_v1', __name__)
api_user__account = Api(api_v1_blueprint, prefix='/user/account')
api_user__board = Api(api_v1_blueprint, prefix='/board')

# - route
api_user__account.add_resource(check_duplicate.IDDuplicateCheckAPI, '/check-duplicate/id/<id>')
api_user__account.add_resource(signup.SignupAPI, '/signup')
api_user__account.add_resource(auth.AuthAPI, '/auth')
api_user__account.add_resource(refresh.RefreshAPI, '/refresh')

api_user__board.add_resource(category.CategoryAPI, '/categories')

# - register blueprint
flask_app.register_blueprint(api_v1_blueprint)

No changes.
@@ -0,0 +1,58 @@
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.category import TblCategories
from app.views.base import BaseResource


class CategoryAPI(BaseResource):
class Schema:
class Post(BaseModel):
name = StringType(
serialized_name='name',
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:
category = TblCategories(
name=payload.name,
author_id=user.id
)

session.add(category)
session.commit()
session.refresh(category)

return {
'id': category.id
}
except IntegrityError:
abort(409)

@jwt_required
def get(self):
"""
카테고리 목록 조회 API
"""

session = main_db.session

return {
'data': [category.json for category in TblCategories.get_all(session)]
}

0 comments on commit f7968a5

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