Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions dna/course/zomes/courses/code/src/content/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@ pub fn section_entry_def() -> ValidatingEntryType {
hdk::ValidationPackageDefinition::Entry
},
validation: | validation_data: hdk::EntryValidationData<Content>| {
match validation_data {
EntryValidationData::Create { .. } => {
// TODO: implement validation
Ok(())
match validation_data {
EntryValidationData::Create { entry, validation_data } => {
validation::create(entry, validation_data)
},
EntryValidationData::Modify { .. } => {
// TODO: implement validation
Ok(())
EntryValidationData::Modify { new_entry, old_entry, old_entry_header, validation_data } => {
validation::modify(new_entry, old_entry, old_entry_header, validation_data)
},
EntryValidationData::Delete { .. } => {
// TODO: implement validation
Ok(())
EntryValidationData::Delete { old_entry, old_entry_header, validation_data } => {
validation::delete(old_entry, old_entry_header, validation_data)
}
}
}
Expand Down
86 changes: 86 additions & 0 deletions dna/course/zomes/courses/code/src/content/validation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use super::{ //Ummmm above should all probably get deleted but since content is linked to section anchor I've made changes. Also I feel like super is the wrong "expression" for retrieving section anchor as its in another directory
anchor::SectionAnchor,
entry::{Content},
};
use crate::anchor_trait::AnchorTrait;
use crate::helper;
use hdk::holochain_core_types::chain_header::ChainHeader;
use hdk::{LinkValidationData, ValidationData};
use holochain_entry_utils::HolochainEntry;

pub fn create(entry: Content, validation_data: ValidationData) -> Result<(), String> {
helper::validate_only_teacher_can_do( //only teachers to create content?
&entry.teacher_address,
validation_data.sources(),
"create their courses",
)?;
helper::validate_entity_title(&entry.title, &Content::entry_type())
}

pub fn modify(
new_entry: Content,
old_entry: Content,
_old_entry_header: ChainHeader,
validation_data: ValidationData,
) -> Result<(), String> {
helper::validate_only_teacher_can_do( //teachers only assumption, else delete rule
&old_entry.teacher_address,
validation_data.sources(),
"modify their content",
)?;
helper::validate_entity_title(&new_entry.title, &Course::entry_type())?;
validate_no_teacher_change(old_entry, new_entry)
}

// this fn is only needed in the current module so it's private
fn validate_no_teacher_change(old_entry: Content, new_entry: Contetnt) -> Result<(), String> {
if new_entry.teacher_address != old_entry.teacher_address {
return Err(String::from("Cannot change the teacher of the content"));
}
Ok(())
}

pub fn delete(
entry: Content,
_entry_header: ChainHeader,
validation_data: ValidationData,
) -> Result<(), String> {
helper::validate_only_teacher_can_do(
&entry.teacher_address,
validation_data.sources(),
"delete their content",
)
}

// =========================== ContentAnchor validation
/* Pointers changed to direct to the section anchor, though I reckon its an unnecessary section hence commenting it out

pub fn anchor_create(entry: SectionAnchor, validation_data: ValidationData) -> Result<(), String> {
helper::validate_only_teacher_can_do(
&entry.teacher_address,
validation_data.sources(),
"create their content",
)?;
helper::validate_entity_title(&entry.title, &SectionAnchor::entry_type())
}

// NOTE: we don't accept any parameters here because we don't need them to always return an error
// because this anchor can never be modified
pub fn anchor_modify() -> Result<(), String> {
Err(String::from(
"Can't modify the CourseAnchor entry: it can only be created or deleted",
))
}

pub fn anchor_delete(
entry: SectionAnchor,
_entry_header: ChainHeader,
validation_data: ValidationData,
) -> Result<(), String> {
helper::validate_only_teacher_can_do(
&entry.teacher_address,
validation_data.sources(),
"delete their content",
)
}
*/
77 changes: 37 additions & 40 deletions dna/course/zomes/courses/code/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// allowing unstable feature to remove item from the vector on a crate level
#![feature(vec_remove_item)]
// allowing for this Rust project to have dead code on a crate level
#![allow(dead_code)]
// unstable Rust feature
Expand All @@ -19,14 +17,12 @@ extern crate serde_json;
extern crate holochain_json_derive;

use hdk::prelude::*;

use hdk_proc_macros::zome;

// Declaring Rust modules that are used in our project
mod anchor_trait;
mod content;
mod course;
mod helper;
mod section;

#[zome]
mod courses {
Expand Down Expand Up @@ -59,8 +55,6 @@ mod courses {
course::entry::course_entry_def()
}

// zome_fn is used to specify that this is a function available to call from our zome
// "hc_public" here means that there are no restrictions as to who can call this function
#[zome_fn("hc_public")]
fn create_course(title: String, timestamp: u64) -> ZomeApiResult<Address> {
course::handlers::create(title, timestamp)
Expand All @@ -70,9 +64,15 @@ mod courses {
fn get_latest_course_entry(
course_anchor_address: Address,
) -> ZomeApiResult<Option<course::entry::Course>> {
course::handlers::get_latest_course_entry(course_anchor_address)
let latest_course_result = course::handlers::get_latest_course(&course_anchor_address)?;
match latest_course_result {
Some((course_entry, _course_entry_address)) => {
return Ok(Some(course_entry));
}
None => return Ok(None),
}
}

// sections_address - do I need to replicate this in the sections CRUD? Seems weird to update the course this way.
#[zome_fn("hc_public")]
fn update_course(
title: String,
Expand Down Expand Up @@ -102,87 +102,84 @@ mod courses {
course::handlers::get_my_enrolled_courses()
}

#[zome_fn("hc_public")]
fn enrol_in_course(course_anchor_address: Address) -> ZomeApiResult<Address> {
course::handlers::enrol_in_course(course_anchor_address)
}

#[zome_fn("hc_public")]
fn get_all_students(course_anchor_address: Address) -> ZomeApiResult<Vec<Address>> {
course::handlers::get_students(course_anchor_address)
}

// ====================== Section definitions
// TODO: implement section entry definitions
#[entry_def]
fn section_anchor_entry_definition() -> ValidatingEntryType {
section::anchor::section_anchor_def()
fn section_entry_definition() -> ValidatingEntryType {
course::entry::section_entry_def()
}

#[entry_def]
fn section_entry_definition() -> ValidatingEntryType {
section::entry::entry_def()
fn section_anchor_definition() -> ValidatingEntryType {
course::anchor::section_anchor_def()
}

// TODO: implement section CRUD methods --> fished out of modules "impl" right? or handlers?
#[zome_fn("hc_public")]
fn create_section(title: String, course_anchor_address: Address, timestamp: u64, anchor_address: Address,
) -> ZomeApiResult<Address> {
section::handlers::create(title, course_anchor_address, timestamp, anchor_address)
}

#[zome_fn("hc_public")]
fn get_latest_section_entry(
section_anchor_address: Address,
) -> ZomeApiResult<Option<section::entry::Section>> {
section::handlers::get_latest_section_entry(section_anchor_address)
section::handlers::get_latest_section_entry(section_anchor_address)?;
}

// this is copied off the course methods code and changed... is the sections_address part relevant here or in the above code?
#[zome_fn("hc_public")]
fn create_section(
fn update_section(
title: String,
course_anchor_address: Address,
timestamp: u64,
anchor_address: Address,
) -> ZomeApiResult<Address> {
section::handlers::create(title, &course_anchor_address, timestamp)
}

#[zome_fn("hc_public")]
fn update_section(title: String, section_anchor_address: Address) -> ZomeApiResult<Address> {
section::handlers::update(title, &section_anchor_address)
section::handlers::update(title, course_anchor_address, timestamp, anchor_address)
}

#[zome_fn("hc_public")]
fn delete_section(section_anchor_address: Address) -> ZomeApiResult<Address> {
section::handlers::delete(section_anchor_address)
}


// ====================== Content definitions
// TODO: implement content entry definition
#[entry_def]
fn content_entry_definition() -> ValidatingEntryType {
content::entry::section_entry_def()
course::entry::section_entry_def()
}

// TODO: implement content CRUD methods --> used section anchor addresses as content uses that anchor
#[zome_fn("hc_public")]
fn create_content(
name: String,
fn create_content(name: String,
section_anchor_address: Address,
url: String,
timestamp: u64,
description: String,
) -> ZomeApiResult<Address> {
content::handlers::create(name, section_anchor_address, url, timestamp, description)
content::handlers::create(name, section_anchor_address, url, timestamp, description) // defined in module
}

#[zome_fn("hc_public")]
fn get_contents(section_anchor_address: Address) -> ZomeApiResult<Vec<Address>> {
content::handlers::get_contents(&section_anchor_address)
content::handlers::get_contents(&section_anchor_address)?;
}

#[zome_fn("hc_public")]
fn update_content(
content_address: Address,
name: String,
section_anchor_address: Address,
url: String,
timestamp: u64,
description: String,
) -> ZomeApiResult<Address> {
content::handlers::update(content_address, name, url, description)
content::handlers::update(name, url, description, timestamp, section_anchor_address,)
}

#[zome_fn("hc_public")]
fn delete_content(content_address: Address) -> ZomeApiResult<Address> {
content::handlers::delete(content_address)
}
}
}
19 changes: 9 additions & 10 deletions dna/course/zomes/courses/code/src/section/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ pub fn section_anchor_def() -> ValidatingEntryType {
},
validation: | validation_data: hdk::EntryValidationData<SectionAnchor>| {
match validation_data{
EntryValidationData::Create { .. } => {
// TODO: implement validation
Ok(())
EntryValidationData::Create { entry, validation_data } => {
validation::anchor_create(entry, validation_data)
},
// NOTE: the symbol .. means that we're skipping unpacking parameters that we receive here
// because we won't need them
EntryValidationData::Modify { .. } => {
// TODO: implement validation
Ok(())
validation::anchor_modify()
},
EntryValidationData::Delete { .. } => {
// TODO: implement validation
Ok(())
EntryValidationData::Delete { old_entry, old_entry_header, validation_data } => {
validation::anchor_delete(old_entry, old_entry_header, validation_data)
}
}
},
Expand All @@ -69,8 +68,8 @@ pub fn section_anchor_def() -> ValidatingEntryType {
validation_package:||{
hdk::ValidationPackageDefinition::Entry
},
validation:|_validation_data: hdk::LinkValidationData|{
Ok(())
validation:|validation_data: hdk::LinkValidationData|{
validation::anchor_to_section_link(validation_data)
}
),
to!(
Expand Down
17 changes: 7 additions & 10 deletions dna/course/zomes/courses/code/src/section/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,15 @@ pub fn entry_def() -> ValidatingEntryType {
hdk::ValidationPackageDefinition::Entry
},
validation: | validation_data: hdk::EntryValidationData<Section>| {
match validation_data {
EntryValidationData::Create { .. } => {
// TODO: implement validation
Ok(())
match validation_data {
EntryValidationData::Create { entry, validation_data } => {
validation::create(entry, validation_data)
},
EntryValidationData::Modify { .. } => {
// TODO: implement validation
Ok(())
EntryValidationData::Modify { new_entry, old_entry, old_entry_header, validation_data } => {
validation::modify(new_entry, old_entry, old_entry_header, validation_data)
},
EntryValidationData::Delete { .. } => {
// TODO: implement validation
Ok(())
EntryValidationData::Delete { old_entry, old_entry_header, validation_data } => {
validation::delete(old_entry, old_entry_header, validation_data)
}
}
},
Expand Down
Loading