58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
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
|