Skip to content

Commit

Permalink
Merge branch 'release/5.2' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bvlinsky committed Sep 25, 2023
2 parents be72800 + a208212 commit c658e27
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 355 deletions.
9 changes: 4 additions & 5 deletions app/Criteria/ProductSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ class ProductSearch extends Criterion
public function query(Builder $query): Builder
{
return $query->whereFullText([
'name',
'slug',
'description_html',
'description_short',
'search_values',
'products.name',
'products.description_html',
'products.description_short',
'products.search_values',
], $this->value);
}
}
2 changes: 1 addition & 1 deletion app/Dtos/OrderIndexDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function instantiateFromRequest(FormRequest|OrderIndexRequest $req
{
return new self(
search: $request->input('search'),
sort: $request->input('sort'),
sort: $request->input('sort', 'created_at:desc'),
status_id: $request->input('status_id'),
shipping_method_id: $request->input('shipping_method_id'),
);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function index(OrderIndexRequest $request): JsonResource
? $request->validated() + ['status.hidden' => 0] : $request->validated();

$query = Order::searchByCriteria($search_data)
->sort($request->input('sort'))
->sort($request->input('sort', 'created_at:desc'))
->with([
'products',
'discounts',
Expand Down
4 changes: 0 additions & 4 deletions app/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use App\Events\ProductSearchValueEvent;
use App\Http\Requests\TagCreateRequest;
use App\Http\Requests\TagIndexRequest;
use App\Http\Requests\TagUpdateRequest;
Expand Down Expand Up @@ -35,16 +34,13 @@ public function store(TagCreateRequest $request): JsonResource
public function update(Tag $tag, TagUpdateRequest $request): JsonResource
{
$tag->update($request->validated());
ProductSearchValueEvent::dispatch($tag->products->pluck('id')->toArray());

return TagResource::make($tag);
}

public function destroy(Tag $tag): HttpResponse
{
$products = $tag->products->pluck('id')->toArray();
$tag->delete();
ProductSearchValueEvent::dispatch($products);

return Response::noContent();
}
Expand Down
10 changes: 3 additions & 7 deletions app/Listeners/ProductSearchValueListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
namespace App\Listeners;

use App\Events\ProductSearchValueEvent;
use App\Services\Contracts\ProductServiceContract;
use Illuminate\Contracts\Queue\ShouldQueue;

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

public function handle(ProductSearchValueEvent $event): void
{
$this->productService->updateProductsSearchValues($event->product_ids);
// TODO: remove this listener
}
}
17 changes: 8 additions & 9 deletions app/Services/AttributeOptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace App\Services;

use App\Dtos\AttributeOptionDto;
use App\Events\ProductSearchValueEvent;
use App\Models\AttributeOption;
use App\Services\Contracts\AttributeOptionServiceContract;
use App\Services\Contracts\MetadataServiceContract;
use Heseya\Dto\Missing;

class AttributeOptionService implements AttributeOptionServiceContract
readonly class AttributeOptionService implements AttributeOptionServiceContract
{
public function __construct(private MetadataServiceContract $metadataService) {}
public function __construct(
private MetadataServiceContract $metadataService,
) {}

public function create(string $attributeId, AttributeOptionDto $dto): AttributeOption
{
Expand All @@ -22,7 +23,9 @@ public function create(string $attributeId, AttributeOptionDto $dto): AttributeO
],
$dto->toArray(),
);
$attributeOption = AttributeOption::create($data);

/** @var AttributeOption $attributeOption */
$attributeOption = AttributeOption::query()->create($data);

if (!($dto->getMetadata() instanceof Missing)) {
$this->metadataService->sync($attributeOption, $dto->getMetadata());
Expand All @@ -35,11 +38,9 @@ public function updateOrCreate(string $attributeId, AttributeOptionDto $dto): At
{
if ($dto->id !== null && !$dto->id instanceof Missing) {
/** @var AttributeOption $attributeOption */
$attributeOption = AttributeOption::findOrFail($dto->id);
$attributeOption = AttributeOption::query()->findOrFail($dto->id);
$attributeOption->update($dto->toArray());

ProductSearchValueEvent::dispatch($attributeOption->productAttributes->pluck('product_id')->toArray());

return $attributeOption;
}

Expand All @@ -48,9 +49,7 @@ public function updateOrCreate(string $attributeId, AttributeOptionDto $dto): At

public function delete(AttributeOption $attributeOption): void
{
$productIds = $attributeOption->productAttributes->pluck('product_id')->toArray();
$attributeOption->delete();
ProductSearchValueEvent::dispatch($productIds);
}

public function deleteAll(string $attributeId): void
Expand Down
4 changes: 0 additions & 4 deletions app/Services/AttributeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Dtos\AttributeDto;
use App\Enums\AttributeType;
use App\Events\ProductSearchValueEvent;
use App\Models\Attribute;
use App\Models\Product;
use App\Services\Contracts\AttributeOptionServiceContract;
Expand Down Expand Up @@ -35,18 +34,15 @@ public function create(AttributeDto $dto): Attribute
public function update(Attribute $attribute, AttributeDto $dto): Attribute
{
$attribute->update($dto->toArray());
ProductSearchValueEvent::dispatch($attribute->products->pluck('id')->toArray());

return $attribute;
}

public function delete(Attribute $attribute): void
{
$products = $attribute->products->pluck('id')->toArray();
$this->attributeOptionService->deleteAll($attribute->getKey());

$attribute->delete();
ProductSearchValueEvent::dispatch($products);
}

public function sync(Product $product, array $data): void
Expand Down
2 changes: 0 additions & 2 deletions app/Services/Contracts/ProductServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ public function getMinMaxPrices(Product $product): array;
* Updates minimum and maximum possible product price.
*/
public function updateMinMaxPrices(Product $product): void;

public function updateProductsSearchValues(array $productIds): void;
}
10 changes: 1 addition & 9 deletions app/Services/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

readonly class ProductService implements ProductServiceContract
final readonly class ProductService implements ProductServiceContract
{
public function __construct(
private MediaServiceContract $mediaService,
Expand Down Expand Up @@ -186,14 +186,6 @@ public function updateMinMaxPrices(Product $product): void
$this->discountService->applyDiscountsOnProduct($product);
}

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

private function assignItems(Product $product, ?array $items): void
{
$items = Collection::make($items)->mapWithKeys(fn (array $item): array => [
Expand Down
15 changes: 0 additions & 15 deletions app/Services/ProductSetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Dtos\ProductSetDto;
use App\Dtos\ProductSetUpdateDto;
use App\Dtos\ProductsReorderDto;
use App\Events\ProductSearchValueEvent;
use App\Events\ProductSetCreated;
use App\Events\ProductSetDeleted;
use App\Events\ProductSetUpdated;
Expand Down Expand Up @@ -117,8 +116,6 @@ public function create(ProductSetDto $dto): ProductSet
$this->metadataService->sync($set, $dto->metadata);
}

ProductSearchValueEvent::dispatch($set->allProductsIds()->toArray());

// searchable is handled by the event listener
ProductSetCreated::dispatch($set);

Expand Down Expand Up @@ -204,7 +201,6 @@ public function update(ProductSet $set, ProductSetUpdateDto $dto): ProductSet
'slug' => $slug,
'public_parent' => $publicParent,
]);
ProductSearchValueEvent::dispatch($productIds->toArray());

if (!($dto->getAttributesIds() instanceof Missing)) {
$attributes = Collection::make($dto->getAttributesIds());
Expand Down Expand Up @@ -233,23 +229,13 @@ public function reorder(array $sets, ProductSet|null $parent = null): void

public function attach(ProductSet $set, array $productsIds): Collection
{
$currentProducts = $set->products()->pluck('id');

$set->products()->sync($productsIds);

ProductSearchValueEvent::dispatch(
array_merge(
$currentProducts->diff($productsIds)->toArray(),
collect($productsIds)->diff($currentProducts)->toArray(),
),
);

return $set->products;
}

public function delete(ProductSet $set): void
{
$productIds = $set->allProductsIds()->merge($set->relatedProducts->pluck('id'));
if ($set->children()->count() > 0) {
$set->children->each(fn ($subset) => $this->delete($subset));
}
Expand All @@ -259,7 +245,6 @@ public function delete(ProductSet $set): void
if ($set->seo !== null) {
$this->seoMetadataService->delete($set->seo);
}
ProductSearchValueEvent::dispatch($productIds->toArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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'],
['search_values', 'name', 'description_short', 'description_html'],
'products_fulltext',
);
});
Expand Down
Loading

0 comments on commit c658e27

Please sign in to comment.