feat(todo-api): add full-stack POC demonstrating micro-lib v0.9.0
Runnable REST API exercising every micro-lib tier in a containerless setup: N-layer architecture, SQLite persistence, header-based auth simulating Firebase output, and bit-mask RBAC enforcement. What's included: - cmd/todo-api: minimal main delegating to application.Run - internal/application: full object graph wiring — launcher, sqlite, httpserver, httpmw stack, routes in BeforeStart - internal/domain: User entity, ResourceTodos constant, PermReadTodo/PermWriteTodo bit positions - internal/repository: TodoRepository, UserRepository, DBPermissionProvider (SQLite via modernc) - internal/service: TodoService, UserService with interface-based dependencies - internal/handler: TodoHandler, UserHandler using httputil adapters and valid for input validation - internal/middleware: Auth (X-User-ID → rbac.Identity) and Require (bit-mask permission gate) - logAdapter: bridges logz.Logger.With return type to httpmw.Logger interface - SQLite schema: users, user_role (bitmask), todos; migrations run in BeforeStart - Routes: POST /users (open), GET+POST /todos (RBAC), GET /users (RBAC) Tested-via: todo-api POC integration Reviewed-against: docs/adr/
This commit is contained in:
61
internal/repository/todo_repository.go
Normal file
61
internal/repository/todo_repository.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.nochebuena.dev/go/sqlite"
|
||||
"code.nochebuena.dev/go/todo-api/internal/domain"
|
||||
)
|
||||
|
||||
// TodoRepository defines persistence operations for todos.
|
||||
type TodoRepository interface {
|
||||
FindAll(ctx context.Context) ([]domain.Todo, error)
|
||||
Create(ctx context.Context, todo domain.Todo) (domain.Todo, error)
|
||||
}
|
||||
|
||||
type todoRepository struct {
|
||||
db sqlite.Client
|
||||
}
|
||||
|
||||
// NewTodoRepository returns a SQLite-backed TodoRepository.
|
||||
func NewTodoRepository(db sqlite.Client) TodoRepository {
|
||||
return &todoRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *todoRepository) FindAll(ctx context.Context) ([]domain.Todo, error) {
|
||||
rows, err := r.db.GetExecutor(ctx).QueryContext(ctx,
|
||||
`SELECT id, title, done, created_at FROM todos ORDER BY created_at DESC`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, r.db.HandleError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var todos []domain.Todo
|
||||
for rows.Next() {
|
||||
var t domain.Todo
|
||||
if err := rows.Scan(&t.ID, &t.Title, &t.Done, &t.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
todos = append(todos, t)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if todos == nil {
|
||||
todos = []domain.Todo{} // always return a slice, never nil
|
||||
}
|
||||
return todos, nil
|
||||
}
|
||||
|
||||
func (r *todoRepository) Create(ctx context.Context, todo domain.Todo) (domain.Todo, error) {
|
||||
row := r.db.GetExecutor(ctx).QueryRowContext(ctx,
|
||||
`INSERT INTO todos (title, done) VALUES (?, ?) RETURNING id, title, done, created_at`,
|
||||
todo.Title, todo.Done,
|
||||
)
|
||||
var t domain.Todo
|
||||
if err := row.Scan(&t.ID, &t.Title, &t.Done, &t.CreatedAt); err != nil {
|
||||
return domain.Todo{}, r.db.HandleError(err)
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
Reference in New Issue
Block a user