Feature: Reviews restructuring and Creating Collection Indexer and Services
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
# Collections
|
||||
|
||||
`Modules\Core\Catalog\Services\CollectionService` provides category browsing/nav AND
|
||||
single-collection lookup for a storefront — `list()`, `getById()`, `getBySlug()` —
|
||||
all reading directly from the Meilisearch index, mirroring
|
||||
`Modules\Core\Catalog\Services\ProductService` (see `product-listing.md`) exactly.
|
||||
|
||||
---
|
||||
|
||||
## Why it reads from the index, not the database
|
||||
|
||||
Lunar's own `Lunar\Search\CollectionIndexer` only carries `id`/`name`/`created_at` —
|
||||
nowhere near enough for a storefront category page or a nav tree.
|
||||
`Modules\Core\Catalog\Services\CollectionIndexer` extends it to add everything
|
||||
`CollectionService` needs:
|
||||
|
||||
| Field | Source | Notes |
|
||||
|---|---|---|
|
||||
| `parent_id` | `$model->parent_id` | Filterable. The nested-set tree's parent pointer — `null` for a top-level collection. |
|
||||
| `_lft` | `$model->_lft` | Filterable and sortable. The nested-set tree position — lets `CollectionService` resolve tree order without a database read. |
|
||||
| `collection_group_id` | `$model->collection_group_id` | Filterable. Mirrors `Collection::scopeInGroup()`. |
|
||||
| `slugs` | `$model->urls->pluck('slug')` | Filterable. Every locale's `Url::slug`, so `getBySlug()` resolves purely from the index. |
|
||||
| `thumbnail` | `$model->getThumbnailImage()` | Display only. `null` if the collection has no thumbnail image. |
|
||||
|
||||
`name`/`description` (and any other `TranslatedText` attribute) are indexed per-locale
|
||||
by Lunar's base indexer and resolved by `CollectionService` exactly like
|
||||
`ProductService` does — see `product-listing.md`'s "Locale resolution" section, same
|
||||
logic, same `LanguageCache::defaultLocale()` fallback.
|
||||
|
||||
---
|
||||
|
||||
## Usage
|
||||
|
||||
```php
|
||||
use Modules\Core\Catalog\DTOs\CollectionFilters;
|
||||
use Modules\Core\Catalog\Enums\CollectionSort;
|
||||
use Modules\Core\Catalog\Services\CollectionService;
|
||||
|
||||
$service = app(CollectionService::class);
|
||||
|
||||
// Top-level collections only (parent_id IS NULL) — for building a nav tree
|
||||
$roots = $service->list(
|
||||
filters: new CollectionFilters(rootOnly: true),
|
||||
sort: CollectionSort::Position,
|
||||
);
|
||||
|
||||
// Children of a specific collection
|
||||
$children = $service->list(
|
||||
filters: new CollectionFilters(parentId: 222),
|
||||
sort: CollectionSort::Position,
|
||||
);
|
||||
|
||||
// Filter by collection group
|
||||
$collections = $service->list(filters: new CollectionFilters(groupId: 4));
|
||||
|
||||
// Single collection, by primary key or slug
|
||||
$collection = $service->getById(223);
|
||||
$collection = $service->getBySlug('keychains');
|
||||
```
|
||||
|
||||
`CollectionFilters(parentId: ..., rootOnly: ...)` are mutually exclusive — if both are
|
||||
set, `parentId` wins. There's no `parentId: null` shorthand for "root only", since
|
||||
that would be ambiguous with "don't filter by parent at all" (the DTO's actual
|
||||
default); `rootOnly` names the root-collections case explicitly instead.
|
||||
|
||||
`CollectionSort::Position` (`_lft:asc`) is the recommended default for any nav/tree
|
||||
UI — it matches the order an admin arranges collections in Lunar's own Filament UI.
|
||||
`Name` and `Newest` are also available, mirroring `ProductSort`'s shape.
|
||||
|
||||
---
|
||||
|
||||
## Registration
|
||||
|
||||
Like `ProductIndexer`, `CollectionIndexer` must be registered in the consuming app's
|
||||
own `config/lunar/search.php`:
|
||||
|
||||
```php
|
||||
'indexers' => [
|
||||
Lunar\Models\Collection::class => Modules\Core\Catalog\Services\CollectionIndexer::class,
|
||||
// ...
|
||||
],
|
||||
```
|
||||
|
||||
New/changed fields aren't filterable/sortable in Meilisearch until `php artisan
|
||||
lunar:meilisearch:setup` re-syncs index settings, and existing documents need
|
||||
`lunar:search:index --refresh` to pick up the new shape. If `SCOUT_QUEUE` is enabled,
|
||||
the queue worker also needs restarting after deploying changes to the indexer class —
|
||||
see `docs/lunar.md` "Gotchas".
|
||||
|
||||
---
|
||||
|
||||
## When to still use Eloquent directly
|
||||
|
||||
A single collection's full detail page (breadcrumb via `$collection->breadcrumb`,
|
||||
tree ancestors/descendants, route-model-bound `Collection $collection` in a
|
||||
controller signature) should keep reading Eloquent directly rather than going through
|
||||
`CollectionService` — the indexed document doesn't carry ancestor chains or the full
|
||||
nested-set relations, and route-model binding already gives a controller the full
|
||||
model for free. `CollectionService` is for browsing/listing and lightweight
|
||||
by-id/by-slug lookups where a full Eloquent hydration would be wasteful, the same
|
||||
tradeoff `ProductService` makes for products.
|
||||
+14
-3
@@ -199,9 +199,20 @@ registered.
|
||||
### Seeding
|
||||
|
||||
A starter set of common e-shop labels (`nav.*`, `cart.*`, `product.*`, `auth.*`, `search.*`,
|
||||
English + Greek) is seeded by `Modules\Core\Command\InstallLunarCommand` (overrides Lunar's own
|
||||
`lunar:install`), guarded by `LanguageLine::where('group', 'storefront')->exists()` — same
|
||||
idempotent pattern as the rest of that command, safe to run unattended on every boot.
|
||||
`review.*`, `shop.*`, `pagination.*`, English + Greek) lives in
|
||||
`Modules\Core\Localization\Services\StorefrontLabels::all()` — kept as its own class, separate
|
||||
from the seeding logic, so the label list can be scanned/diffed without wading through the
|
||||
seeding mechanics.
|
||||
|
||||
`Modules\Core\Command\InstallLunarCommand` (overrides Lunar's own `lunar:install`) seeds them via
|
||||
a **per-key upsert**, not an all-or-nothing "only seed if the group is empty" guard: a key already
|
||||
present in the database — including one an admin has since edited via the Filament **Language
|
||||
Lines** resource — is left untouched; only keys missing entirely are created. This is what makes
|
||||
it safe to add new keys to `StorefrontLabels::all()` later and re-run `lunar:install` on an
|
||||
already-installed store, without either silently skipping the new keys (the old guard's behavior)
|
||||
or reverting an admin's edits back to the hardcoded default (what a naive `updateOrCreate` would
|
||||
do). New writes go through `TranslationService::create()`, so the usual cache-invalidation and
|
||||
activity-log events fire for them too.
|
||||
|
||||
### Admin UI
|
||||
|
||||
|
||||
@@ -72,7 +72,8 @@ needs, listing and detail alike:
|
||||
| Field | Source | Notes |
|
||||
|---|---|---|
|
||||
| `id` | — | Newly marked **filterable** — needed for `getById()`'s `id = "..."` filter; Meilisearch doesn't filter on the primary key by default. |
|
||||
| `collections` | `$product->collections` | Array of `{id, name}` — `name` is the translated collection name. Filterable on the nested field `collections.id`, not `collections` itself. |
|
||||
| `collections` | `$product->collections` | Array of `{id, name}` — directly assigned collections only, `name` is the translated collection name. Not filterable — see `collection_ids`. |
|
||||
| `collection_ids` | `$product->collections` + `->ancestors` | Filterable. Flat array of every directly-assigned collection's id, unioned with all of its ancestors' ids. `ProductFilters(collectionId: ...)` filters against this field, not `collections`, since products are typically attached only to leaf collections — a plain direct-match filter would never return anything for a parent/root category page. |
|
||||
| `slugs` | `$product->urls->pluck('slug')` | Filterable. Every locale's `Url::slug` for the product, so `getBySlug()` resolves purely from the index — no database read. |
|
||||
| `price` | Cheapest variant's base price | Filterable. Float in major units (e.g. `19.99`, not `1999`). Base price only — no customer group, default currency (`Currency::getDefault()`) only. `null` if the product has no priced variant yet, so it's excluded from range filters rather than treated as free. |
|
||||
| `brand` | Already indexed by Lunar's base indexer | Newly marked **filterable** — it existed in the document already, just wasn't usable in a `filter` clause. |
|
||||
|
||||
Reference in New Issue
Block a user