예제 #1
0
def create():
    schema = CategorySchema()

    try:
        data = schema.load(request.get_json())
        category = Category(**data)
        db.commit()
    except ValidationError as err:
        return jsonify({
            'message': 'Validation failed',
            'errors': err.messages
        }), 422
    return schema.dumps(category), 201
예제 #2
0
def update(category_id):
    schema = CategorySchema()
    category = Category.get(id=category_id)

    if not category:
        abort(404)

    try:
        data = schema.load(request.get_json())
        category.set(**data)
        db.commit()
    except ValidationError as err:
        return jsonify({
            'message': 'Validation failed',
            'errors': err.messages
        }), 422

    return schema.dumps(category)