feat: Implemented submit vote

This commit is contained in:
Mahmoud-Emad
2025-05-21 13:49:20 +03:00
parent 9c71c63ec5
commit 5d9eaac1f8
3 changed files with 172 additions and 44 deletions

View File

@@ -134,8 +134,8 @@ impl GovernanceController {
let votes = Self::get_mock_votes_for_proposal(&proposal_id);
ctx.insert("votes", &votes);
// Get voting results
let results = Self::get_mock_voting_results(&proposal_id);
// Calculate voting results directly from the proposal
let results = Self::calculate_voting_results_from_proposal(&proposal);
ctx.insert("results", &results);
render_template(&tmpl, "governance/proposal_detail.html", &ctx)
@@ -264,57 +264,62 @@ impl GovernanceController {
/// Handles the submission of a vote on a proposal
pub async fn submit_vote(
path: web::Path<String>,
_form: web::Form<VoteForm>,
form: web::Form<VoteForm>,
tmpl: web::Data<Tera>,
session: Session,
) -> Result<impl Responder> {
let proposal_id = path.into_inner();
// Check if user is logged in
if Self::get_user_from_session(&session).is_none() {
return Ok(HttpResponse::Found()
.append_header(("Location", "/login"))
.finish());
}
let mut ctx = tera::Context::new();
ctx.insert("active_page", "governance");
// Add user to context if available
if let Some(user) = Self::get_user_from_session(&session) {
ctx.insert("user", &user);
}
// Check if user is logged in
let user = match Self::get_user_from_session(&session) {
Some(user) => user,
None => {
return Ok(HttpResponse::Found()
.append_header(("Location", "/login"))
.finish());
}
};
ctx.insert("user", &user);
// Get mock proposal detail
let proposal = Self::get_mock_proposal_by_id(&proposal_id);
if let Some(proposal) = proposal {
ctx.insert("proposal", &proposal);
ctx.insert("success", "Your vote has been recorded!");
// Extract user ID
let user_id = user.get("id").and_then(|v| v.as_i64()).unwrap_or(1) as i32;
// Get mock votes for this proposal
let votes = Self::get_mock_votes_for_proposal(&proposal_id);
ctx.insert("votes", &votes);
// Parse proposal ID
let proposal_id_u32 = match proposal_id.parse::<u32>() {
Ok(id) => id,
Err(_) => {
ctx.insert("error", "Invalid proposal ID");
return render_template(&tmpl, "error.html", &ctx);
}
};
// Get voting results
let results = Self::get_mock_voting_results(&proposal_id);
ctx.insert("results", &results);
// Submit the vote
match crate::db::proposals::submit_vote_on_proposal(
proposal_id_u32,
user_id,
&form.vote_type,
1, // Default to 1 share
) {
Ok(updated_proposal) => {
ctx.insert("proposal", &updated_proposal);
ctx.insert("success", "Your vote has been recorded!");
render_template(&tmpl, "governance/proposal_detail.html", &ctx)
} else {
// Proposal not found
ctx.insert("error", "Proposal not found");
// For the error page, we'll use a special case to set the status code to 404
match tmpl.render("error.html", &ctx) {
Ok(content) => Ok(HttpResponse::NotFound()
.content_type("text/html")
.body(content)),
Err(e) => {
eprintln!("Error rendering error template: {}", e);
Err(actix_web::error::ErrorInternalServerError(format!(
"Error: {}",
e
)))
}
// Get votes for this proposal
// For now, we'll still use mock votes until we implement a function to extract votes from the proposal
let votes = Self::get_mock_votes_for_proposal(&proposal_id);
ctx.insert("votes", &votes);
// Calculate voting results directly from the updated proposal
let results = Self::calculate_voting_results_from_proposal(&updated_proposal);
ctx.insert("results", &results);
render_template(&tmpl, "governance/proposal_detail.html", &ctx)
}
Err(e) => {
ctx.insert("error", &format!("Failed to submit vote: {}", e));
render_template(&tmpl, "error.html", &ctx)
}
}
}
@@ -614,6 +619,26 @@ impl GovernanceController {
results
}
/// Calculate voting results from a proposal
fn calculate_voting_results_from_proposal(proposal: &Proposal) -> VotingResults {
let mut results = VotingResults::new(proposal.base_data.id.to_string());
// Count votes for each option
for option in &proposal.options {
match option.id {
1 => results.yes_count = option.count as usize,
2 => results.no_count = option.count as usize,
3 => results.abstain_count = option.count as usize,
_ => {} // Ignore other options
}
}
// Calculate total votes
results.total_votes = results.yes_count + results.no_count + results.abstain_count;
results
}
/// Generate mock statistics for the governance dashboard
fn get_mock_statistics() -> GovernanceStats {
GovernanceStats {