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")
|
||||
|
||||
Reference in New Issue
Block a user