Files

75 lines
2.1 KiB
Go

// 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"`
}