add relationship endpoints
This commit is contained in:
parent
2d039f7b8f
commit
faeea6e4e0
62
api/endpoints/create_cosponsored_relationship.py
Normal file
62
api/endpoints/create_cosponsored_relationship.py
Normal file
@ -0,0 +1,62 @@
|
||||
# endpoints/create_cosponsored_relationship.py
|
||||
|
||||
from flask import Blueprint, jsonify
|
||||
import logging
|
||||
import csv
|
||||
import os
|
||||
from neo4j import GraphDatabase
|
||||
from app import get_driver, neo4j_logger # Ensure relative imports work
|
||||
|
||||
bp = Blueprint('create_cosponsored_relationship', __name__)
|
||||
|
||||
@bp.route('/create_cosponsored_relationship', methods=['GET'])
|
||||
def create_cosponsored_relationship():
|
||||
try:
|
||||
# Path to the CSV file
|
||||
csv_file_path = os.path.join(os.path.dirname(__file__), '..', 'cosponsored_legislation.csv')
|
||||
|
||||
if not os.path.exists(csv_file_path):
|
||||
return jsonify({"status": "error", "message": "CSV file not found"}), 404
|
||||
|
||||
driver = get_driver()
|
||||
with driver.session() as session:
|
||||
# Read the CSV data
|
||||
with open(csv_file_path, mode='r', newline='', encoding='utf-8') as csv_file:
|
||||
csv_reader = csv.DictReader(csv_file)
|
||||
|
||||
for row in csv_reader:
|
||||
cosponsored_by = row.get('cosponsored_by')
|
||||
number = row.get('number')
|
||||
|
||||
if cosponsored_by and number:
|
||||
person_node = session.run(
|
||||
"MATCH (p:Person {bioguideId: $bioguideId}) RETURN p",
|
||||
{"bioguideId": cosponsored_by}
|
||||
).single()
|
||||
|
||||
legislation_node = session.run(
|
||||
"MATCH (l:Legislation {number: $number}) RETURN l",
|
||||
{"number": number}
|
||||
).single()
|
||||
|
||||
if person_node and legislation_node:
|
||||
person = person_node['p']
|
||||
legislation = legislation_node['l']
|
||||
session.run(
|
||||
"MATCH (p:Person), (l:Legislation) "
|
||||
"WHERE id(p) = $person_id AND id(l) = $legislation_id "
|
||||
"CREATE (p)-[:COSPONSORED]->(l)",
|
||||
{"person_id": person.id, "legislation_id": legislation.id}
|
||||
)
|
||||
neo4j_logger.info(f"Created COSPONSORED relationship from Person {person['name']} to Legislation {legislation['title']}")
|
||||
else:
|
||||
if not person_node:
|
||||
neo4j_logger.warning(f"No Person node found for bioguideId: {cosponsored_by}")
|
||||
if not legislation_node:
|
||||
neo4j_logger.warning(f"No Legislation node found for number: {number}")
|
||||
|
||||
return jsonify({"status": "success", "message": "COSPONSORED relationships created successfully"}), 201
|
||||
|
||||
except Exception as e:
|
||||
neo4j_logger.error(f"Error creating COSPONSORED relationships: {e}")
|
||||
return jsonify({"status": "error", "message": str(e)}), 500
|
43
api/endpoints/create_sponsored_relationship.py
Normal file
43
api/endpoints/create_sponsored_relationship.py
Normal file
@ -0,0 +1,43 @@
|
||||
# endpoints/create_sponsored_relationship.py
|
||||
|
||||
from flask import Blueprint, jsonify
|
||||
from neo4j import GraphDatabase
|
||||
import logging
|
||||
from app import get_driver, neo4j_logger # Use absolute import
|
||||
|
||||
bp = Blueprint('create_sponsored_relationship', __name__)
|
||||
|
||||
@bp.route('/create_sponsored_relationship', methods=['GET'])
|
||||
def create_sponsored_relationship():
|
||||
try:
|
||||
driver = get_driver()
|
||||
with driver.session() as session:
|
||||
# Step 3: Implement the endpoint logic
|
||||
legislation_nodes = session.run("MATCH (l:Legislation) RETURN l")
|
||||
for record in legislation_nodes:
|
||||
legislation = record['l']
|
||||
bioguide_id = legislation.get('sponsored_by')
|
||||
|
||||
if bioguide_id:
|
||||
person_node = session.run(
|
||||
"MATCH (p:Person {bioguideId: $bioguideId}) RETURN p",
|
||||
{"bioguideId": bioguide_id}
|
||||
).single()
|
||||
|
||||
if person_node:
|
||||
person = person_node['p']
|
||||
session.run(
|
||||
"MATCH (p:Person), (l:Legislation) "
|
||||
"WHERE id(p) = $person_id AND id(l) = $legislation_id "
|
||||
"CREATE (p)-[:SPONSORED]->(l)",
|
||||
{"person_id": person.id, "legislation_id": legislation.id}
|
||||
)
|
||||
neo4j_logger.info(f"Created SPONSORED relationship from Person {person['name']} to Legislation {legislation['title']}")
|
||||
else:
|
||||
neo4j_logger.warning(f"No Person node found for bioguideId: {bioguide_id}")
|
||||
|
||||
return jsonify({"status": "success", "message": "SPONSORED relationships created successfully"}), 201
|
||||
|
||||
except Exception as e:
|
||||
neo4j_logger.error(f"Error creating SPONSORED relationships: {e}")
|
||||
return jsonify({"status": "error", "message": str(e)}), 500
|
Loading…
Reference in New Issue
Block a user