junobuild_cdn/proposals/state/
stable.rs1use crate::proposals::state::filter::filter_proposals_range;
2use crate::proposals::{ListProposalResults, Proposal, ProposalId, ProposalKey, ProposalsStable};
3use crate::strategies::CdnStableStrategy;
4use junobuild_shared::structures::collect_stable_vec;
5
6pub fn get_proposal(
7 cdn_stable: &impl CdnStableStrategy,
8 proposal_id: &ProposalId,
9) -> Option<Proposal> {
10 cdn_stable.with_proposals(|proposals| get_proposal_impl(proposal_id, proposals))
11}
12
13fn get_proposal_impl(proposal_id: &ProposalId, proposals: &ProposalsStable) -> Option<Proposal> {
14 proposals.get(&proposal_key(proposal_id))
15}
16
17pub fn list_proposals(
18 cdn_stable: &impl CdnStableStrategy,
19 filters_key: &(u128, u128),
20) -> ListProposalResults {
21 cdn_stable.with_proposals(|proposals| list_proposals_impl(filters_key, proposals))
22}
23
24fn list_proposals_impl(
25 filters_key: &(u128, u128),
26 proposals: &ProposalsStable,
27) -> ListProposalResults {
28 let items: Vec<(ProposalKey, Proposal)> =
29 collect_stable_vec(proposals.range(filter_proposals_range(filters_key)));
30
31 let items_length = items.len();
32
33 ListProposalResults {
34 items,
35 items_length,
36 matches_length: items_length,
37 }
38}
39
40pub fn count_proposals(cdn_stable: &impl CdnStableStrategy) -> usize {
41 cdn_stable.with_proposals(count_proposals_impl)
42}
43
44fn count_proposals_impl(proposals: &ProposalsStable) -> usize {
45 proposals.iter().count()
46}
47
48pub fn insert_proposal(
49 cdn_stable: &impl CdnStableStrategy,
50 proposal_id: &ProposalId,
51 proposal: &Proposal,
52) {
53 cdn_stable
54 .with_proposals_mut(|proposals| insert_proposal_impl(proposal_id, proposal, proposals))
55}
56
57fn insert_proposal_impl(
58 proposal_id: &ProposalId,
59 proposal: &Proposal,
60 proposals: &mut ProposalsStable,
61) {
62 proposals.insert(proposal_key(proposal_id), proposal.clone());
63}
64
65fn proposal_key(proposal_id: &ProposalId) -> ProposalKey {
66 ProposalKey {
67 proposal_id: *proposal_id,
68 }
69}