Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): add todolist package & realm #1811

Merged
merged 15 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 6 additions & 0 deletions examples/gno.land/p/demo/todolist/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/p/demo/todolist

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
68 changes: 68 additions & 0 deletions examples/gno.land/p/demo/todolist/todolist.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package todolist

import (
"std"
"strconv"

"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
)

type TodoList struct {
Title string
Tasks avl.Tree
Owner std.Address
}

type Task struct {
Title string
Done bool
}

func NewTodoList(title string) *TodoList {
return &TodoList{
Title: title,
Tasks: avl.Tree{},
MalekLahbib marked this conversation as resolved.
Show resolved Hide resolved
Owner: std.GetOrigCaller(),
}
}

func NewTask(title string) *Task {
return &Task{
Title: title,
Done: false,
}
}

func (tl *TodoList) AddTask(id int, task *Task) {
tl.Tasks.Set(strconv.Itoa(id), task)
}

func ToggleTaskStatus(task *Task) {
task.Done = !task.Done
}

func (tl *TodoList) RemoveTask(taskId string) {
tl.Tasks.Remove(taskId)
}

func (tl *TodoList) GetTasks() []*Task {
tasks := make([]*Task, 0, tl.Tasks.Size())
tl.Tasks.Iterate("", "", func(key string, value interface{}) bool {
tasks = append(tasks, value.(*Task))
return false
})
return tasks
}

func (tl *TodoList) String() string {
return ufmt.Sprintf("TodoList{Title: %q, Tasks: %v, Owner: %v}", tl.Title, tl.GetTasks(), tl.Owner)
}

func (tl *TodoList) GetTodolistOwner() std.Address {
return tl.Owner
}

func (tl *TodoList) GetTodolistTitle() string {
return tl.Title
}
51 changes: 51 additions & 0 deletions examples/gno.land/p/demo/todolist/todolist_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package todolist

import (
"testing"

"gno.land/p/demo/avl"
)

func TestTodolistNew(t *testing.T) {
newtl := &TodoList{
Title: "new todolist",
Tasks: avl.Tree{},
}
tl := NewTodoList("new todolist")
if tl.Title != newtl.Title {
t.Fatalf("title is not good")
}
if tl.Tasks != newtl.Tasks {
t.Fatalf("tasks not good")
}

// test new task
newtask := &Task{
Title: "new task",
Done: false,
}
nt := NewTask("new task")
if nt.Title != newtask.Title {
t.Fatalf("title is not good")
}
if nt.Done != newtask.Done {
t.Fatalf("done is not good")
}

// test add a task
id := tl.Tasks.Size()
tl.AddTask(id, nt)
if tl.Tasks.Size() != 1 {
t.Fatalf("task not added")
}

// test ToggleTaskStatus
task, exists := tl.Tasks.Get("0")
if !exists {
t.Fatalf("task not found")
}
ToggleTaskStatus(task.(*Task))
if task.(*Task).Done != true {
t.Fatalf("task not toggled")
}
}
7 changes: 7 additions & 0 deletions examples/gno.land/r/demo/todolist/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/r/demo/todolist

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/todolist v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
179 changes: 179 additions & 0 deletions examples/gno.land/r/demo/todolist/todolist.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package todolist

import (
"bytes"
"strconv"

"gno.land/p/demo/avl"
"gno.land/p/demo/todolist"
"gno.land/p/demo/ufmt"
)

// State variables
var (
todolistTree *avl.Tree
tlid int
MalekLahbib marked this conversation as resolved.
Show resolved Hide resolved
)

// Constructor
func init() {
todolistTree = avl.NewTree()
}

func NewTodolist(title string) (int, string) {
// Create new Todolist
tl := todolist.NewTodoList(title)
// Update AVL tree with new state
todolistTree.Set(strconv.Itoa(tlid), tl)
tlid++
return tlid - 1, "created successfully"
}

func AddTask(todolistID int, title string) string {
// Get Todolist from AVL tree
tl, ok := todolistTree.Get(strconv.Itoa(todolistID))
if !ok {
return "Todolist not found"
MalekLahbib marked this conversation as resolved.
Show resolved Hide resolved
}

// get the number of tasks in the todolist
id := tl.(*todolist.TodoList).Tasks.Size()

// create the task
task := todolist.NewTask(title)

// Cast raw data from tree into Todolist struct
tl.(*todolist.TodoList).AddTask(id, task)

return "task added successfully"
}

func ToggleTaskStatus(todolistID int, taskID int) string {
// Get Todolist from AVL tree
tl, ok := todolistTree.Get(strconv.Itoa(todolistID))
if !ok {
return "Todolist not found"
MalekLahbib marked this conversation as resolved.
Show resolved Hide resolved
}

// Get the task from the todolist
task, found := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID))
if !found {
return "Task not found"
}

// Change the status of the task
todolist.ToggleTaskStatus(task.(*todolist.Task))

return "task status changed successfully"
}

func RemoveTask(todolistID int, taskID int) string {
// Get user who sent the transaction
// txSender := std.GetOrigCaller()
MalekLahbib marked this conversation as resolved.
Show resolved Hide resolved

// Get Todolist from AVL tree
tl, ok := todolistTree.Get(strconv.Itoa(todolistID))
if !ok {
return "Todolist not found"
}

// Get the task from the todolist
_, ok = tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID))
if !ok {
return "Task not found"
}

// Change the status of the task
tl.(*todolist.TodoList).RemoveTask(strconv.Itoa(taskID))

return "task status changed successfully"
}

func RemoveTodoList(todolistID int) string {
// Get user who sent the transaction
// txSender := std.GetOrigCaller()

// Get Todolist from AVL tree
_, ok := todolistTree.Get(strconv.Itoa(todolistID))
if !ok {
return "Todolist not found"
}

// Remove the todolist
todolistTree.Remove(strconv.Itoa(todolistID))

return "Todolist removed successfully"
}

func Render(path string) string {
if path == "" {
return renderHomepage()
}

return "unknown page"
}

func renderHomepage() string {
// Define empty buffer
var b bytes.Buffer

b.WriteString("# Welcome to ToDolist\n\n")

// If no todolists have been created
if todolistTree.Size() == 0 {
b.WriteString("### No todolists available currently!")
return b.String()
}

// Iterate through AVL tree
todolistTree.Iterate("", "", func(key string, value interface{}) bool {
// cast raw data from tree into Todolist struct
tl := value.(*todolist.TodoList)

// Add Todolist name
b.WriteString(
ufmt.Sprintf(
"## Todolist #%s: %s\n",
key, // Todolist ID
tl.GetTodolistTitle(),
),
)

// Add Todolist owner
b.WriteString(
ufmt.Sprintf(
"#### Todolist owner : %s\n",
tl.GetTodolistOwner(),
),
)

// List all todos that are currently Todolisted
if todos := tl.GetTasks(); len(todos) > 0 {
b.WriteString(
ufmt.Sprintf("Currently Todo tasks: %d\n\n", len(todos)),
)

for index, todo := range todos {
b.WriteString(
ufmt.Sprintf("#%d - %s ", index, todo.Title),
)
if todo.Done {
b.WriteString(
"☑\n\n",
)
} else {
b.WriteString(
"☐\n\n",
)
}
MalekLahbib marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
b.WriteString("No tasks in this list currently\n")
}

b.WriteString("\n")
return false
})

return b.String()
}
Loading