DDD Meilisearch showcase: domain/app/infra/interfaces, Taskfile, tests
This commit is contained in:
commit
a9674ce14e
22 files changed
+1584
No files matched your search
@@ -0,0 +1,74 @@
|
||||
// Package app is the application layer: use cases that orchestrate the domain
|
||||
// and the ports. No business rules live here, and no HTTP/Meili types either.
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/domain/catalog"
|
||||
)
|
||||
|
||||
// CatalogService exposes the use cases of the catalog bounded context.
|
||||
type CatalogService struct {
|
||||
index catalog.MovieIndex
|
||||
}
|
||||
|
||||
func NewCatalogService(index catalog.MovieIndex) *CatalogService {
|
||||
return &CatalogService{index: index}
|
||||
}
|
||||
|
||||
// Setup prepares the read model (index settings). Idempotent.
|
||||
func (s *CatalogService) Setup(ctx context.Context) error {
|
||||
return s.index.EnsureSchema(ctx)
|
||||
}
|
||||
|
||||
// ImportMovies validates raw input into aggregates and indexes them.
|
||||
func (s *CatalogService) ImportMovies(ctx context.Context, in []MovieInput) (int, error) {
|
||||
movies := make([]*catalog.Movie, 0, len(in))
|
||||
for i, r := range in {
|
||||
id, err := catalog.NewMovieID(r.ID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("record %d: %w", i, err)
|
||||
}
|
||||
m, err := catalog.NewMovie(id, r.Title, r.Overview, r.Genres, r.Year, r.Rating)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("record %d: %w", i, err)
|
||||
}
|
||||
movies = append(movies, m)
|
||||
}
|
||||
if err := s.index.Index(ctx, movies); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(movies), nil
|
||||
}
|
||||
|
||||
func (s *CatalogService) Search(ctx context.Context, q catalog.Query) (catalog.Results, error) {
|
||||
return s.index.Search(ctx, q.Normalize())
|
||||
}
|
||||
|
||||
func (s *CatalogService) Get(ctx context.Context, rawID string) (*catalog.Movie, error) {
|
||||
id, err := catalog.NewMovieID(rawID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.index.Get(ctx, id)
|
||||
}
|
||||
|
||||
func (s *CatalogService) Delete(ctx context.Context, rawID string) error {
|
||||
id, err := catalog.NewMovieID(rawID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.index.Delete(ctx, id)
|
||||
}
|
||||
|
||||
// MovieInput is the unvalidated shape crossing into the application layer.
|
||||
type MovieInput struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Overview string `json:"overview"`
|
||||
Genres []string `json:"genres"`
|
||||
Year int `json:"year"`
|
||||
Rating float64 `json:"rating"`
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/app"
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/domain/catalog"
|
||||
)
|
||||
|
||||
// fakeIndex is an in-memory MovieIndex: the port makes the use cases testable
|
||||
// without Meilisearch running.
|
||||
type fakeIndex struct {
|
||||
movies map[catalog.MovieID]*catalog.Movie
|
||||
schema bool
|
||||
}
|
||||
|
||||
func newFake() *fakeIndex { return &fakeIndex{movies: map[catalog.MovieID]*catalog.Movie{}} }
|
||||
|
||||
func (f *fakeIndex) EnsureSchema(context.Context) error { f.schema = true; return nil }
|
||||
func (f *fakeIndex) Index(_ context.Context, ms []*catalog.Movie) error {
|
||||
for _, m := range ms {
|
||||
f.movies[m.ID()] = m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (f *fakeIndex) Get(_ context.Context, id catalog.MovieID) (*catalog.Movie, error) {
|
||||
m, ok := f.movies[id]
|
||||
if !ok {
|
||||
return nil, catalog.ErrNotFound
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
func (f *fakeIndex) Delete(_ context.Context, id catalog.MovieID) error {
|
||||
delete(f.movies, id)
|
||||
return nil
|
||||
}
|
||||
func (f *fakeIndex) Search(_ context.Context, q catalog.Query) (catalog.Results, error) {
|
||||
var r catalog.Results
|
||||
for _, m := range f.movies {
|
||||
r.Hits = append(r.Hits, catalog.Hit{Movie: m})
|
||||
}
|
||||
r.EstimatedTotal = int64(len(r.Hits))
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func TestImportRejectsInvalidRecord(t *testing.T) {
|
||||
svc := app.NewCatalogService(newFake())
|
||||
_, err := svc.ImportMovies(context.Background(), []app.MovieInput{
|
||||
{ID: "1", Title: "Alien", Year: 1979, Rating: 8.5},
|
||||
{ID: "2", Title: "", Year: 1999, Rating: 7},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("want validation error for record 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportThenGet(t *testing.T) {
|
||||
f := newFake()
|
||||
svc := app.NewCatalogService(f)
|
||||
n, err := svc.ImportMovies(context.Background(), []app.MovieInput{{ID: "1", Title: "Alien", Year: 1979, Rating: 8.5}})
|
||||
if err != nil || n != 1 {
|
||||
t.Fatalf("import: n=%d err=%v", n, err)
|
||||
}
|
||||
m, err := svc.Get(context.Background(), "1")
|
||||
if err != nil || m.Title() != "Alien" {
|
||||
t.Fatalf("get: %v %v", m, err)
|
||||
}
|
||||
if _, err := svc.Get(context.Background(), " "); err == nil {
|
||||
t.Fatal("want invalid id error")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user