The backend component to interface with the smart contract.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 2 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import pika
  2. from fastapi import APIRouter
  3. from datetime import datetime
  4. from app.core.config import settings
  5. from app.contracts.issue import IssueContract
  6. from app.crud.user import get_user
  7. from app.db.session import get_db_session
  8. from app.schemas.issue import IssuePost, AddIssueRequest, VoteIssueRequest, AddIssueResponse
  9. router = APIRouter()
  10. @router.get("/list", response_model=list[IssuePost])
  11. async def list_issues():
  12. smart_contract = IssueContract(
  13. settings.CONTRACT_ID,
  14. settings.WALLET_ID
  15. )
  16. issue_list = await smart_contract.list_issues()
  17. return issue_list
  18. @router.get("/paragraphs")
  19. async def get_paragraphs(issue_id: str):
  20. smart_contract = IssueContract(
  21. settings.CONTRACT_ID,
  22. settings.WALLET_ID
  23. )
  24. paragraphs = await smart_contract.get_paragraphs(bytes.fromhex(issue_id))
  25. return paragraphs
  26. @router.post("/create", response_model=AddIssueResponse)
  27. async def create_issue(request: AddIssueRequest):
  28. smart_contract = IssueContract(
  29. settings.CONTRACT_ID,
  30. settings.WALLET_ID
  31. )
  32. async for session in get_db_session():
  33. user = await get_user(session, request.session_id)
  34. time_since_auth = datetime.now() - datetime.fromtimestamp(user.auth_date)
  35. if user and time_since_auth.total_seconds() < settings.AUTH_TIMEOUT:
  36. await smart_contract.add_issue(request.title, request.paragraphs, user.username)
  37. return {'result': True}
  38. else:
  39. return {'result': False}
  40. return {'result': False}
  41. @router.post("/vote")
  42. async def vote_issue(request: VoteIssueRequest):
  43. smart_contract = IssueContract(
  44. settings.CONTRACT_ID,
  45. settings.WALLET_ID
  46. )
  47. if request.increase and not request.decrease:
  48. await smart_contract.increase_vote(bytes.fromhex(request.issue_id))
  49. elif request.decrease and not request.increase:
  50. await smart_contract.decrease_vote(bytes.fromhex(request.issue_id))
  51. return