37 lines
1004 B
Go
37 lines
1004 B
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"code.nochebuena.dev/go/todo-api/internal/domain"
|
||
|
|
"code.nochebuena.dev/go/todo-api/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
// CreateTodoRequest is the input for creating a new todo.
|
||
|
|
type CreateTodoRequest struct {
|
||
|
|
Title string `json:"title" validate:"required,min=1,max=255"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// TodoService handles todo business logic.
|
||
|
|
type TodoService interface {
|
||
|
|
FindAll(ctx context.Context) ([]domain.Todo, error)
|
||
|
|
Create(ctx context.Context, req CreateTodoRequest) (domain.Todo, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
type todoService struct {
|
||
|
|
repo repository.TodoRepository
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewTodoService returns a TodoService backed by the given repository.
|
||
|
|
func NewTodoService(repo repository.TodoRepository) TodoService {
|
||
|
|
return &todoService{repo: repo}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *todoService) FindAll(ctx context.Context) ([]domain.Todo, error) {
|
||
|
|
return s.repo.FindAll(ctx)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *todoService) Create(ctx context.Context, req CreateTodoRequest) (domain.Todo, error) {
|
||
|
|
return s.repo.Create(ctx, domain.Todo{Title: req.Title})
|
||
|
|
}
|