Skip to content

Commit

Permalink
Added search to payment methods, price maps and shipping methods
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL committed Oct 25, 2024
1 parent 67a11be commit e76fba3
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 4 deletions.
14 changes: 14 additions & 0 deletions app/Criteria/WhereHasNameLike.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Criteria;

use Heseya\Searchable\Criteria\Criterion;
use Illuminate\Database\Eloquent\Builder;

class WhereHasNameLike extends Criterion
{
public function query(Builder $query): Builder
{
return $query->where('name', 'LIKE', "%{$this->value}%");
}
}
5 changes: 3 additions & 2 deletions src/Application/PriceMap/Controllers/PriceMapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Product;
use Application\PriceMap\Requests\PriceMapListPricesRequest;
use Domain\PriceMap\Dtos\PriceMapCreateDto;
use Domain\PriceMap\Dtos\PriceMapIndexDto;
use Domain\PriceMap\Dtos\PriceMapPricesUpdateDto;
use Domain\PriceMap\Dtos\PriceMapProductPricesUpdateDto;
use Domain\PriceMap\Dtos\PriceMapSchemaPricesUpdateDto;
Expand All @@ -28,9 +29,9 @@ public function __construct(
private PriceMapService $priceMapService,
) {}

public function index(Request $request): HttpResponse
public function index(Request $request, PriceMapIndexDto $dto): HttpResponse
{
return $this->priceMapService->list()->toResponse($request);
return $this->priceMapService->list($dto)->toResponse($request);
}

public function store(Request $request, PriceMapCreateDto $dto): HttpResponse
Expand Down
1 change: 1 addition & 0 deletions src/Domain/PaymentMethods/Dtos/PaymentMethodIndexDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(
public readonly array|Optional $ids,
#[Uuid, Exists('sales_channels', 'id')]
public readonly Optional|string $sales_channel_id,
public readonly Optional|string $search,
) {}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Domain/PaymentMethods/Models/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Domain\PaymentMethods\Models;

use App\Criteria\WhereHasNameLike;
use App\Criteria\WhereHasOrderWithCode;
use App\Criteria\WhereHasSalesChannel;
use App\Criteria\WhereHasShippingMethod;
Expand Down Expand Up @@ -56,6 +57,7 @@ final class PaymentMethod extends Model
'alias',
'ids' => WhereInIds::class,
'sales_channel_id' => WhereHasSalesChannel::class,
'search' => WhereHasNameLike::class,
];

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Domain/PriceMap/Dtos/PriceMapIndexDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Domain\PriceMap\Dtos;

use Spatie\LaravelData\Data;
use Spatie\LaravelData\Optional;

final class PriceMapIndexDto extends Data
{
public function __construct(
public Optional|string $search,
) {}
}
8 changes: 8 additions & 0 deletions src/Domain/PriceMap/PriceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Domain\PriceMap;

use App\Criteria\WhereHasNameLike;
use App\Models\Model;
use Domain\Currency\Currency;
use Domain\PriceMap\Resources\PriceMapData;
use Domain\SalesChannel\Models\SalesChannel;
use Heseya\Searchable\Traits\HasCriteria;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\LaravelData\WithData;
Expand All @@ -17,6 +19,7 @@
*/
final class PriceMap extends Model
{
use HasCriteria;
use HasFactory;
/**
* @use WithData<PriceMapData>
Expand All @@ -38,6 +41,11 @@ final class PriceMap extends Model
'prices_generated' => 'bool',
];

/** @var string[] */
protected array $criteria = [
'search' => WhereHasNameLike::class,
];

/**
* @return HasMany<PriceMapProductPrice>
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Domain/PriceMap/PriceMapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Domain\Currency\Currency;
use Domain\Price\Dtos\PriceDto;
use Domain\PriceMap\Dtos\PriceMapCreateDto;
use Domain\PriceMap\Dtos\PriceMapIndexDto;
use Domain\PriceMap\Dtos\PriceMapPricesUpdateDto;
use Domain\PriceMap\Dtos\PriceMapPriceUpdateDto;
use Domain\PriceMap\Dtos\PriceMapProductPricesUpdateDto;
Expand Down Expand Up @@ -53,9 +54,9 @@
/**
* @return PaginatedDataCollection<int|string,PriceMapData>
*/
public function list(): PaginatedDataCollection
public function list(PriceMapIndexDto $dto): PaginatedDataCollection
{
return PriceMapData::collection(PriceMap::query()->paginate(Config::get('pagination.per_page')));
return PriceMapData::collection(PriceMap::searchByCriteria($dto->toArray())->paginate(Config::get('pagination.per_page')));
}

public function create(PriceMapCreateDto $dto): PriceMap
Expand Down
1 change: 1 addition & 0 deletions src/Domain/ShippingMethod/Dtos/ShippingMethodIndexDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __construct(
public array|Optional $metadata_private,
#[Uuid, Exists('sales_channels', 'id')]
public Optional|string $sales_channel_id,
public Optional|string $search,
#[ArrayType]
public array $items = [],
) {}
Expand Down
2 changes: 2 additions & 0 deletions src/Domain/ShippingMethod/Models/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Criteria\MetadataPrivateSearch;
use App\Criteria\MetadataSearch;
use App\Criteria\ShippingMethodItems;
use App\Criteria\WhereHasNameLike;
use App\Criteria\WhereHasSalesChannel;
use App\Criteria\WhereInIds;
use App\Enums\ShippingType;
Expand Down Expand Up @@ -85,6 +86,7 @@ final class ShippingMethod extends Model
'ids' => WhereInIds::class,
'items' => ShippingMethodItems::class,
'sales_channel_id' => WhereHasSalesChannel::class,
'search' => WhereHasNameLike::class,
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function index(ShippingMethodIndexDto $dto, ?Money $cartValue): LengthAwa
'ids',
'items',
'sales_channel_id',
'search',
)->toArray();

$query = ShippingMethod::query()
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/PaymentMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ public function testIndex($user): void
{
$this->{$user}->givePermissionTo('payment_methods.show');

$response = $this->actingAs($this->{$user})->json('GET', '/payment-methods', ['search' => $this->payment_method->name]);
$response
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonFragment(['id' => $this->payment_method->getKey()]);
}

/**
* @dataProvider authProvider
*/
public function testIndexSearch($user): void
{
$this->{$user}->givePermissionTo('payment_methods.show');

$response = $this->actingAs($this->{$user})->getJson('/payment-methods');
$response
->assertOk()
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/PriceMap/PriceMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public function testIndex(string $user): void
$response->assertOk()->assertJsonCount(1 + count(Currency::cases()), 'data');
}

/**
* @dataProvider authProvider
*/
public function testIndexSearch(string $user): void
{
$this->{$user}->givePermissionTo('price-maps.show');

$response = $this->actingAs($this->{$user})->json('GET', '/price-maps', ['search' => $this->priceMap->name]);

$response->assertOk()->assertJsonCount(1, 'data');
}

public function testCreateUnauthorized(): void
{
$this
Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/ShippingMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ public function testIndex($user): void
]);
}

/**
* @dataProvider authProvider
*/
public function testIndexSearch($user): void
{
$this->{$user}->givePermissionTo('shipping_methods.show');

$shippingMethod = ShippingMethod::factory()->create([
'public' => true,
'name' => 'Searched value',
]);

$this->actingAs($this->{$user})->json('GET', '/shipping-methods', ['search' => 'Searched value'])
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonFragment([
'id' => $shippingMethod->getKey(),
]);
}

/**
* @dataProvider authProvider
*/
Expand Down

0 comments on commit e76fba3

Please sign in to comment.