예제 #1
0
def post_create():
    """
    새로운 Post 등록
    ---
    tags:
    - post
    parameters:
    - in: "body"
      name: "body"
      description: "Pet object that needs to be added to the store"
      required: true
      schema:
        $ref: "#/definitions/PostInput"
    responses:
      201:
        description: "생성됨"
      403:
        description: "권한 없음"
      405:
        description: "Invalid input"
    """
    payload = request.get_json()
    req = PostCreateRequestObject.from_dict({
        "title": payload.get("title"),
        "body": payload.get("body")
    })
    uc = PostCreateUseCase(repo=current_repo)
    resp = uc.execute(req)

    if resp:
        schema = PostSchema()
        post = schema.dump(resp.value).data
        return jsonify(post)
    else:
        return jsonify({}), 500
예제 #2
0
def post_item(post_id):
    """
    Post 조회
    ---
    tags:
    - post
    parameters:
    - in: "path"
      name: "post_id"
      description: "게시물 번호"
      required: true
      type: "integer"
    responses:
      200:
        description: "성공"
        schema:
          $ref: "#/definitions/Post"
      403:
        description: "게시물 조회 권한이 없음"
      404:
        description: "게시물이 존재하지 않음"
    """

    req = PostItemRequestObject.from_dict({"id": int(post_id)})
    uc = PostItemUseCase(repo=current_repo)
    resp = uc.execute(req)

    if resp:
        schema = PostSchema()
        post = schema.dump(resp.value).data
        return jsonify(post)
    elif resp.type == "NotFoundError":
        return jsonify({}), 404
    else:
        return jsonify({}), 500
예제 #3
0
def post_list():
    """
    Post 목록
    ---
    tags:
    - post
    responses:
      200:
        description: "Success"
        schema:
          type: "array"
          items:
            $ref: "#/definitions/Post"
    """

    req = PostListRequestObject()
    uc = PostListUseCase(repo=current_repo)
    resp = uc.execute(req)

    if resp:
        schema = PostSchema(many=True, exclude=("comments", ))
        posts = schema.dump(resp.value).data
        return jsonify(posts)
    else:
        return jsonify({}), 400
예제 #4
0
def get_by_category(cid):
    posts = Post.query.join(post_category).filter(post_category.c.category_id == cid).all()

    if not posts:
        return None, ec[404]

    post_schema = PostSchema(many=True)
    post_data, errs = post_schema.dump(posts)

    return post_data, errs
예제 #5
0
def get_by_slug(slug):
    posts = Post.query.filter(Post.slug.like(slug)).all()

    if not posts:
        return None, ec[404]

    post_schema = PostSchema(many=True)
    post_data, errs = post_schema.dump(posts)

    return post_data, errs
예제 #6
0
def get_by_id(pid):
    post = Post.query.filter_by(id=pid).one_or_none()

    if not post:
        return None, ec[404]

    post_schema = PostSchema()
    post_data, errs = post_schema.dump(post)

    return post_data, errs
예제 #7
0
def delete(pid):
    post = Post.query.filter_by(id=pid).one_or_none()

    if not post:
        return None, ec[404]

    db.session.delete(post)
    db.session.commit()

    post_schema = PostSchema()
    post_data, errs = post_schema.dump(post)

    return post_data, errs
예제 #8
0
def update(pid, data):
    post_schema = PostSchema()
    post_model, errs = post_schema.load(data)

    if errs:
        return None, errs

    post = Post.query.filter_by(id=pid).one_or_none()

    if not post:
        return None, ec[409]

    post = db.session.merge(post_model)
    db.session.commit()

    post_data, errs = post_schema.dump(post)

    return post_data, errs
def test_serialize_domain_post():
    post = Post(id=1, title="title", body="body text")

    expected_json = """
    {
      "id": 1,
      "title": "title",
      "body": "body text"
    }
    """

    json_post = PostSchema(exclude=("comments", )).dump(post).data

    assert json_post == json.loads(expected_json)
예제 #10
0
def create(uid, data):
    post_schema = PostSchema()
    print(data)
    new_post, errs = post_schema.load(data)

    if errs:
        return None, format_schema_errors(errs)

    new_post.user_id = uid
    # get list categories by ids
    db.session.add(new_post)
    db.session.commit()
    db.session.refresh(new_post)

    categories = Category.query.filter(Category.id.in_(data['categories'])).all()
    for c in categories:
        new_post.categories.append(c)

    new_post.slug = slugify(new_post.title + "-" + str(new_post.id))
    db.session.commit()

    post, errs = post_schema.dump(new_post)

    return post, errs
def test_serialize_domain_post_with_comments():
    post = Post(
        id=1,
        title="title",
        body="body text",
        comments=[dict(id=1, body="body one"),
                  dict(id=2, body="body two")],
    )

    expected_json = """
    {
      "id": 1,
      "title": "title",
      "body": "body text",
      "comments": [
        { "id": 1, "body": "body one" },
        { "id": 2, "body": "body two" }
      ]
    }
    """

    json_post = PostSchema().dump(post).data

    assert json_post == json.loads(expected_json)