Skip to content

Commit 068a695

Browse files
committed
posts, users api 구현
1 parent 4d0a2bb commit 068a695

7 files changed

Lines changed: 139 additions & 16 deletions

File tree

app/posts/models.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,24 @@ class Post(DB.Model):
1010
__table_args__ = {'mysql_collate': 'utf8_general_ci'}
1111

1212
id = DB.Column(DB.Integer, primary_key=True)
13-
name = DB.Column(DB.String(255), nullable=False)
1413
author_id = DB.Column(DB.Integer, DB.ForeignKey(Users.id))
14+
name = DB.Column(DB.String(255), nullable=False)
15+
title = DB.Column(DB.String(255), nullable=False)
16+
body = DB.Column(DB.String(1024), nullable=False)
1517
author = DB.relationship("Users", uselist=False)
18+
created = DB.Column(DB.TIMESTAMP, server_default=text(
19+
"CURRENT_TIMESTAMP"), nullable=False)
1620

17-
def __init__(self, name: str, author_id: int):
21+
def __init__(self, name: str, title : str, body : str, author_id: int):
1822
self.name = name
23+
self.title = title
24+
self.body = body
1925
self.author_id = author_id
2026

2127
class PostSchema(Schema):
2228
id = fields.Integer()
2329
name = fields.Str()
30+
title = fields.Str()
31+
body = fields.Str()
2432
author = fields.Nested(UsersSchema)
33+
created = fields.Str()

app/posts/views.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,49 @@
99
from app.posts.models import Post, PostSchema
1010
from app.api.database import DB
1111

12-
API = Namespace('Posts',description="Post's REST API")
12+
API = Namespace('Posts', description="Post's REST API")
1313

1414
POSTS_SCHEMA = PostSchema()
1515

1616
POST_FIELDS = API.model('Post', {
1717
'name': fields.String,
18+
'title': fields.String,
19+
'body': fields.String,
1820
'author_id': fields.Integer,
1921
})
2022

21-
@API.route('/posts')
23+
24+
@API.route('')
2225
class Posts(Resource):
2326
parser = reqparse.RequestParser()
24-
parser.add_argument('name', required=True, type=str, help="post's name", location='json')
25-
parser.add_argument('author_id', required=True, type=int, help="post's author", location='json')
27+
parser.add_argument('name', required=True, type=str,
28+
help="post's name", location='json')
29+
parser.add_argument('title', required=True, type=str,
30+
help="post's title", location='json')
31+
parser.add_argument('body', required=True, type=str,
32+
help="post's body", location='json')
33+
parser.add_argument('author_id', required=True, type=int,
34+
help="post's author", location='json')
35+
36+
def get(self):
37+
try:
38+
posts_query = Post.query.all()
39+
body = jsonify(POSTS_SCHEMA.dump(posts_query, many=True).data)
40+
if posts_query:
41+
code = HTTPStatus.OK
42+
else:
43+
code = HTTPStatus.NOT_FOUND
44+
except SQLAlchemyError as err:
45+
message = str(err)
46+
body = jsonify({"message": message})
47+
code = HTTPStatus.INTERNAL_SERVER_ERROR
48+
return make_response(body, code.value)
2649

2750
@API.expect(POST_FIELDS)
2851
def post(self):
2952
args_ = self.parser.parse_args()
30-
post = Post(name=args_['name'], author_id=args_['author_id'])
53+
post = Post(name=args_['name'], title=args_['title'], body=args_[
54+
'body'], author_id=args_['author_id'])
3155
try:
3256
DB.session.add(post)
3357
DB.session.commit()
@@ -40,12 +64,14 @@ def post(self):
4064
code = HTTPStatus.INTERNAL_SERVER_ERROR
4165
return make_response(body, code.value)
4266

43-
@API.route('/post/<int:seqno>')
67+
68+
@API.route('/<int:seqno>')
4469
class PostItem(Resource):
4570
def get(self, seqno):
4671
try:
47-
post_item = DB.session.query(Post).outerjoin(Users, Users.id==Post.author_id).filter(Post.id==seqno).first()
48-
body = jsonify({"post":POSTS_SCHEMA.dump(post_item).data})
72+
post_item = DB.session.query(Post).outerjoin(
73+
Users, Users.id == Post.author_id).filter(Post.id == seqno).first()
74+
body = jsonify({"post": POSTS_SCHEMA.dump(post_item).data})
4975
if post_item:
5076
code = HTTPStatus.OK
5177
else:

app/users/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class Users(DB.Model):
99
__table_args__ = {'mysql_collate': 'utf8_general_ci'}
1010

1111
id = DB.Column(DB.Integer, primary_key=True)
12-
name = DB.Column(DB.String(255), nullable=False)
13-
email = DB.Column(DB.String(50), unique=True, nullable=False)
12+
name = DB.Column(DB.String(255), unique=True, nullable=False)
13+
email = DB.Column(DB.String(50), nullable=False)
1414
password = DB.Column(DB.String(255), nullable=False)
1515
created = DB.Column(DB.TIMESTAMP, server_default=text(
1616
"CURRENT_TIMESTAMP"), nullable=False)

app/users/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
USERS_SCHEMA = UsersSchema()
1414

15-
@API.route('/users/<int:user_id>')
15+
@API.route('<int:user_id>')
1616
@API.param('user_id', 'The user identifier')
1717
class UserItem(Resource):
1818
parser = reqparse.RequestParser()
@@ -48,7 +48,7 @@ def delete(self, user_id):
4848
return response
4949

5050

51-
@API.route('/users')
51+
@API.route('')
5252
class UsersList(Resource):
5353
parser = reqparse.RequestParser()
5454
parser.add_argument('name', required=True, type=str, help="user's name", location='json')
@@ -82,7 +82,7 @@ def post(self):
8282
code = HTTPStatus.INTERNAL_SERVER_ERROR
8383
return make_response(body, code.value)
8484

85-
@API.route('/users/auth')
85+
@API.route('/auth')
8686
class GetUser(Resource):
8787
parser = reqparse.RequestParser()
8888
parser.add_argument('name', required=True, type=str, help="user's name", location='json')
@@ -97,7 +97,7 @@ class GetUser(Resource):
9797
def post(self):
9898
args = self.parser.parse_args()
9999
try:
100-
user = Users.query.filter(Users.name == args['name'] and Users.password == args['password']).first()
100+
user = Users.query.filter(Users.name == args['name'], Users.password == args['password']).first()
101101
body = jsonify({"user" : USERS_SCHEMA.dump(user).data})
102102
if user:
103103
code = HTTPStatus.OK
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""empty message
2+
3+
Revision ID: 42d37d473148
4+
Revises: e98a498cca09
5+
Create Date: 2019-07-20 11:37:30.071743
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '42d37d473148'
14+
down_revision = 'e98a498cca09'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column('posts', sa.Column('created', sa.TIMESTAMP(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False))
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
op.drop_column('posts', 'created')
28+
# ### end Alembic commands ###
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""empty message
2+
3+
Revision ID: 76e0c583e527
4+
Revises: d6202514e0b0
5+
Create Date: 2019-07-20 10:27:06.086196
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '76e0c583e527'
14+
down_revision = 'd6202514e0b0'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.drop_index('email', table_name='users')
22+
op.create_unique_constraint(None, 'users', ['name'])
23+
# ### end Alembic commands ###
24+
25+
26+
def downgrade():
27+
# ### commands auto generated by Alembic - please adjust! ###
28+
op.drop_constraint(None, 'users', type_='unique')
29+
op.create_index('email', 'users', ['email'], unique=True)
30+
# ### end Alembic commands ###
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""empty message
2+
3+
Revision ID: e98a498cca09
4+
Revises: 76e0c583e527
5+
Create Date: 2019-07-20 10:33:42.873220
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'e98a498cca09'
14+
down_revision = '76e0c583e527'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column('posts', sa.Column('body', sa.String(length=1024), nullable=False))
22+
op.add_column('posts', sa.Column('title', sa.String(length=255), nullable=False))
23+
# ### end Alembic commands ###
24+
25+
26+
def downgrade():
27+
# ### commands auto generated by Alembic - please adjust! ###
28+
op.drop_column('posts', 'title')
29+
op.drop_column('posts', 'body')
30+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)