-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomeworkdiary.mo
79 lines (61 loc) · 2.16 KB
/
homeworkdiary.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Time "mo:base/Time";
import Buffer "mo:base/Buffer";
import Result "mo:base/Result";
import Array "mo:base/Array";
actor HomeworkDiary {
type Homework={
title: Text;
description: Text;
dueDate: Time.Time;
completed: Bool;
};
var homeworkDiary=Buffer.Buffer<Homework>(0);
public func addHomework (homework:Homework):async Nat{
homeworkDiary.add(homework);
return homeworkDiary.size()-1;
};
public query func getHomework (index:Nat):async Result.Result<Homework,Text>{
let result:?Homework=homeworkDiary.getOpt(index);
switch(result) {
case(null) { #err("Error on Index" )};
case(?content) { #ok(content); };
};
};
public func updateHomework (id:Nat,homework:Homework):async Result.Result<(),Text>{
let result:?Homework=homeworkDiary.getOpt(id);
switch(result) {
case(null) { #err("Error on Index" )};
case(?content) { homeworkDiary.put(id,homework); #ok() };
};
};
public func markAsCompleted (id:Nat):async Result.Result<(),Text> {
let result:?Homework=homeworkDiary.getOpt(id);
switch(result) {
case(null) { #err("Error on Index" )};
case(?content) { homeworkDiary.put(id,{content with completed=true}); #ok() };
};
};
public func deleteHomework (id:Nat):async Result.Result<(),Text> {
let result:?Homework=homeworkDiary.getOpt(id);
switch(result) {
case(null) { #err("Error on Index" )};
case(?content) { let x=homeworkDiary.remove(id); #ok() };
};
};
public query func getAllHomework(): async [Homework] {
return Buffer.toArray(homeworkDiary);
};
public query func getPendingHomework(): async [Homework] {
let pending = Buffer.clone(homeworkDiary);
pending.filterEntries(func(_, x) = x.completed == false);
return Buffer.toArray(pending);
};
public query func searchHomework(searchTerm: Text): async [Homework] {
let searchTitle = Buffer.clone(homeworkDiary);
let searchDescription = Buffer.clone(homeworkDiary);
searchTitle.filterEntries(func(_, x) = (x.title==searchTerm ));
searchDescription.filterEntries(func(_, x) = (x.description==searchTerm ));
searchTitle.append(searchDescription);
return Buffer.toArray(searchTitle);
};
}