feat: todo
This commit is contained in:
parent
7b6833703a
commit
c738d9447c
3 changed files with 114 additions and 0 deletions
25
.gitignore
vendored
Normal file
25
.gitignore
vendored
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# Various IDEs and Editors
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
**/*~
|
||||||
|
|
||||||
|
# Mac OSX temporary files
|
||||||
|
.DS_Store
|
||||||
|
**/.DS_Store
|
||||||
|
|
||||||
|
# dfx temporary files
|
||||||
|
.dfx/
|
||||||
|
|
||||||
|
# generated files
|
||||||
|
**/declarations/
|
||||||
|
|
||||||
|
# rust
|
||||||
|
target/
|
||||||
|
|
||||||
|
# frontend code
|
||||||
|
node_modules/
|
||||||
|
dist/
|
||||||
|
.svelte-kit/
|
||||||
|
|
||||||
|
# environment variables
|
||||||
|
.env
|
16
dfx.json
Normal file
16
dfx.json
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"canisters": {
|
||||||
|
"motoko_backend": {
|
||||||
|
"main": "src/motoko_backend/main.mo",
|
||||||
|
"type": "motoko"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaults": {
|
||||||
|
"build": {
|
||||||
|
"args": "",
|
||||||
|
"packtool": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"output_env_file": ".env",
|
||||||
|
"version": 1
|
||||||
|
}
|
73
src/motoko_backend/main.mo
Normal file
73
src/motoko_backend/main.mo
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
import Text "mo:base/Text";
|
||||||
|
import Bool "mo:base/Bool";
|
||||||
|
import Hash "mo:base/Hash";
|
||||||
|
import Nat "mo:base/Nat";
|
||||||
|
import Map "mo:base/HashMap";
|
||||||
|
import Iter "mo:base/Iter";
|
||||||
|
actor Assistant
|
||||||
|
{
|
||||||
|
public query func greet(name : Text) : async Text
|
||||||
|
{
|
||||||
|
return "Hello, " # name # "!";
|
||||||
|
};
|
||||||
|
|
||||||
|
type ToDo =
|
||||||
|
{
|
||||||
|
description: Text;
|
||||||
|
completed: Bool;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
func naturalHash(n: Nat) : Hash.Hash
|
||||||
|
{
|
||||||
|
Text.hash(Nat.toText(n))
|
||||||
|
};
|
||||||
|
|
||||||
|
var todos = Map.HashMap<Nat, ToDo>(0, Nat.equal, naturalHash);
|
||||||
|
var nextID: Nat = 0;
|
||||||
|
|
||||||
|
public query func getTodos() : async [ToDo]
|
||||||
|
{
|
||||||
|
Iter.toArray(todos.vals());
|
||||||
|
};
|
||||||
|
|
||||||
|
public query func addTodo(description: Text) : async Nat
|
||||||
|
{
|
||||||
|
let id = nextID;
|
||||||
|
todos.put(id, {description = description; completed = false});
|
||||||
|
nextID += 1;
|
||||||
|
id
|
||||||
|
};
|
||||||
|
|
||||||
|
public func completeToDo(id: Nat): async ()
|
||||||
|
{
|
||||||
|
ignore do ?
|
||||||
|
{
|
||||||
|
let description = todos.get(id)!.description;
|
||||||
|
todos.put(id, {description; completed = true});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
public query func showTodos() : async Text
|
||||||
|
{
|
||||||
|
var output : Text = "\nTO-DO\n";
|
||||||
|
for (todo: ToDo in todos.vals())
|
||||||
|
{
|
||||||
|
output #= "\n" # todo.description;
|
||||||
|
if (todo.completed)
|
||||||
|
{
|
||||||
|
output #= "+";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
output # "\n"
|
||||||
|
};
|
||||||
|
|
||||||
|
public func clearCompleted() : async ()
|
||||||
|
{
|
||||||
|
todos := Map.mapFilter<Nat, ToDo, ToDo>(todos, Nat.equal, naturalHash,
|
||||||
|
func(_, todo)
|
||||||
|
{
|
||||||
|
if (todo.completed) null else ?todo
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue