Skip to content

Instantly share code, notes, and snippets.

@achingachris
Last active April 4, 2024 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save achingachris/e1c965da1adda0d6b3231a62276e63b0 to your computer and use it in GitHub Desktop.
Save achingachris/e1c965da1adda0d6b3231a62276e63b0 to your computer and use it in GitHub Desktop.
#[derive(candid::CandidType, Clone)]
struct Mentor {
id: u64,
name: String,
interests: Vec<String>,
passions: Vec<String>,
location: String, // Location can be the ward level
}
#[derive(candid::CandidType, Clone)]
struct Mentee {
id: u64,
name: String,
interests: Vec<String>,
passions: Vec<String>,
location: String, // Location can be the ward level
}
type Mentors = StableBTreeMap<u64, Mentor, IdCell>;
type Mentees = StableBTreeMap<u64, Mentee, IdCell>;
#[derive(candid::CandidType)]
struct MentorshipContract {
mentors: RefCell<Mentors>,
mentees: RefCell<Mentees>,
next_mentor_id: RefCell<u64>,
next_mentee_id: RefCell<u64>,
}
#[ic_cdk_macros::init]
fn init() -> MentorshipContract {
MentorshipContract {
mentors: RefCell::new(Mentors::new(
MemoryId::from_u64(0),
Memory::new(MemoryId::from_u64(0)),
)),
mentees: RefCell::new(Mentees::new(
MemoryId::from_u64(1),
Memory::new(MemoryId::from_u64(1)),
)),
next_mentor_id: RefCell::new(1),
next_mentee_id: RefCell::new(1),
}
}
#[ic_cdk_macros::update]
fn add_mentor(&self, mentor: Mentor) {
let mut mentors = self.mentors.borrow_mut();
let mut next_mentor_id = self.next_mentor_id.borrow_mut();
mentor.id = *next_mentor_id;
mentors.insert(*next_mentor_id, mentor);
*next_mentor_id += 1;
}
#[ic_cdk_macros::update]
fn add_mentee(&self, mentee: Mentee) {
let mut mentees = self.mentees.borrow_mut();
let mut next_mentee_id = self.next_mentee_id.borrow_mut();
mentee.id = *next_mentee_id;
mentees.insert(*next_mentee_id, mentee);
*next_mentee_id += 1;
}
#[ic_cdk_macros::query]
fn match_mentors_to_mentees(&self) -> Vec<(Mentor, Vec<Mentee>)> {
let mentors = self.mentors.borrow();
let mentees = self.mentees.borrow();
let mut matches: Vec<(Mentor, Vec<Mentee>)> = Vec::new();
for (_, mentor) in mentors.iter() {
let mut matched_mentees: Vec<Mentee> = Vec::new();
for (_, mentee) in mentees.iter() {
// Check if mentee's location matches mentor's location
if mentor.location == mentee.location {
// Check if mentor's interests match mentee's interests
let shared_interests: Vec<&String> = mentor
.interests
.iter()
.filter(|interest| mentee.interests.contains(interest))
.collect();
// Check if mentor's passions match mentee's passions
let shared_passions: Vec<&String> = mentor
.passions
.iter()
.filter(|passion| mentee.passions.contains(passion))
.collect();
// If there are shared interests or passions, add mentee to matched list
if !shared_interests.is_empty() || !shared_passions.is_empty() {
matched_mentees.push(mentee.clone());
}
}
}
// Add mentor and matched mentees to the matches list
if !matched_mentees.is_empty() {
matches.push((mentor.clone(), matched_mentees));
}
}
matches
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment