feat: implement backend API clients for multi-marketplace integration and frontend service layer
This commit is contained in:
@@ -12,24 +12,26 @@ cases_blueprint = Blueprint('cases', __name__)
|
||||
def serialize_case(row):
|
||||
return {
|
||||
"id": row[0],
|
||||
"violator_url": row[1],
|
||||
"violator_article": row[2],
|
||||
"my_article": row[3],
|
||||
"comment": row[4],
|
||||
"status": row[5],
|
||||
"created_at": row[6].isoformat() if row[6] else None,
|
||||
"updated_at": row[7].isoformat() if row[7] else None,
|
||||
"platform": row[1],
|
||||
"violator_url": row[2],
|
||||
"violator_article": row[3],
|
||||
"my_article": row[4],
|
||||
"comment": row[5],
|
||||
"status": row[6],
|
||||
"created_at": row[7].isoformat() if row[7] else None,
|
||||
"updated_at": row[8].isoformat() if row[8] else None,
|
||||
}
|
||||
|
||||
# ---------- CASE CRUD ----------
|
||||
@cases_blueprint.route('/cases', methods=['GET'])
|
||||
def list_cases():
|
||||
# optional filters: ?article=...&status=...
|
||||
# optional filters: ?article=...&status=...&platform=...
|
||||
article = request.args.get('article')
|
||||
status = request.args.get('status')
|
||||
platform = request.args.get('platform')
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
query = "SELECT * FROM cases"
|
||||
query = "SELECT id, platform, violator_url, violator_article, my_article, comment, status, created_at, updated_at FROM cases"
|
||||
params = []
|
||||
conditions = []
|
||||
if article:
|
||||
@@ -38,8 +40,12 @@ def list_cases():
|
||||
if status:
|
||||
conditions.append("status = %s")
|
||||
params.append(status)
|
||||
if platform:
|
||||
conditions.append("platform = %s")
|
||||
params.append(platform)
|
||||
if conditions:
|
||||
query += " WHERE " + " AND ".join(conditions)
|
||||
query += " ORDER BY id DESC"
|
||||
cur.execute(query, params)
|
||||
rows = cur.fetchall()
|
||||
release_db_connection(conn)
|
||||
@@ -48,16 +54,17 @@ def list_cases():
|
||||
@cases_blueprint.route('/cases', methods=['POST'])
|
||||
def create_case():
|
||||
data = request.json
|
||||
required = ["violator_url", "violator_article"]
|
||||
required = ["platform", "violator_url", "violator_article"]
|
||||
for f in required:
|
||||
if f not in data:
|
||||
return jsonify({"error": f"Missing field {f}"}), 400
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"""INSERT INTO cases (violator_url, violator_article, my_article, comment, status)
|
||||
VALUES (%s, %s, %s, %s, %s) RETURNING id""",
|
||||
"""INSERT INTO cases (platform, violator_url, violator_article, my_article, comment, status)
|
||||
VALUES (%s, %s, %s, %s, %s, %s) RETURNING id""",
|
||||
(
|
||||
data.get('platform'),
|
||||
data.get('violator_url'),
|
||||
data.get('violator_article'),
|
||||
data.get('my_article'),
|
||||
@@ -74,7 +81,7 @@ def create_case():
|
||||
def get_case(case_id):
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT * FROM cases WHERE id = %s", (case_id,))
|
||||
cur.execute("SELECT id, platform, violator_url, violator_article, my_article, comment, status, created_at, updated_at FROM cases WHERE id = %s", (case_id,))
|
||||
row = cur.fetchone()
|
||||
release_db_connection(conn)
|
||||
if not row:
|
||||
@@ -86,7 +93,7 @@ def update_case(case_id):
|
||||
data = request.json
|
||||
fields = []
|
||||
params = []
|
||||
allowed = ["violator_url", "violator_article", "my_article", "comment", "status"]
|
||||
allowed = ["platform", "violator_url", "violator_article", "my_article", "comment", "status"]
|
||||
for f in allowed:
|
||||
if f in data:
|
||||
fields.append(f"{f} = %s")
|
||||
|
||||
59
backend/routes/credentials.py
Normal file
59
backend/routes/credentials.py
Normal file
@@ -0,0 +1,59 @@
|
||||
# backend/routes/credentials.py
|
||||
from flask import Blueprint, request, jsonify
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
from ..db import get_db_connection, release_db_connection
|
||||
|
||||
credentials_blueprint = Blueprint('credentials', __name__)
|
||||
|
||||
@credentials_blueprint.route('/credentials', methods=['GET'])
|
||||
def list_credentials():
|
||||
"""Return configured platforms (without exposing secrets)."""
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT platform FROM credentials")
|
||||
rows = cur.fetchall()
|
||||
release_db_connection(conn)
|
||||
|
||||
platforms = [r[0] for r in rows]
|
||||
return jsonify({"configured_platforms": platforms})
|
||||
|
||||
@credentials_blueprint.route('/credentials/<string:platform>', methods=['POST'])
|
||||
def save_credentials(platform):
|
||||
"""Save or update credentials for a specific platform."""
|
||||
data = request.json
|
||||
api_key = data.get('api_key')
|
||||
client_id = data.get('client_id')
|
||||
client_secret = data.get('client_secret')
|
||||
other_data = data.get('other_data', {})
|
||||
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute(
|
||||
"""INSERT INTO credentials (platform, api_key, client_id, client_secret, other_data, updated_at)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
ON CONFLICT (platform) DO UPDATE SET
|
||||
api_key = EXCLUDED.api_key,
|
||||
client_id = EXCLUDED.client_id,
|
||||
client_secret = EXCLUDED.client_secret,
|
||||
other_data = EXCLUDED.other_data,
|
||||
updated_at = EXCLUDED.updated_at""",
|
||||
(platform, api_key, client_id, client_secret, json.dumps(other_data), datetime.utcnow())
|
||||
)
|
||||
conn.commit()
|
||||
release_db_connection(conn)
|
||||
|
||||
return jsonify({"message": f"Credentials for {platform} saved successfully."}), 200
|
||||
|
||||
@credentials_blueprint.route('/credentials/<string:platform>', methods=['DELETE'])
|
||||
def delete_credentials(platform):
|
||||
"""Delete credentials for a specific platform."""
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute("DELETE FROM credentials WHERE platform = %s", (platform,))
|
||||
conn.commit()
|
||||
release_db_connection(conn)
|
||||
|
||||
return jsonify({"message": f"Credentials for {platform} deleted."}), 200
|
||||
Reference in New Issue
Block a user