Michael Thomson 09a957ca4b
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful
db, handlers, and interfaces
2024-06-15 21:33:14 -04:00

62 lines
1.1 KiB
Go

package db
import (
"fmt"
"github.com/google/uuid"
"michaelthomson.dev/mthomson/go-todos-app/models"
)
type TodosStore struct {
Todos []models.Todo
}
func (ts *TodosStore) List() (todo []models.Todo, err error) {
return ts.Todos, nil
}
func (ts *TodosStore) Get(id uuid.UUID) (todo models.Todo, err error) {
index := -1
for i, todo := range ts.Todos {
if id == todo.Id {
index = i
}
}
if index == -1 {
return models.Todo{}, fmt.Errorf("Could not find todo by id %q", id)
}
ts.Todos = append(ts.Todos[:index], ts.Todos[index + 1:]...)
return ts.Todos[index], nil
}
func (ts *TodosStore) Add(todo models.Todo) (addedTodo models.Todo, err error) {
ts.Todos = append(ts.Todos, todo)
return todo, nil
}
func (ts *TodosStore) Delete(id uuid.UUID) (err error) {
index := -1
for i, todo := range ts.Todos {
if id == todo.Id {
index = i
}
}
if index == -1 {
return fmt.Errorf("Could not find todo by id %q", id)
}
ts.Todos = append(ts.Todos[:index], ts.Todos[index + 1:]...)
return nil
}
func NewTodoStore() TodosStore {
return TodosStore{
Todos: []models.Todo{},
}
}