Advertisement
Guest User

Untitled

a guest
Sep 7th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.06 KB | None | 0 0
  1. #[macro_use]
  2. extern crate hdk;
  3. #[macro_use]
  4. extern crate serde_derive;
  5. #[macro_use]
  6. extern crate holochain_json_derive;
  7.  
  8. use hdk::{
  9.     error::ZomeApiResult,
  10.     holochain_persistence_api::{
  11.         cas::content::Address,
  12.         hash::HashString,
  13.     },
  14.     holochain_json_api::{
  15.         json::JsonString,
  16.         error::JsonError,
  17.     },
  18.     holochain_core_types::{
  19.         dna::entry_types::Sharing,
  20.         entry::Entry,
  21.         link::LinkMatch,
  22.     }
  23. };
  24.  
  25.  
  26. define_zome! {
  27.     entries: [
  28.         entry!(
  29.             name: "list",
  30.             description: "",
  31.             sharing: Sharing::Public,
  32.             validation_package: || hdk::ValidationPackageDefinition::Entry,
  33.             validation: |validation_data: hdk::EntryValidationData<List>| {
  34.                 Ok(())
  35.             },
  36.             links: [
  37.                 to!(
  38.                     "listItem",
  39.                     link_type: "items",
  40.                     validation_package: || hdk::ValidationPackageDefinition::Entry,
  41.                     validation: |_validation_data: hdk::LinkValidationData| {
  42.                         Ok(())
  43.                     }
  44.                 )
  45.             ]
  46.         ),
  47.         entry!(
  48.             name: "listItem",
  49.             description: "",
  50.             sharing: Sharing::Public,
  51.             validation_package: || hdk::ValidationPackageDefinition::Entry,
  52.             validation: |validation_data: hdk::EntryValidationData<ListItem>| {
  53.                 Ok(())
  54.             }
  55.         )
  56.     ]
  57.  
  58.     init: || {
  59.         Ok(())
  60.     }
  61.  
  62.     validate_agent: |validation_data : EntryValidationData::<AgentId>| {
  63.         Ok(())
  64.     }
  65.  
  66.     functions: [
  67.         create_list: {
  68.             inputs: |list: List|,
  69.             outputs: |result: ZomeApiResult<Address>|,
  70.             handler: handle_create_list
  71.         }
  72.         add_item: {
  73.             inputs: |list_item: ListItem, list_addr: HashString|,
  74.             outputs: |result: ZomeApiResult<Address>|,
  75.             handler: handle_add_item
  76.         }
  77.         get_list: {
  78.             inputs: |list_addr: HashString|,
  79.             outputs: |result: ZomeApiResult<GetListResponse>|,
  80.             handler: handle_get_list
  81.         }
  82.     ]
  83.     traits: {
  84.         hc_public [create_list, add_item, get_list]
  85.     }
  86. }
  87.  
  88.  
  89. #[derive(Serialize, Deserialize, Debug, Clone, DefaultJson)]
  90. struct List {
  91.     name: String
  92. }
  93.  
  94. #[derive(Serialize, Deserialize, Debug, Clone, DefaultJson)]
  95. struct ListItem {
  96.     text: String,
  97.     completed: bool
  98. }
  99.  
  100. #[derive(Serialize, Deserialize, Debug, DefaultJson)]
  101. struct GetListResponse {
  102.     name: String,
  103.     items: Vec<ListItem>
  104. }
  105.  
  106. fn handle_create_list(list: List) -> ZomeApiResult<Address> {
  107.     // define the entry
  108.     let list_entry = Entry::App(
  109.         "list".into(),
  110.         list.into()
  111.     );
  112.  
  113.     // commit the entry and return the address
  114.     hdk::commit_entry(&list_entry)
  115. }
  116.  
  117.  
  118. fn handle_add_item(list_item: ListItem, list_addr: HashString) -> ZomeApiResult<Address> {
  119.     // define the entry
  120.     let list_item_entry = Entry::App(
  121.         "listItem".into(),
  122.         list_item.into()
  123.     );
  124.  
  125.     let item_addr = hdk::commit_entry(&list_item_entry)?; // commit the list item
  126.     hdk::link_entries(&list_addr, &item_addr, "items", "")?; // if successful, link to list address
  127.     Ok(item_addr)
  128. }
  129.  
  130.  
  131. fn handle_get_list(list_addr: HashString) -> ZomeApiResult<GetListResponse> {
  132.  
  133.     // load the list entry. Early return error if it cannot load or is wrong type
  134.     let list = hdk::utils::get_as_type::<List>(list_addr.clone())?;
  135.  
  136.     // try and load the list items, filter out errors and collect in a vector
  137.     let list_items = hdk::get_links(&list_addr, LinkMatch::Exactly("items"), LinkMatch::Any)?.addresses()
  138.         .iter()
  139.         .map(|item_address| {
  140.             hdk::utils::get_as_type::<ListItem>(item_address.to_owned())
  141.         })
  142.         .filter_map(Result::ok)
  143.         .collect::<Vec<ListItem>>();
  144.  
  145.     // if this was successful then return the list items
  146.     Ok(GetListResponse{
  147.         name: list.name,
  148.         items: list_items
  149.     })
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement