Skip to content

Commit

Permalink
domain updates/adds
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBoatyMcBoatFace committed Aug 21, 2023
1 parent 3396e77 commit b298f13
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/api/database/postgres/queries/domain/edit_add.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- app/api/database/postgres/queries/domain/edit_add.sql
-- Add new domains
INSERT INTO targets.domains ("domain", active, is_objective)
VALUES (LOWER(:domain), TRUE, TRUE)
RETURNING "domain", id;
7 changes: 7 additions & 0 deletions app/api/database/postgres/queries/domain/edit_update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- app/api/database/postgres/queries/domain/edit_update.sql
-- Updates existing domains
UPDATE targets.domains
SET active = :active,
is_objective = :is_objective
WHERE "domain" = :domain
RETURNING "domain", id, active, is_objective;
14 changes: 14 additions & 0 deletions app/api/database/postgres/queries/domain/list_edit_domains.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- app/api/database/postgres/queries/domain/list_edit_domains.sql
-- Select domains from search before editing
SELECT d."domain",
COUNT(u.url) AS url_count,
d.active,
d.is_objective,
d.is_valid,
date_trunc('day', age(now(), d.created_at)) AS created_ago,
date_trunc('day', age(now(), d.updated_at)) AS updated_ago
FROM targets.domains d
LEFT JOIN targets.urls u ON d.id = u.domain_id
WHERE d."domain" ILIKE '%s'
AND is_valid = TRUE
GROUP BY d."domain", d.active, d.is_objective, d.is_valid, d.created_at, d.updated_at;
2 changes: 2 additions & 0 deletions app/api/database/postgres/queries/domain/update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- app/api/database/postgres/queries/domain/update.sql
-- Update Domain objective status
80 changes: 78 additions & 2 deletions app/api/domain/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy import text
from app.api.database.postgres.connect import postgres_conn
from ..utils import format_output

from app.logging import logger

# Blueprint Configuration
domain_bp = Blueprint(
Expand All @@ -13,7 +13,6 @@
url_prefix='/domain'
)


@domain_bp.route('/summary', methods=['GET'])
def domain_summary():
raw_domain = request.args.get('domain', 'nasa.gov')
Expand Down Expand Up @@ -59,5 +58,82 @@ def domain_list():
results.append({key: value for key,value in zip(keys,row)})
output_format = request.args.get('format', 'json')
return format_output(results, output_format, 'domain_summary')
except Exception as e:
return jsonify({"error": str(e)}), 500



@domain_bp.route('/list-update', methods=['GET'])
def domain_update_list():
domain = request.args.get('domain', '%')
sql_file = "app/api/database/postgres/queries/domain/list_edit_domains.sql"

# Read sql file
with open(sql_file) as file:
sql_content = text(file.read())

results = []
try:
with postgres_conn.begin() as connection:
result_proxy = connection.execute(sql_content.params(domain=domain)) # use params method
keys = result_proxy.keys()
for row in result_proxy:
results.append({key: value for key,value in zip(keys,row)})
output_format = request.args.get('format', 'json')
return format_output(results, output_format, 'domain_summary')
except Exception as e:
return jsonify({"error": str(e)}), 500


@domain_bp.route('/update', methods=['POST'])
def domain_update():
data = request.get_json()
sql_file = "app/api/database/postgres/queries/domain/edit_update.sql"

domain = data.get('domain')
active = data.get('active')
is_objective = data.get('is_objective')

if not domain or active is None or is_objective is None:
return jsonify({"error": "Invalid input data"}), 400

# Read sql file
with open(sql_file) as file:
sql_content = text(file.read())

results = []
try:
with postgres_conn.begin() as connection:
result_proxy = connection.execute(sql_content.params(domain=domain, active=active, is_objective=is_objective)) # use params method
keys = result_proxy.keys()
for row in result_proxy:
results.append({key: value for key,value in zip(keys,row)})
output_format = request.args.get('format', 'json')
return format_output(results, output_format, 'domain_summary')
except Exception as e:
return jsonify({"error": str(e)}), 500

@domain_bp.route('/add', methods=['POST'])
def domain_add():
data = request.get_json()
sql_file = "app/api/database/postgres/queries/domain/edit_add.sql"

domain = data.get('domain')
if not domain:
return jsonify({"error": "Invalid input data"}), 400

# Read sql file
with open(sql_file) as file:
sql_content = text(file.read())

results = []
try:
with postgres_conn.begin() as connection:
result_proxy = connection.execute(sql_content.params(domain=domain)) # use params method
keys = result_proxy.keys()
for row in result_proxy:
results.append({key: value for key,value in zip(keys,row)})
output_format = request.args.get('format', 'json')
return format_output(results, output_format, 'domain_summary')
except Exception as e:
return jsonify({"error": str(e)}), 500

0 comments on commit b298f13

Please sign in to comment.