Skip to content

Commit

Permalink
Merge branch 'feature/HES-2219' into 'release/5.2'
Browse files Browse the repository at this point in the history
Resolve HES-2219 "Feature/"

See merge request heseya/core!380
  • Loading branch information
bvlinsky committed Sep 19, 2023
2 parents d55fb7a + 25e52d9 commit f59a5d9
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 387 deletions.
18 changes: 7 additions & 11 deletions app/Criteria/ProductSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ class ProductSearch extends Criterion
{
public function query(Builder $query): Builder
{
if ($this->value === null) {
return $query;
}

return $query
->where(function (Builder $query): void {
$query
->whereFullText(['search_values'], $this->value)
->orWhere('id', 'LIKE', '%' . $this->value . '%')
->orWhere('slug', 'LIKE', '%' . $this->value . '%');
});
return $query->whereFullText([
'name',
'slug',
'description_html',
'description_short',
'search_values',
], $this->value);
}
}
19 changes: 2 additions & 17 deletions app/Dtos/ProductSearchDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ProductSearchDto extends Dto implements InstantiateFromRequest
{
private ?string $search;
private Missing|string $search;
private ?string $sort;

private array|Missing $ids;
Expand Down Expand Up @@ -46,7 +46,7 @@ public static function instantiateFromRequest(FormRequest|ProductIndexRequest $r
? Str::replace('price:desc', 'price_max:desc', $sort) : $sort;

return new self(
search: $request->input('search'),
search: $request->input('search', new Missing()),
sort: $sort,
ids: $request->input('ids', new Missing()),
slug: $request->input('slug', new Missing()),
Expand All @@ -70,26 +70,11 @@ public static function instantiateFromRequest(FormRequest|ProductIndexRequest $r
);
}

public function getSearch(): ?string
{
return $this->search;
}

public function getSort(): ?string
{
return $this->sort;
}

public function getPriceMin(): float|Missing
{
return $this->price_min;
}

public function getPriceMax(): float|Missing
{
return $this->price_max;
}

private static function boolean(string $key, FormRequest|ProductIndexRequest $request): bool|Missing
{
if (!$request->has($key)) {
Expand Down
9 changes: 3 additions & 6 deletions app/Events/ProductSearchValueEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ class ProductSearchValueEvent
use Dispatchable;
use SerializesModels;

public function __construct(protected array $productIds) {}

public function getProductIds(): array
{
return $this->productIds;
}
public function __construct(
public readonly array $product_ids,
) {}
}
8 changes: 4 additions & 4 deletions app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use App\Http\Resources\ResourceCollection;
use App\Models\MediaAttachment;
use App\Models\Product;
use App\Repositories\Contracts\ProductRepositoryContract;
use App\Repositories\ProductRepository;
use App\Services\Contracts\MediaAttachmentServiceContract;
use App\Services\Contracts\ProductServiceContract;
use Illuminate\Http\JsonResponse;
Expand All @@ -29,9 +29,9 @@
class ProductController extends Controller
{
public function __construct(
private ProductServiceContract $productService,
private ProductRepositoryContract $productRepository,
private MediaAttachmentServiceContract $attachmentService,
private readonly ProductRepository $productRepository,
private readonly ProductServiceContract $productService,
private readonly MediaAttachmentServiceContract $attachmentService,
) {}

public function index(ProductIndexRequest $request): JsonResource
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ProductIndexRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function rules(): array
}

return [
'search' => ['nullable', 'string', 'max:255'],
'search' => ['sometimes', 'string', 'max:255'],

'ids' => ['array'],
'ids.*' => ['uuid'],
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/ProductSearchValueListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use App\Events\ProductSearchValueEvent;
use App\Services\Contracts\ProductServiceContract;

class ProductSearchValueListener
readonly class ProductSearchValueListener
{
public function __construct(
private ProductServiceContract $productService,
) {}

public function handle(ProductSearchValueEvent $event): void
{
$this->productService->updateProductsSearchValues($event->getProductIds());
$this->productService->updateProductsSearchValues($event->product_ids);
}
}
4 changes: 0 additions & 4 deletions app/Models/Contracts/SortableContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ interface SortableContract
public function scopeSort(Builder $query, ?string $sortString = null): Builder;

public function getSortable(): array;

public function getDefaultSortBy(): string;

public function getDefaultSortDirection(): string;
}
3 changes: 0 additions & 3 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ class Order extends Model implements AuditableContract, SortableContract
'summary',
];

protected string $defaultSortBy = 'created_at';
protected string $defaultSortDirection = 'desc';

protected $casts = [
'paid' => 'boolean',
'invoice_requested' => 'boolean',
Expand Down
6 changes: 4 additions & 2 deletions app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Product extends Model implements AuditableContract, SortableContract
'purchase_limit_per_user',
'search_values',
];

protected array $auditInclude = [
'name',
'slug',
Expand All @@ -89,6 +90,7 @@ class Product extends Model implements AuditableContract, SortableContract
'price_max',
'available',
];

protected $casts = [
'shipping_date' => 'date',
'price' => 'float',
Expand All @@ -101,6 +103,7 @@ class Product extends Model implements AuditableContract, SortableContract
'shipping_digital' => 'bool',
'purchase_limit_per_user' => 'float',
];

protected array $sortable = [
'id',
'price',
Expand All @@ -114,6 +117,7 @@ class Product extends Model implements AuditableContract, SortableContract
'attribute.*',
'set.*',
];

protected array $criteria = [
'search' => ProductSearch::class,
'ids' => WhereInIds::class,
Expand All @@ -136,8 +140,6 @@ class Product extends Model implements AuditableContract, SortableContract
'has_schemas' => WhereHasSchemas::class,
'shipping_digital' => Equals::class,
];
protected string $defaultSortBy = 'products.created_at';
protected string $defaultSortDirection = 'desc';

public function sets(): BelongsToMany
{
Expand Down
3 changes: 0 additions & 3 deletions app/Models/WebHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ class WebHook extends Model implements AuditableContract, SortableContract
'updated_at',
];

protected string $defaultSortBy = 'created_at';
protected string $defaultSortDirection = 'desc';

public function logs(): HasMany
{
return $this->hasMany(WebHookEventLogEntry::class);
Expand Down
21 changes: 0 additions & 21 deletions app/Providers/RepositoryServiceProvider.php

This file was deleted.

11 changes: 0 additions & 11 deletions app/Repositories/Contracts/ProductRepositoryContract.php

This file was deleted.

3 changes: 1 addition & 2 deletions app/Repositories/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

use App\Dtos\ProductSearchDto;
use App\Models\Product;
use App\Repositories\Contracts\ProductRepositoryContract;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Gate;

class ProductRepository implements ProductRepositoryContract
class ProductRepository
{
public function search(ProductSearchDto $dto): LengthAwarePaginator
{
Expand Down
24 changes: 13 additions & 11 deletions app/Services/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ public function updateMinMaxPrices(Product $product): void

public function updateProductsSearchValues(array $productIds): void
{
Product::whereIn('id', $productIds)
->with(['tags', 'sets', 'relatedSets', 'attributes', 'attributes.options'])
Product::query()
->whereIn('id', $productIds)
->with(['tags', 'sets', 'attributes', 'attributes.options'])
->each(fn (Product $product) => $this->prepareProductSearchValues($product)->save());
}

Expand Down Expand Up @@ -297,22 +298,23 @@ private function bestMinMax(Collection $minmaxCol): array

private function prepareProductSearchValues(Product $product): Product
{
$searchValues = rtrim($product->name . ' ' . $product->description_html . ' ' . $product->description_short);
$searchValues .= rtrim(' ' . $product->tags->pluck('name')->implode(' '));
$searchValues .= rtrim(' ' . $product->allProductSet()->pluck('name')->implode(' '));
$searchValues .= rtrim(' ' . $product->relatedSets->pluck('name')->implode(' '));
$searchValues = [
...$product->tags->pluck('name'),
...$product->sets->pluck('name'),
];

/** @var Attribute $attribute */
foreach ($product->attributes as $attribute) {
$searchValues .= rtrim(' ' . $attribute->name);
$searchValues[] = $attribute->name;
/** @var AttributeOption $option */
foreach ($attribute->pivot->options as $option) {
$searchValues .= rtrim(' ' . $option->name . ' ' . $option->value_number . ' ' . $option->value_date);
$searchValues[] = $option->name;
$searchValues[] = $option->value_number;
$searchValues[] = $option->value_date;
}
}
$product->update([
'search_values' => $searchValues,
]);

$product->search_values = implode(' ', $searchValues);

return $product;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Traits/HasUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function boot(): void
{
static::creating(function ($model): void {
if (!$model->getKey()) {
$model->{$model->getKeyName()} = Str::uuid()->toString();
$model->{$model->getKeyName()} = Str::orderedUuid()->toString();
}
});

Expand Down
23 changes: 4 additions & 19 deletions app/Traits/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,17 @@ trait Sortable
{
public function scopeSort(Builder $query, ?string $sortString = null): Builder
{
if ($sortString !== null) {
$query = app(SortServiceContract::class)
->sort($query, $sortString, $this->getSortable());
if ($sortString === null) {
return $query;
}

return $query->orderBy(
$this->getDefaultSortBy(),
$this->getDefaultSortDirection(),
);
return app(SortServiceContract::class)
->sort($query, $sortString, $this->getSortable());
}

public function getSortable(): array
{
// @phpstan-ignore-next-line
return $this->sortable ?? [];
}

public function getDefaultSortBy(): string
{
// @phpstan-ignore-next-line
return $this->defaultSortBy ?? 'created_at';
}

public function getDefaultSortDirection(): string
{
// @phpstan-ignore-next-line
return $this->defaultSortDirection ?? 'asc';
}
}
1 change: 0 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@

// Application Service Providers...
App\Providers\AppServiceProvider::class,
App\Providers\RepositoryServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Expand Down
26 changes: 26 additions & 0 deletions database/migrations/2023_09_19_195946_update_products_indexes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('products', function (Blueprint $table): void {
$table->dropFullText('products_search_values_fulltext');
$table->fullText(
['search_values', 'slug', 'name', 'description_short', 'description_html'],
'products_fulltext',
);
});
}

public function down(): void
{
Schema::table('products', function (Blueprint $table): void {
$table->dropFullText('products_fulltext');
$table->fullText('search_values');
});
}
};
7 changes: 2 additions & 5 deletions tests/Feature/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,8 @@ public function testIndex($user): void
->json('GET', '/products', ['limit' => 100])
->assertOk()
->assertJsonCount(2, 'data')
->assertJson([
'data' => [
1 => $this->expected_short,
],
])->assertJsonFragment([
->assertJsonFragment([
...$this->expected_short,
'price_min' => $this->product->price_min,
'price_max' => $this->product->price_max,
]);
Expand Down
Loading

0 comments on commit f59a5d9

Please sign in to comment.