feat: Working on the propsal page:

- Integerated the view proposal detail db call
- Use real data instead of mock data
This commit is contained in:
Mahmoud-Emad 2025-05-21 12:21:38 +03:00
parent 4a2f1c7282
commit 9c71c63ec5
4 changed files with 142 additions and 124 deletions

View File

@ -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

View File

@ -75,3 +75,18 @@ pub fn get_proposals() -> Result<Vec<Proposal>, String> {
};
Ok(proposals)
}
/// Fetches a single proposal by its ID from the database.
pub fn get_proposal_by_id(proposal_id: u32) -> Result<Option<Proposal>, String> {
let db = get_db(DB_PATH).map_err(|e| format!("DB error: {}", e))?;
let collection = db
.collection::<Proposal>()
.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))
}
}
}

View File

@ -48,18 +48,21 @@
<tr>
<td>{{ proposal.title }}</td>
<td>
<span class="badge {% if vote.vote_type == 'Yes' %}bg-success{% elif vote.vote_type == 'No' %}bg-danger{% else %}bg-secondary{% endif %}">
<span
class="badge {% if vote.vote_type == 'Yes' %}bg-success{% elif vote.vote_type == 'No' %}bg-danger{% else %}bg-secondary{% endif %}">
{{ vote.vote_type }}
</span>
</td>
<td>
<span class="badge {% if proposal.status == 'Active' %}bg-success{% elif proposal.status == 'Approved' %}bg-primary{% elif proposal.status == 'Rejected' %}bg-danger{% elif proposal.status == 'Draft' %}bg-secondary{% else %}bg-warning{% endif %}">
<span
class="badge {% if proposal.status == 'Active' %}bg-success{% elif proposal.status == 'Approved' %}bg-primary{% elif proposal.status == 'Rejected' %}bg-danger{% elif proposal.status == 'Draft' %}bg-secondary{% else %}bg-warning{% endif %}">
{{ proposal.status }}
</span>
</td>
<td>{{ vote.created_at | date(format="%Y-%m-%d") }}</td>
<td>
<a href="/governance/proposals/{{ proposal.id }}" class="btn btn-sm btn-primary">View Proposal</a>
<a href="/governance/proposals/{{ proposal.base_data.id }}"
class="btn btn-sm btn-primary">View Proposal</a>
</td>
</tr>
{% endfor %}

View File

@ -107,7 +107,7 @@
<h5 class="mb-0">Cast Your Vote</h5>
</div>
<div class="card-body">
<form action="/governance/proposals/{{ proposal.id }}/vote" method="post">
<form action="/governance/proposals/{{ proposal.base_data.id }}/vote" method="post">
<div class="mb-3">
<label class="form-label">Vote Type</label>
<div class="form-check">