github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/todolist/todolist.gno (about)

     1  package todolist
     2  
     3  import (
     4  	"std"
     5  	"strconv"
     6  
     7  	"gno.land/p/demo/avl"
     8  )
     9  
    10  type TodoList struct {
    11  	Title string
    12  	Tasks *avl.Tree
    13  	Owner std.Address
    14  }
    15  
    16  type Task struct {
    17  	Title string
    18  	Done  bool
    19  }
    20  
    21  func NewTodoList(title string) *TodoList {
    22  	return &TodoList{
    23  		Title: title,
    24  		Tasks: avl.NewTree(),
    25  		Owner: std.GetOrigCaller(),
    26  	}
    27  }
    28  
    29  func NewTask(title string) *Task {
    30  	return &Task{
    31  		Title: title,
    32  		Done:  false,
    33  	}
    34  }
    35  
    36  func (tl *TodoList) AddTask(id int, task *Task) {
    37  	tl.Tasks.Set(strconv.Itoa(id), task)
    38  }
    39  
    40  func ToggleTaskStatus(task *Task) {
    41  	task.Done = !task.Done
    42  }
    43  
    44  func (tl *TodoList) RemoveTask(taskId string) {
    45  	tl.Tasks.Remove(taskId)
    46  }
    47  
    48  func (tl *TodoList) GetTasks() []*Task {
    49  	tasks := make([]*Task, 0, tl.Tasks.Size())
    50  	tl.Tasks.Iterate("", "", func(key string, value interface{}) bool {
    51  		tasks = append(tasks, value.(*Task))
    52  		return false
    53  	})
    54  	return tasks
    55  }
    56  
    57  func (tl *TodoList) GetTodolistOwner() std.Address {
    58  	return tl.Owner
    59  }
    60  
    61  func (tl *TodoList) GetTodolistTitle() string {
    62  	return tl.Title
    63  }