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

     1  package todolistrealm
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  
     7  	"gno.land/p/demo/avl"
     8  	"gno.land/p/demo/seqid"
     9  	"gno.land/p/demo/todolist"
    10  	"gno.land/p/demo/ufmt"
    11  )
    12  
    13  // State variables
    14  var (
    15  	todolistTree *avl.Tree
    16  	tlid         seqid.ID
    17  )
    18  
    19  // Constructor
    20  func init() {
    21  	todolistTree = avl.NewTree()
    22  }
    23  
    24  func NewTodoList(title string) (int, string) {
    25  	// Create new Todolist
    26  	tl := todolist.NewTodoList(title)
    27  	// Update AVL tree with new state
    28  	tlid.Next()
    29  	todolistTree.Set(strconv.Itoa(int(tlid)), tl)
    30  	return int(tlid), "created successfully"
    31  }
    32  
    33  func AddTask(todolistID int, title string) string {
    34  	// Get Todolist from AVL tree
    35  	tl, ok := todolistTree.Get(strconv.Itoa(todolistID))
    36  	if !ok {
    37  		panic("Todolist not found")
    38  	}
    39  
    40  	// get the number of tasks in the todolist
    41  	id := tl.(*todolist.TodoList).Tasks.Size()
    42  
    43  	// create the task
    44  	task := todolist.NewTask(title)
    45  
    46  	// Cast raw data from tree into Todolist struct
    47  	tl.(*todolist.TodoList).AddTask(id, task)
    48  
    49  	return "task added successfully"
    50  }
    51  
    52  func ToggleTaskStatus(todolistID int, taskID int) string {
    53  	// Get Todolist from AVL tree
    54  	tl, ok := todolistTree.Get(strconv.Itoa(todolistID))
    55  	if !ok {
    56  		panic("Todolist not found")
    57  	}
    58  
    59  	// Get the task from the todolist
    60  	task, found := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID))
    61  	if !found {
    62  		panic("Task not found")
    63  	}
    64  
    65  	// Change the status of the task
    66  	todolist.ToggleTaskStatus(task.(*todolist.Task))
    67  
    68  	return "task status changed successfully"
    69  }
    70  
    71  func RemoveTask(todolistID int, taskID int) string {
    72  	// Get Todolist from AVL tree
    73  	tl, ok := todolistTree.Get(strconv.Itoa(todolistID))
    74  	if !ok {
    75  		panic("Todolist not found")
    76  	}
    77  
    78  	// Get the task from the todolist
    79  	_, ok = tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID))
    80  	if !ok {
    81  		panic("Task not found")
    82  	}
    83  
    84  	// Change the status of the task
    85  	tl.(*todolist.TodoList).RemoveTask(strconv.Itoa(taskID))
    86  
    87  	return "task status changed successfully"
    88  }
    89  
    90  func RemoveTodoList(todolistID int) string {
    91  	// Get Todolist from AVL tree
    92  	_, ok := todolistTree.Get(strconv.Itoa(todolistID))
    93  	if !ok {
    94  		panic("Todolist not found")
    95  	}
    96  
    97  	// Remove the todolist
    98  	todolistTree.Remove(strconv.Itoa(todolistID))
    99  
   100  	return "Todolist removed successfully"
   101  }
   102  
   103  func Render(path string) string {
   104  	if path == "" {
   105  		return renderHomepage()
   106  	}
   107  
   108  	return "unknown page"
   109  }
   110  
   111  func renderHomepage() string {
   112  	// Define empty buffer
   113  	var b bytes.Buffer
   114  
   115  	b.WriteString("# Welcome to ToDolist\n\n")
   116  
   117  	// If no todolists have been created
   118  	if todolistTree.Size() == 0 {
   119  		b.WriteString("### No todolists available currently!")
   120  		return b.String()
   121  	}
   122  
   123  	// Iterate through AVL tree
   124  	todolistTree.Iterate("", "", func(key string, value interface{}) bool {
   125  		// cast raw data from tree into Todolist struct
   126  		tl := value.(*todolist.TodoList)
   127  
   128  		// Add Todolist name
   129  		b.WriteString(
   130  			ufmt.Sprintf(
   131  				"## Todolist #%s: %s\n",
   132  				key, // Todolist ID
   133  				tl.GetTodolistTitle(),
   134  			),
   135  		)
   136  
   137  		// Add Todolist owner
   138  		b.WriteString(
   139  			ufmt.Sprintf(
   140  				"#### Todolist owner : %s\n",
   141  				tl.GetTodolistOwner(),
   142  			),
   143  		)
   144  
   145  		// List all todos that are currently Todolisted
   146  		if todos := tl.GetTasks(); len(todos) > 0 {
   147  			b.WriteString(
   148  				ufmt.Sprintf("Currently Todo tasks: %d\n\n", len(todos)),
   149  			)
   150  
   151  			for index, todo := range todos {
   152  				b.WriteString(
   153  					ufmt.Sprintf("#%d - %s  ", index, todo.Title),
   154  				)
   155  				// displays a checked box if task is marked as done, an empty box if not
   156  				if todo.Done {
   157  					b.WriteString(
   158  						"☑\n\n",
   159  					)
   160  					continue
   161  				}
   162  
   163  				b.WriteString(
   164  					"☐\n\n",
   165  				)
   166  			}
   167  		} else {
   168  			b.WriteString("No tasks in this list currently\n")
   169  		}
   170  
   171  		b.WriteString("\n")
   172  		return false
   173  	})
   174  
   175  	return b.String()
   176  }