telexy-vnpt6s/endpoints/login.post.py
2025-03-19 20:34:31 +00:00

47 lines
1.4 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from core.database import fake_users_db
import uuid
from datetime import datetime
router = APIRouter()
@router.post("/vote")
async def vote_handler(
election_id: str,
candidate_id: str,
voter_id: str,
token: str = "dummy_token"
):
"""Cast a vote in an election"""
if voter_id not in fake_users_db:
raise HTTPException(status_code=404, detail="Voter not found")
# Check if user has already voted
if "votes" in fake_users_db[voter_id] and election_id in fake_users_db[voter_id]["votes"]:
raise HTTPException(status_code=400, detail="User has already voted in this election")
vote_id = str(uuid.uuid4())
# Initialize votes if not present
if "votes" not in fake_users_db[voter_id]:
fake_users_db[voter_id]["votes"] = {}
# Record the vote
fake_users_db[voter_id]["votes"][election_id] = {
"vote_id": vote_id,
"candidate_id": candidate_id,
"timestamp": datetime.utcnow().isoformat()
}
return {
"message": "Vote cast successfully",
"data": {
"vote_id": vote_id,
"election_id": election_id,
"timestamp": datetime.utcnow().isoformat()
},
"metadata": {
"voter_id": voter_id,
"status": "confirmed"
}
}