-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.mo
More file actions
179 lines (141 loc) · 5.09 KB
/
lib.mo
File metadata and controls
179 lines (141 loc) · 5.09 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import Option "mo:base@0.16/Option";
import Array "mo:base@0.16/Array";
import Itertools "mo:itertools@0.2/Iter";
import ZenDB "../../src/EmbeddedInstance";
shared ({ caller = owner }) actor class Notes() {
stable let zendb = ZenDB.newStableStore(null);
let db = ZenDB.launchDefaultDB(zendb);
type Note = {
user_id : Principal;
title : Text;
content : Text;
};
let NoteSchema : ZenDB.Types.Schema = #Record([
("user_id", #Principal),
("title", #Text),
("content", #Text),
]);
let candify_notes : ZenDB.Types.Candify<Note> = {
from_blob = func(blob : Blob) : ?Note { from_candid (blob) };
to_blob = func(c : Note) : Blob { to_candid (c) };
};
let schema_constraints : [ZenDB.Types.SchemaConstraint] = [
#Unique(["user_id", "title"]), // a user cannot have two notes with the same title
#Field("title", [#MaxSize(100)]), // title must be <= 100 characters
#Field("content", [#MaxSize(100_000)]), // content must be <= 100_000 characters
];
let #ok(notes_collection) = db.createCollection<Note>("notes", NoteSchema, candify_notes, schema_constraints);
// no need to create additional indexes, as the unique constraint on (user_id, title) will create an index for us
public shared ({ caller = user_id }) func createNote(title : Text, content : Text) : async ZenDB.Types.Result<Nat, Text> {
let note : Note = { user_id; title; content };
notes_collection.insert(note);
};
public shared ({ caller = user_id }) func getNote(title : Text) : async ZenDB.Types.Result<Note, Text> {
let response = notes_collection.search(
ZenDB.QueryBuilder().Where(
"user_id",
#eq(#Principal(user_id)),
).And(
"title",
#eq(#Text(title)),
)
);
let notes = switch (response) {
case (#ok(notes)) notes;
case (#err(msg)) return #err(msg);
};
if (notes.size() == 0) {
return #err("Note not found");
};
let (note_id, note) = notes.get(0);
#ok(note);
};
public shared ({ caller = user_id }) func updateNote(title : Text, content : Text) : async ZenDB.Types.Result<(), Text> {
let notes_to_update_query = ZenDB.QueryBuilder().Where(
"user_id",
#eq(#Principal(user_id)),
).And(
"title",
#eq(#Text(title)),
);
let response = notes_collection.update(
notes_to_update_query,
[
("content", #Text(content)),
],
);
switch (response) {
case (#ok(documents_updated)) assert documents_updated == 1;
case (#err(msg)) return #err(msg);
};
#ok();
};
public shared ({ caller = user_id }) func deleteNote(title : Text) : async ZenDB.Types.Result<(), Text> {
let response = notes_collection.delete(
ZenDB.QueryBuilder().Where(
"user_id",
#eq(#Principal(user_id)),
).And(
"title",
#eq(#Text(title)),
)
);
let deleted_notes = switch (response) {
case (#ok(deleted_notes)) deleted_notes;
case (#err(msg)) return #err(msg);
};
assert Itertools.all(
deleted_notes.vals(),
func((note_id, note) : (Nat, Note)) : Bool {
note.user_id == user_id and note.title == title;
},
);
#ok();
};
public shared ({ caller = user_id }) func getAllNotes(curr_page : Nat, opt_page_size : ?Nat) : async ZenDB.Types.Result<[Note], Text> {
let page_size = Option.get(opt_page_size, 10);
let response = notes_collection.search(
ZenDB.QueryBuilder().Where(
"user_id",
#eq(#Principal(user_id)),
).Limit(
page_size
).Skip(
page_size * curr_page
)
);
let notes = switch (response) {
case (#ok(notes)) notes;
case (#err(msg)) return #err(msg);
};
#ok(
Array.map(
notes,
func((note_id, note) : (Nat, Note)) : Note {
note;
},
)
);
};
public shared ({ caller = user_id }) func getNoteCount() : async ZenDB.Types.Result<Nat, Text> {
let response = notes_collection.count(
ZenDB.QueryBuilder().Where(
"user_id",
#eq(#Principal(user_id)),
)
);
let count = switch (response) {
case (#ok(count)) count;
case (#err(msg)) return #err(msg);
};
#ok(count);
};
public shared ({ caller }) func getAllNotesCount() : async ZenDB.Types.Result<Nat, Text> {
if (caller != owner) {
return #err("Only the owner can call this function");
};
notes_collection.count(
ZenDB.QueryBuilder()
);
};
};