Skip to content

Commit

Permalink
Merge pull request #945 from SlovakNationalGallery/frontends
Browse files Browse the repository at this point in the history
[item] frontends
  • Loading branch information
rastislav-chynoransky authored Feb 21, 2024
2 parents c1be271 + 0e8cf52 commit f2bbefc
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

services:
mysql:
image: mysql:5.7
image: mysql:8.0.36
env:
MYSQL_DATABASE: webumenia_test
MYSQL_ALLOW_EMPTY_PASSWORD: yes
Expand Down
52 changes: 33 additions & 19 deletions app/Elasticsearch/Repositories/ItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Elasticsearch\Repositories;

use App\Authority;
use App\Facades\Frontend;
use App\Filter\Contracts\Filter;
use App\Filter\Contracts\SearchRequest;
use App\IntegerRange;
Expand Down Expand Up @@ -67,14 +68,21 @@ public function getSuggestions(int $size, string $search, string $locale = null)
'size' => $size,
'body' => [
'query' => [
'multi_match' => [
'query' => $search,
'type' => 'cross_fields',
'fields' => ['identifier', 'title.suggest', 'author.suggest'],
'operator' => 'and',
]
]
]
'bool' => [
'must' => [
'multi_match' => [
'query' => $search,
'type' => 'cross_fields',
'fields' => ['identifier', 'title.suggest', 'author.suggest'],
'operator' => 'and',
],
],
'filter' => [
['term' => ['frontend' => Frontend::get()]],
],
],
],
],
]);

return $this->createSearchResult($response);
Expand Down Expand Up @@ -121,7 +129,10 @@ public function getSimilar(int $size, Model $model, $locale = null): SearchResul
[
'term' => ['has_iip' => true]
]
]
],
'filter' => [
['term' => ['frontend' => Frontend::get()]],
],
]
];

Expand Down Expand Up @@ -184,7 +195,7 @@ public function getSimilarByColor(int $size, Item $item, $locale = null): Search
]
]
]
]
],
]
];
}
Expand All @@ -198,6 +209,7 @@ public function getSimilarByColor(int $size, Item $item, $locale = null): Search
}
$query['bool']['must_not'][]['term']['id'] = $item->id;
$query['bool']['minimum_should_match'] = '-30%';
$query['bool']['filter'][]['term']['frontend'] = Frontend::get();

$response = $this->elasticsearch->search([
'index' => $this->getLocalizedIndexName($locale),
Expand All @@ -224,6 +236,9 @@ public function getPreviewItems(int $size, Authority $authority, string $locale
['term' => ['has_iip' => true]],
],
],
'filter' => [
['term' => ['frontend' => Frontend::get()]],
],
],
'sort' => [
'_score',
Expand All @@ -237,16 +252,15 @@ public function getPreviewItems(int $size, Authority $authority, string $locale

public function buildQueryFromFilter(?Filter $filter): ?array
{
if (!$filter) {
return null;
}

$query = [];
$query = $this->addFilterablesQuery($query, $filter);
$query = $this->addSearchQuery($query, $filter->get('search'));
$query = $this->addYearsQuery($query, $filter->get('years'));
$query = $this->addColorQuery($query, $filter->get('color'));
return $query ?: null;
$query['bool']['filter'][]['term']['frontend'] = Frontend::get();
if ($filter) {
$query = $this->addFilterablesQuery($query, $filter);
$query = $this->addSearchQuery($query, $filter->get('search'));
$query = $this->addYearsQuery($query, $filter->get('years'));
$query = $this->addColorQuery($query, $filter->get('color'));
}
return $query;
}

protected function addSearchQuery(array $query, ?string $search): array
Expand Down
8 changes: 8 additions & 0 deletions app/Enums/FrontendEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Enums;

enum FrontendEnum: string
{
case WEBUMENIA = 'webumenia';
}
13 changes: 13 additions & 0 deletions app/Facades/Frontend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Frontend extends Facade
{
protected static function getFacadeAccessor()
{
return \App\Services\Frontend::class;
}
}
22 changes: 6 additions & 16 deletions app/Http/Controllers/Api/V1/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api\V1;

use App\Elasticsearch\Repositories\ItemRepository;
use App\Facades\Frontend;
use App\Http\Controllers\Controller;
use App\Item;
use ElasticScoutDriverPlus\Exceptions\QueryBuilderException;
Expand Down Expand Up @@ -55,11 +56,7 @@ public function index(Request $request)
$size = (int) $request->get('size', 1);
$q = (string) $request->get('q');

try {
$query = $this->createQueryBuilder($q, $filter)->buildQuery();
} catch (QueryBuilderException $e) {
$query = ['match_all' => new \stdClass()];
}
$query = $this->createQueryBuilder($q, $filter)->buildQuery();

if (array_key_exists('random', $sort)) {
$query = ItemRepository::buildRandomSortQuery($query, $request->get('page', 1) == 1);
Expand Down Expand Up @@ -180,13 +177,9 @@ public function detail(Request $request, $id)
$filter = (array) $request->get('filter');
$q = (string) $request->get('q');

if ($q || $filter) {
$query = $this->createQueryBuilder($q, $filter)
->must(Query::ids()->values([$id]))
->buildQuery();
} else {
$query = Query::ids()->values([$id]);
}
$query = $this->createQueryBuilder($q, $filter)
->must(Query::ids()->values([$id]))
->buildQuery();

$items = Item::searchQuery($query)->execute();

Expand All @@ -199,10 +192,6 @@ public function detail(Request $request, $id)

protected function createQueryBuilder($q, $filter)
{
if (empty($q) && empty($filter)) {
return Query::matchAll();
}

$builder = Query::bool();

if ($q) {
Expand Down Expand Up @@ -248,6 +237,7 @@ protected function createQueryBuilder($q, $filter)
}
}

$builder->filter(['term' => ['frontend' => Frontend::get()]]);
return $builder;
}
}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Kernel extends HttpKernel
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\AcceptLanguage::class,
\App\Http\Middleware\ConfigureFrontendFromRequest::class,
\App\Http\Middleware\ApplyFrontendScope::class,
],
];

Expand Down
18 changes: 18 additions & 0 deletions app/Http/Middleware/ApplyFrontendScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Middleware;

use App\Facades\Frontend;
use App\Item;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ApplyFrontendScope
{
public function handle(Request $request, \Closure $next): Response
{
Item::addGlobalScope('frontend', fn ($query) => $query->whereJsonContains('frontends', Frontend::get()));

return $next($request);
}
}
22 changes: 22 additions & 0 deletions app/Http/Middleware/ConfigureFrontendFromRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Middleware;

use App\Enums\FrontendEnum;
use App\Facades\Frontend;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ConfigureFrontendFromRequest
{
public function handle(Request $request, Closure $next): Response
{
$frontend = FrontendEnum::tryFrom($request->header('X-Frontend'));
if ($frontend) {
Frontend::set($frontend);
}

return $next($request);
}
}
4 changes: 2 additions & 2 deletions app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Chelout\RelationshipEvents\Concerns\HasBelongsToManyEvents;
use ElasticScoutDriverPlus\Searchable;
use ElasticScoutDriverPlus\Support\Query;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\URL;
Expand Down Expand Up @@ -123,6 +121,7 @@ class Item extends Model implements IndexableModel, TranslatableContract

protected $casts = [
'colors' => 'json',
'frontends' => 'array',
];

protected $observables = [
Expand Down Expand Up @@ -765,6 +764,7 @@ public function getIndexedData($locale)
];
})
->values(),
'frontend' => $this->frontends,
];
}

Expand Down
4 changes: 4 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

use App\Elasticsearch\Repositories\AuthorityRepository;
use App\Elasticsearch\Repositories\ItemRepository;
use App\Enums\FrontendEnum;
use App\Filter\Forms\Types\AuthoritySearchRequestType;
use App\Filter\Forms\Types\ItemSearchRequestType;
use App\Harvest\Importers\ItemImporter;
use App\Harvest\Mappers\BaseAuthorityMapper;
use App\Services\Frontend;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Blade;
Expand Down Expand Up @@ -45,6 +47,8 @@ public function register()
->give(function () {
return new BaseAuthorityMapper();
});

$this->app->singleton(Frontend::class, fn () => new Frontend(FrontendEnum::WEBUMENIA));
}

public function boot()
Expand Down
22 changes: 22 additions & 0 deletions app/Services/Frontend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Services;

use App\Enums\FrontendEnum;

class Frontend
{
public function __construct(private FrontendEnum $current)
{
}

public function set(FrontendEnum $frontend): void
{
$this->current = $frontend;
}

public function get(): FrontendEnum
{
return $this->current;
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Enums\FrontendEnum;
use Illuminate\Support\Facades\Facade;

return [
Expand Down
3 changes: 3 additions & 0 deletions config/elasticsearch/mapping/items.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@
'location' => [
'type' => 'keyword',
],
'frontend' => [
'type' => 'keyword',
],
],
];

Expand Down
1 change: 1 addition & 0 deletions database/factories/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function definition()
],
'created_at' => fake()->date,
'updated_at' => fake()->date,
'frontends' => ["webumenia"],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement("ALTER TABLE items ADD COLUMN frontends JSON DEFAULT (JSON_ARRAY('webumenia'))");
DB::statement('CREATE INDEX items_frontends_index ON items ( (CAST(frontends AS CHAR(32) ARRAY)) )');
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('items', function (Blueprint $table) {
$table->dropIndex('items_frontends_index');
$table->dropColumn('frontends');
});
}
};
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
REDIS_HOST: redis

mysql:
image: mysql:5.7
image: mysql:8.0.36
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
Expand Down
Loading

0 comments on commit f2bbefc

Please sign in to comment.