import pika from fastapi import APIRouter from datetime import datetime from app.core.config import settings from app.contracts.issue import IssueContract from app.crud.user import get_user from app.db.session import get_db_session from app.schemas.issue import IssuePost, AddIssueRequest, VoteIssueRequest, AddIssueResponse router = APIRouter() @router.get("/list", response_model=list[IssuePost]) async def list_issues(): smart_contract = IssueContract( settings.CONTRACT_ID, settings.WALLET_ID ) issue_list = await smart_contract.list_issues() return issue_list @router.get("/paragraphs") async def get_paragraphs(issue_id: str): smart_contract = IssueContract( settings.CONTRACT_ID, settings.WALLET_ID ) paragraphs = await smart_contract.get_paragraphs(bytes.fromhex(issue_id)) return paragraphs @router.post("/create", response_model=AddIssueResponse) async def create_issue(request: AddIssueRequest): smart_contract = IssueContract( settings.CONTRACT_ID, settings.WALLET_ID ) async for session in get_db_session(): user = await get_user(session, request.session_id) time_since_auth = datetime.now() - datetime.fromtimestamp(user.auth_date) if user and time_since_auth.total_seconds() < settings.AUTH_TIMEOUT: await smart_contract.add_issue(request.title, request.paragraphs, user.username) return {'result': True} else: return {'result': False} return {'result': False} @router.post("/vote") async def vote_issue(request: VoteIssueRequest): smart_contract = IssueContract( settings.CONTRACT_ID, settings.WALLET_ID ) if request.increase and not request.decrease: await smart_contract.increase_vote(bytes.fromhex(request.issue_id)) elif request.decrease and not request.increase: await smart_contract.decrease_vote(bytes.fromhex(request.issue_id)) return