todo

package module
v0.0.0-...-6a58efc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

README

Example – To-Do App

This example shows how to implement a "todo list" app. The app consists of a "todo" server and a client that sends commands to the server.

Details

Backends
  • NATS Core (Event Bus)
  • MongoDB (Event Store)

Build & Run

Requires Docker.

Default setup
make build && make default
Debounced projection

This setup sets the TODO_DEBOUNCE environment variable to 1s, resulting in a single "batch"-update of the Counter projection.

make build && make debounce

Documentation

Index

Constants

View Source
const (
	AddTaskCmd    = "todo.list.add_task"
	RemoveTaskCmd = "todo.list.remove_task"
	DoneTaskCmd   = "todo.list.done_task"
)

Commands

View Source
const (
	TaskAdded   = "todo.list.task_added"
	TaskRemoved = "todo.list.task_removed"
	TasksDone   = "todo.list.tasks_done"
)

Events

View Source
const ListAggregate = "todo.list"

ListAggregate is the name of the List aggregate.

Variables

View Source
var ListEvents = [...]string{
	TaskAdded,
	TaskRemoved,
	TasksDone,
}

ListEvents are all events of a todo list.

Functions

func AddTask

func AddTask(listID uuid.UUID, task string) command.Cmd[string]

AddTask returns the command to add the given task to the given todo list.

func DoneTasks

func DoneTasks(listID uuid.UUID, tasks ...string) command.Cmd[[]string]

DoneTasks returns the command to mark the given tasks within the given list a done.

func HandleCommands

func HandleCommands(ctx context.Context, bus command.Bus, repo aggregate.Repository) <-chan error

HandleCommands handles todo list commands that are dispatched over the provided command bus until ctx is canceled. Command errors are sent into the returned error channel.

func RegisterCommands

func RegisterCommands(r codec.Registerer)

RegisterCommands registers commands into a registry.

func RegisterEvents

func RegisterEvents(r codec.Registerer)

RegisterEvents registers events into a registry.

func RemoveTask

func RemoveTask(listID uuid.UUID, task string) command.Cmd[string]

RemoveTask removes the command to remove the given task from the given todo list.

Types

type Counter

type Counter struct {
	*projection.Base

	sync.RWMutex
	// contains filtered or unexported fields
}

Counter is a read model that provides the number of active, removed, and archived tasks.

func NewCounter

func NewCounter() *Counter

NewCounter returns a new task counter.

func (*Counter) Active

func (c *Counter) Active() int

Active returns the active tasks.

func (*Counter) Archived

func (c *Counter) Archived() int

Archived returns the archived tasks.

func (*Counter) Project

func (c *Counter) Project(
	ctx context.Context,
	bus event.Bus,
	store event.Store,
	opts ...schedule.ContinuousOption,
) (<-chan error, error)

Project projects the Counter until ctx is canceled. Each time one of TaskEvents is published, the counter is updated.

func (*Counter) Removed

func (c *Counter) Removed() int

Removed returns the removed tasks.

type List

type List struct {
	*aggregate.Base
	*handler.BaseHandler
	// contains filtered or unexported fields
}

List is a "todo" list.

func New

func New(id uuid.UUID) *List

New returns the "todo" list with the given id.

func (*List) Add

func (list *List) Add(task string) error

Add adds the given task to the list, if it doesn't contain the task yet.

func (*List) Archive

func (list *List) Archive() []string

Archive returns the completed tasks.

func (*List) Contains

func (list *List) Contains(task string) bool

Contains returns whether the list contains the given task (case-insensitive).

func (*List) Done

func (list *List) Done(tasks ...string) error

Done marks the given tasks as done.

func (*List) Remove

func (list *List) Remove(task string) error

Remove removes the given task from the list.

func (*List) Tasks

func (list *List) Tasks() []string

Tasks returns the active tasks.

type TaskRemovedEvent

type TaskRemovedEvent struct{ Task string }

TaskRemovedEvent is the event data for the TaskRemoved event.

You can use any type as event data. In this case, the event data is a struct. The event data types for the TaskAdded and TasksDone events are `string` and `[]string` respectively.

Directories

Path Synopsis
cmd
client command
server command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL