refactor: establish standalone application boundary

This commit is contained in:
leefer
2026-08-03 21:42:25 +08:00
parent cc5fb8d73e
commit e1e76cd51e
324 changed files with 63090 additions and 44743 deletions
+57
View File
@@ -0,0 +1,57 @@
from __future__ import annotations
import json
from datetime import date
from http import HTTPStatus
from urllib.parse import parse_qs
class MentorRoutesMixin:
def _handle_mentor_get(self, parsed) -> bool:
if parsed.path == "/api/mentors/setup":
query = parse_qs(parsed.query)
trade_date = query.get("trade_date", [date.today().isoformat()])[0]
try:
self.send_json(self.application_service.mentor_setup(trade_date))
except ValueError as exc:
self.send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
return True
if parsed.path == "/api/mentors/messages":
query = parse_qs(parsed.query)
try:
self.send_json(
{
"items": self.application_service.mentor_messages(
query.get("mentor_id", [""])[0],
query.get("trade_date", [date.today().isoformat()])[0],
)
}
)
except ValueError as exc:
self.send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
return True
return False
def _handle_mentor_post(self, parsed) -> bool:
if parsed.path == "/api/mentors/preferences":
try:
result = self.application_service.save_mentor_preferences(self.read_json_body())
self.send_json({"ok": True, **result})
except (ValueError, json.JSONDecodeError) as exc:
self.send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
return True
return False
def _handle_mentor_delete(self, parsed) -> bool:
if parsed.path == "/api/mentors/messages":
query = parse_qs(parsed.query)
try:
deleted = self.application_service.clear_mentor_messages(
query.get("mentor_id", [""])[0],
query.get("trade_date", [date.today().isoformat()])[0],
)
self.send_json({"ok": True, "deleted": deleted})
except ValueError as exc:
self.send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
return True
return False