Update code in endpoints/login.post.py
This commit is contained in:
parent
5b169865c6
commit
3038a403fe
@ -1,25 +1,47 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from core.auth import get_current_user_dummy
|
|
||||||
from core.database import fake_users_db
|
from core.database import fake_users_db
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/login")
|
@router.post("/vote")
|
||||||
async def login_demo(
|
async def vote_handler(
|
||||||
username: str = "demo",
|
election_id: str,
|
||||||
password: str = "password"
|
candidate_id: str,
|
||||||
|
voter_id: str,
|
||||||
|
token: str = "dummy_token"
|
||||||
):
|
):
|
||||||
"""Demo login endpoint"""
|
"""Cast a vote in an election"""
|
||||||
user = fake_users_db.get(username)
|
if voter_id not in fake_users_db:
|
||||||
if not user or user["password"] != password:
|
raise HTTPException(status_code=404, detail="Voter not found")
|
||||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
|
||||||
|
# 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 {
|
return {
|
||||||
"message": "Login successful (demo)",
|
"message": "Vote cast successfully",
|
||||||
"user": username,
|
"data": {
|
||||||
"token": "dummy_jwt_token_123",
|
"vote_id": vote_id,
|
||||||
"features": {
|
"election_id": election_id,
|
||||||
"rate_limit": 100,
|
"timestamp": datetime.utcnow().isoformat()
|
||||||
"expires_in": 3600
|
},
|
||||||
|
"metadata": {
|
||||||
|
"voter_id": voter_id,
|
||||||
|
"status": "confirmed"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user