In the previous video, we completed the state setup and implemented our TraitLocal for managing the application state. In this video, we begin defining the core logic by implementing our first query and update functions using the Internet Computer's ic-cdk.
This function retrieves a proposal by its unique u64 key.
#[query]
fn get_proposal(key: u64) -> Option<Proposal> {
PROPOSAL_MAP.with(|p| p.borrow().get(&key).cloned())
}
Returns the total number of proposals stored.
#[query]
fn get_proposal_count() -> u64 {
PROPOSAL_MAP.with(|p| p.borrow().len() as u64)
}
This function adds a new proposal entry to the map.
#[update]
fn create_proposal(key: u64, proposal: CreateProposal) -> Option<Proposal> {
let value = Proposal {
description: proposal.description,
approve: 0,
reject: 0,
pass: 0,
is_active: proposal.is_active,
voted: Vec::new(),
owner: ic_cdk::caller(),
};
PROPOSAL_MAP.with(|p| {
p.borrow_mut().insert(key, value)
})
}
In this session, we:
Swap insights and ask questions about “Build on Internet Computer with ICP Rust CDK”.
Ask a question or share your thoughts about this lesson.