db, handlers, and interfaces
This commit is contained in:
58
db/db.go
58
db/db.go
@@ -1,19 +1,61 @@
|
||||
package db
|
||||
|
||||
import "github.com/google/uuid"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
type Todo struct {
|
||||
Id uuid.UUID
|
||||
Name string
|
||||
Done bool
|
||||
}
|
||||
"github.com/google/uuid"
|
||||
"michaelthomson.dev/mthomson/go-todos-app/models"
|
||||
)
|
||||
|
||||
type TodosStore struct {
|
||||
Todos []Todo
|
||||
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: []Todo{},
|
||||
Todos: []models.Todo{},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user