|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kami\Cocktail\Http\Filters; |
| 6 | + |
| 7 | +use Kami\Cocktail\Models\Bar; |
| 8 | +use Kami\Cocktail\Models\Cocktail; |
| 9 | +use Spatie\QueryBuilder\AllowedSort; |
| 10 | +use Spatie\QueryBuilder\QueryBuilder; |
| 11 | +use Spatie\QueryBuilder\AllowedFilter; |
| 12 | +use Spatie\QueryBuilder\AllowedInclude; |
| 13 | + |
| 14 | +/** |
| 15 | + * @extends \Spatie\QueryBuilder\QueryBuilder<Cocktail> |
| 16 | + */ |
| 17 | +final class PublicCocktailQueryFilter extends QueryBuilder |
| 18 | +{ |
| 19 | + public function __construct(Bar $bar) |
| 20 | + { |
| 21 | + parent::__construct(Cocktail::query()); |
| 22 | + |
| 23 | + $this |
| 24 | + ->allowedFilters([ |
| 25 | + AllowedFilter::exact('id'), |
| 26 | + AllowedFilter::custom('name', new FilterNameSearch()), |
| 27 | + AllowedFilter::partial('ingredient_name', 'ingredients.ingredient.name'), |
| 28 | + AllowedFilter::exact('ingredient_substitute_id', 'ingredients.substitutes.ingredient.id'), |
| 29 | + AllowedFilter::callback('ingredient_id', function ($query, $value) { |
| 30 | + if (!is_array($value)) { |
| 31 | + $value = [$value]; |
| 32 | + } |
| 33 | + |
| 34 | + $query->where(function ($q) use ($value) { |
| 35 | + $q->whereIn('ci.ingredient_id', $value)->orWhereIn('cis.ingredient_id', $value); |
| 36 | + }); |
| 37 | + }), |
| 38 | + AllowedFilter::exact('tag_id', 'tags.id'), |
| 39 | + AllowedFilter::exact('created_user_id'), |
| 40 | + AllowedFilter::exact('glass_id'), |
| 41 | + AllowedFilter::exact('cocktail_method_id'), |
| 42 | + AllowedFilter::callback('bar_shelf', function ($query, $value) use ($bar) { |
| 43 | + if ($value === true) { |
| 44 | + $query->whereIn('cocktails.id', $bar->getShelfCocktailsOnce()); |
| 45 | + } |
| 46 | + }), |
| 47 | + AllowedFilter::callback('abv_min', function ($query, $value) { |
| 48 | + $query->where('abv', '>=', $value); |
| 49 | + }), |
| 50 | + AllowedFilter::callback('abv_max', function ($query, $value) { |
| 51 | + $query->where('abv', '<=', $value); |
| 52 | + }), |
| 53 | + AllowedFilter::callback('main_ingredient_id', function ($query, $value) { |
| 54 | + if (!is_array($value)) { |
| 55 | + $value = [$value]; |
| 56 | + } |
| 57 | + |
| 58 | + $query->whereIn('ci.ingredient_id', $value)->where('sort', '=', 1); |
| 59 | + }), |
| 60 | + AllowedFilter::callback('total_ingredients', function ($query, $value) { |
| 61 | + $query->having('total_ingredients', '>=', (int) $value); |
| 62 | + }), |
| 63 | + AllowedFilter::exact('parent_cocktail_id'), |
| 64 | + ]) |
| 65 | + ->defaultSort('name') |
| 66 | + ->allowedSorts([ |
| 67 | + 'name', |
| 68 | + 'created_at', |
| 69 | + 'abv', |
| 70 | + 'total_ingredients', |
| 71 | + AllowedSort::callback('random', function ($query) { |
| 72 | + $query->inRandomOrder(); |
| 73 | + }), |
| 74 | + ]) |
| 75 | + ->allowedIncludes([ |
| 76 | + 'glass', |
| 77 | + 'method', |
| 78 | + 'user', |
| 79 | + 'utensils', |
| 80 | + 'images', |
| 81 | + 'tags', |
| 82 | + 'ingredients.ingredient', |
| 83 | + ]) |
| 84 | + ->select('cocktails.*') |
| 85 | + ->leftJoin('cocktail_ingredients AS ci', 'ci.cocktail_id', '=', 'cocktails.id') |
| 86 | + ->leftJoin('cocktail_ingredient_substitutes AS cis', 'cis.cocktail_ingredient_id', '=', 'ci.id') |
| 87 | + ->leftJoin('bar_ingredients AS bi', function ($query) { |
| 88 | + $query->on('bi.ingredient_id', '=', 'ci.ingredient_id'); |
| 89 | + }) |
| 90 | + ->where('cocktails.bar_id', $bar->id) |
| 91 | + ->groupBy('cocktails.id') |
| 92 | + ->with( |
| 93 | + 'bar.shelfIngredients', |
| 94 | + 'ingredients.ingredient.bar', |
| 95 | + 'tags', |
| 96 | + 'ingredients.substitutes.ingredient', |
| 97 | + 'glass', |
| 98 | + 'method', |
| 99 | + 'utensils', |
| 100 | + 'images', |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
0 commit comments