37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
from flask import Flask, jsonify, request
|
|
from neo4j import GraphDatabase
|
|
|
|
class Neo4JAPis:
|
|
|
|
def __init__(self, uri, username, password):
|
|
self.app = Flask(__name__)
|
|
self.setup_routes()
|
|
self.driver = GraphDatabase.driver(uri, auth=(username, password))
|
|
|
|
|
|
def setup_routes(self):
|
|
@self.app.route('/article', methods=['GET'])
|
|
def get_article():
|
|
with self.driver.session() as session:
|
|
result = session.run("MATCH (a:Artikel) RETURN a.name AS name, a.EANGTIN as EANGTIN")
|
|
article = [{"name": record["name"], "EANGTIN": record["EANGTIN"]} for record in result]
|
|
return jsonify(article)
|
|
|
|
@self.app.route('/autocomplete', methods=['GET'])
|
|
def autocomplete(parameters = None):
|
|
with self.driver.session() as session:
|
|
search_string = request.args.get('search_string')
|
|
query = f"""
|
|
MATCH (a:Artikel)
|
|
WHERE a.name STARTS WITH "{search_string}"
|
|
RETURN a.name AS name
|
|
LIMIT 10
|
|
"""
|
|
result = session.run(query.format(search_string=search_string))
|
|
suggestion = [record["name"] for record in result]
|
|
return jsonify(suggestion)
|
|
|
|
|
|
|
|
def run(self, debug=True):
|
|
self.app.run(debug=debug) |