From 9c71c63ec5bdb09be928b9805315178457dca6f1 Mon Sep 17 00:00:00 2001 From: Mahmoud-Emad Date: Wed, 21 May 2025 12:21:38 +0300 Subject: [PATCH] feat: Working on the propsal page: - Integerated the view proposal detail db call - Use real data instead of mock data --- actix_mvc_app/src/controllers/governance.rs | 6 +- actix_mvc_app/src/db/proposals.rs | 15 ++ .../src/views/governance/my_votes.html | 243 +++++++++--------- .../src/views/governance/proposal_detail.html | 2 +- 4 files changed, 142 insertions(+), 124 deletions(-) diff --git a/actix_mvc_app/src/controllers/governance.rs b/actix_mvc_app/src/controllers/governance.rs index 7eaa772..e037aff 100644 --- a/actix_mvc_app/src/controllers/governance.rs +++ b/actix_mvc_app/src/controllers/governance.rs @@ -1,4 +1,4 @@ -use crate::db::proposals; +use crate::db::proposals::{self, get_proposal_by_id}; use crate::models::governance::{Vote, VoteType, VotingResults}; use crate::utils::render_template; use actix_session::Session; @@ -126,8 +126,8 @@ impl GovernanceController { } // Get mock proposal detail - let proposal = Self::get_mock_proposal_by_id(&proposal_id); - if let Some(proposal) = proposal { + let proposal = get_proposal_by_id(proposal_id.parse().unwrap()); + if let Ok(Some(proposal)) = proposal { ctx.insert("proposal", &proposal); // Get mock votes for this proposal diff --git a/actix_mvc_app/src/db/proposals.rs b/actix_mvc_app/src/db/proposals.rs index c2214ed..425d530 100644 --- a/actix_mvc_app/src/db/proposals.rs +++ b/actix_mvc_app/src/db/proposals.rs @@ -75,3 +75,18 @@ pub fn get_proposals() -> Result, String> { }; Ok(proposals) } + +/// Fetches a single proposal by its ID from the database. +pub fn get_proposal_by_id(proposal_id: u32) -> Result, String> { + let db = get_db(DB_PATH).map_err(|e| format!("DB error: {}", e))?; + let collection = db + .collection::() + .map_err(|e| format!("Collection error: {:?}", e))?; + match collection.get_by_id(proposal_id) { + Ok(proposal) => Ok(Some(proposal.expect("proposal not found"))), + Err(e) => { + eprintln!("Error fetching proposal by id {}: {:?}", proposal_id, e); + Err(format!("Failed to fetch proposal: {:?}", e)) + } + } +} diff --git a/actix_mvc_app/src/views/governance/my_votes.html b/actix_mvc_app/src/views/governance/my_votes.html index 626d250..9a1085d 100644 --- a/actix_mvc_app/src/views/governance/my_votes.html +++ b/actix_mvc_app/src/views/governance/my_votes.html @@ -3,133 +3,136 @@ {% block title %}My Votes - Governance Dashboard{% endblock %} {% block content %} - -
-
- + +
+
+ +
+
+ + +
+
+
+
+
My Voting History
+
+
+ {% if votes | length > 0 %} +
+ + + + + + + + + + + + {% for item in votes %}{% set vote = item.0 %}{% set proposal = item.1 %} + + + + + + + + {% endfor %} + +
ProposalMy VoteStatusVoted OnActions
{{ proposal.title }} + + {{ vote.vote_type }} + + + + {{ proposal.status }} + + {{ vote.created_at | date(format="%Y-%m-%d") }} + View Proposal +
+
+ {% else %} +
+ +
You haven't voted on any proposals yet
+

When you vote on proposals, they will appear here.

+ Browse Proposals +
+ {% endif %} +
+
- -
-
-
-
-
My Voting History
-
-
- {% if votes | length > 0 %} -
- - - - - - - - - - - - {% for item in votes %}{% set vote = item.0 %}{% set proposal = item.1 %} - - - - - - - - {% endfor %} - -
ProposalMy VoteStatusVoted OnActions
{{ proposal.title }} - - {{ vote.vote_type }} - - - - {{ proposal.status }} - - {{ vote.created_at | date(format="%Y-%m-%d") }} - View Proposal -
-
- {% else %} -
- -
You haven't voted on any proposals yet
-

When you vote on proposals, they will appear here.

- Browse Proposals -
+ +{% if votes | length > 0 %} +
+
+
+
+
Yes Votes
+

+ {% set yes_count = 0 %} + {% for item in votes %}{% set vote = item.0 %}{% set proposal = item.1 %} + {% if vote.vote_type == 'Yes' %} + {% set yes_count = yes_count + 1 %} {% endif %} -

+ {% endfor %} + {{ yes_count }} +

- - - {% if votes | length > 0 %} -
-
-
-
-
Yes Votes
-

- {% set yes_count = 0 %} - {% for item in votes %}{% set vote = item.0 %}{% set proposal = item.1 %} - {% if vote.vote_type == 'Yes' %} - {% set yes_count = yes_count + 1 %} - {% endif %} - {% endfor %} - {{ yes_count }} -

-
-
-
-
-
-
-
No Votes
-

- {% set no_count = 0 %} - {% for item in votes %}{% set vote = item.0 %}{% set proposal = item.1 %} - {% if vote.vote_type == 'No' %} - {% set no_count = no_count + 1 %} - {% endif %} - {% endfor %} - {{ no_count }} -

-
-
-
-
-
-
-
Abstain Votes
-

- {% set abstain_count = 0 %} - {% for item in votes %}{% set vote = item.0 %}{% set proposal = item.1 %} - {% if vote.vote_type == 'Abstain' %} - {% set abstain_count = abstain_count + 1 %} - {% endif %} - {% endfor %} - {{ abstain_count }} -

-
+
+
+
+
No Votes
+

+ {% set no_count = 0 %} + {% for item in votes %}{% set vote = item.0 %}{% set proposal = item.1 %} + {% if vote.vote_type == 'No' %} + {% set no_count = no_count + 1 %} + {% endif %} + {% endfor %} + {{ no_count }} +

- {% endif %} -{% endblock %} +
+
+
+
Abstain Votes
+

+ {% set abstain_count = 0 %} + {% for item in votes %}{% set vote = item.0 %}{% set proposal = item.1 %} + {% if vote.vote_type == 'Abstain' %} + {% set abstain_count = abstain_count + 1 %} + {% endif %} + {% endfor %} + {{ abstain_count }} +

+
+
+
+
+{% endif %} +{% endblock %} \ No newline at end of file diff --git a/actix_mvc_app/src/views/governance/proposal_detail.html b/actix_mvc_app/src/views/governance/proposal_detail.html index b6e99ae..98cd542 100644 --- a/actix_mvc_app/src/views/governance/proposal_detail.html +++ b/actix_mvc_app/src/views/governance/proposal_detail.html @@ -107,7 +107,7 @@
Cast Your Vote
-
+