show db items in a table, and can create new items with form
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
from flask import jsonify, request
|
||||
import rethinkdb as r
|
||||
|
||||
table_name = 'conditions'
|
||||
route = table_name
|
||||
|
||||
def create_conditions_routes(app, r, conn):
|
||||
# Create a table (if not exists)
|
||||
if table_name not in r.table_list().run(conn):
|
||||
r.table_create(table_name).run(conn)
|
||||
|
||||
# Routes for CRUD operations
|
||||
|
||||
# Create operation
|
||||
@app.route('/' + route, methods=['POST'])
|
||||
def create_condition():
|
||||
if request.headers['Content-Type'] == 'application/json':
|
||||
data = request.json
|
||||
else: # Assuming form data is in 'application/x-www-form-urlencoded' format
|
||||
data = request.form.to_dict()
|
||||
result = r.table(table_name).insert(data).run(conn)
|
||||
return jsonify({'id': result['generated_keys'][0]}), 201
|
||||
|
||||
# Read operation
|
||||
@app.route('/' + route, methods=['GET'])
|
||||
def get_conditions():
|
||||
cursor = r.table(table_name).run(conn)
|
||||
conditions = list(cursor)
|
||||
return jsonify(conditions)
|
||||
|
||||
@app.route('/' + route + '/<id>', methods=['GET'])
|
||||
def get_condition(id):
|
||||
condition = r.table(table_name).get(id).run(conn)
|
||||
return jsonify(condition)
|
||||
|
||||
# Update operation
|
||||
@app.route('/' + route + '/<id>', methods=['PUT'])
|
||||
def update_condition(id):
|
||||
data = request.get_json()
|
||||
r.table(table_name).get(id).update(data).run(conn)
|
||||
return jsonify({'message': 'condition updated successfully'})
|
||||
|
||||
# Delete operation
|
||||
@app.route('/' + route + '/<id>', methods=['DELETE'])
|
||||
def delete_condition(id):
|
||||
r.table(table_name).get(id).delete().run(conn)
|
||||
return jsonify({'message': 'condition deleted successfully'})
|
||||
Reference in New Issue
Block a user