Exemple #1
0
def discussion_board(board):
    '''
    Discussion Board Thead pages: Performs GET, POST, DELETE, PUT, or PATCH action based on the requet method.

        Parameters:
            board

        Returns:
            dependant on method
    '''
    if request.method == 'GET':
        resp = User().get_thread(board)
        if resp is None:
            return jsonify({"error": "Thread not found"}), 404

        resp = jsonify(resp)
        resp.status_code = 200
        return resp

    elif request.method == 'POST':
        post_to_add = request.get_json()
        resp = User().add_post(post_to_add, board)
        if resp is None:
            return jsonify({"error": "Thread not found"}), 404

        resp = jsonify(post_to_add)
        resp.status_code = 201
        return resp

    elif request.method == 'DELETE':
        post = request.get_json()
        if User().remove_post(post, board):
            return post
        return jsonify({"error": "Post not found"}), 404

    elif request.method == 'PUT':
        reply = request.get_json()
        resp = User().reply_to_post(reply, board)
        if resp is None:
            return jsonify({"error": "Thread or Post not found"}), 404

        resp = jsonify(resp)
        resp.status_code = 201
        return resp
Exemple #2
0
def discussion():
    '''
    Discussion Board Index page: Performs GET, POST, or DELETE action based on the requet method.

        Parameters:
            None

        Returns:
            dependant on method
    '''
    if request.method == 'GET':
        resp = jsonify(User().get_discussion_index())
        resp.status_code = 201

    elif request.method == 'POST':
        thread_to_add = request.get_json()
        resp = User().add_thread(thread_to_add)

        if resp is None:
            resp = jsonify({"error": "Thread already exists"})
            resp.status_code = 409
        else:
            resp = jsonify(resp)
            resp.status_code = 201

    elif request.method == 'DELETE':
        thread = request.get_json()
        if User().remove_thread(thread):
            resp = jsonify(thread)
            resp.status_code = 201
        else:
            resp = jsonify({"error": "Thread not found"})
            resp.status_code = 404

    elif request.method == 'PUT':
        thread =  request.get_json()
        out = User().update_thread(thread, 1)
        resp = jsonify(out)
        resp.status_code = 203

    elif request.method == 'PATCH':
        thread =  request.get_json()
        out = User().update_thread(thread, -1)
        resp = jsonify(out)
        resp.status_code = 203

    return resp