Skip to content

Commit 41898b8

Browse files
committed
Match complex ingredients in the filter
1 parent 2604141 commit 41898b8

File tree

7 files changed

+49
-45
lines changed

7 files changed

+49
-45
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
php-version: ['8.3', '8.4']
18+
php-version: ['8.4']
1919

2020
steps:
2121
- name: Install libvips
@@ -54,4 +54,4 @@ jobs:
5454

5555
- uses: stoplightio/spectral-action@latest
5656
with:
57-
file_glob: 'docs/*.yaml'
57+
file_glob: 'docs/*.yaml'

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- This will show cocktails that are locked for the bar (not all ingredients are in bar's shelf)
77
- Added `potential_bar_shelf_cocktails` sort to ingredients endpoint
88
- This will sort ingredients by how many new cocktails would be unlocked for the bar if the ingredient is added to bar shelf
9+
10+
## Changes
11+
- Docker image is now built on top of PHP 8.4 base image
912

1013
# v5.9.3
1114
## Fixes

app/External/Export/ToRecipeType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Kami\Cocktail\Models\Cocktail;
1010
use Illuminate\Support\Facades\Log;
1111
use Kami\RecipeUtils\UnitConverter\Units;
12-
use Kami\Cocktail\External\ExportTypeEnum;
1312
use Illuminate\Contracts\Filesystem\Cloud;
13+
use Kami\Cocktail\External\ExportTypeEnum;
1414
use Illuminate\Container\Attributes\Storage;
1515
use Kami\Cocktail\External\ForceUnitConvertEnum;
1616
use Kami\Cocktail\Exceptions\ImageFileNotFoundException;

app/Http/Filters/IngredientQueryFilter.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Kami\Cocktail\Http\Filters;
66

7+
use Illuminate\Support\Facades\DB;
78
use Kami\Cocktail\Models\Ingredient;
89
use Spatie\QueryBuilder\AllowedSort;
910
use Spatie\QueryBuilder\QueryBuilder;
@@ -96,17 +97,21 @@ public function __construct(IngredientService $ingredientQuery)
9697
'name',
9798
'created_at',
9899
'strength',
99-
AllowedSort::callback('potential_bar_shelf_cocktails', function ($query, bool $descending) use ($barMembership) {
100+
AllowedSort::callback('potential_bar_shelf_cocktails', function ($query, bool $descending) use ($barMembership, $ingredientQuery) {
100101
$direction = $descending ? 'DESC' : 'ASC';
101102

103+
$barIngredientIds = DB::table('bar_ingredients')
104+
->where('bar_id', $barMembership->bar_id)
105+
->pluck('ingredient_id')
106+
->toArray();
107+
$complexIngredientIds = $ingredientQuery->resolveComplexIngredients($barIngredientIds);
108+
109+
$ingredientIds = array_merge($barIngredientIds, $complexIngredientIds);
110+
102111
$query->selectRaw('ingredients.*, COUNT(DISTINCT ci.cocktail_id) AS potential_cocktails_count')
103-
->leftJoin('cocktail_ingredients AS ci', function ($join) use ($barMembership) {
112+
->leftJoin('cocktail_ingredients AS ci', function ($join) use ($ingredientIds) {
104113
$join->on('ci.ingredient_id', '=', 'ingredients.id')
105-
->whereNotIn('ci.ingredient_id', function ($subquery) use ($barMembership) {
106-
$subquery->select('ingredient_id')
107-
->from('bar_ingredients')
108-
->where('bar_id', $barMembership->bar_id);
109-
});
114+
->whereNotIn('ci.ingredient_id', $ingredientIds);
110115
})
111116
->groupBy('ingredients.id')
112117
->orderBy('potential_cocktails_count', $direction);

app/Scraper/Sites/CocktailsDistilled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function ingredients(): array
4848

4949
$this->crawler->filter('.ingredients ul li')->each(function ($listNode) use (&$result) {
5050
$ingredientName = $listNode->innerText();
51-
if (!isset($ingredientName) || (string) $ingredientName === '') {
51+
if ((string) $ingredientName === '') {
5252
$ingredientName = $listNode->filter('a')->text();
5353
}
5454

app/Services/CocktailService.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public function __construct(
2626
private DatabaseManager $db,
2727
private LogManager $log,
28+
private IngredientService $ingredientService,
2829
) {
2930
}
3031

@@ -261,23 +262,7 @@ public function getCocktailsByIngredients(array $ingredientIds, int $barId, ?int
261262
// Basically, goes through all ingredients to match ($ingredientIds) and check if they can create complex ingredients
262263
// If they can, that ingredient is added to the list of ingredients to match
263264
if ($matchComplexIngredients) {
264-
$additionalIngredients = $this->db->table('complex_ingredients AS ci')
265-
->distinct()
266-
->select('ci.main_ingredient_id')
267-
->join('ingredients AS i_main', 'ci.main_ingredient_id', '=', 'i_main.id')
268-
->whereIn('ci.id', function ($query) use ($ingredientIds) {
269-
$query->select('ci_inner.id')
270-
->from('complex_ingredients AS ci_inner')
271-
->whereNotExists(function ($query) use ($ingredientIds) {
272-
$query->select('i_ingredient.id')
273-
->from('complex_ingredients AS ci_sub')
274-
->join('ingredients AS i_ingredient', 'ci_sub.ingredient_id', '=', 'i_ingredient.id')
275-
->whereColumn('ci_sub.main_ingredient_id', 'ci_inner.main_ingredient_id')
276-
->whereNotIn('i_ingredient.id', $ingredientIds);
277-
});
278-
})
279-
->pluck('main_ingredient_id')
280-
->toArray();
265+
$additionalIngredients = $this->ingredientService->resolveComplexIngredients($ingredientIds);
281266

282267
$ingredientIds = array_merge($ingredientIds, $additionalIngredients);
283268
$ingredientIds = array_unique($ingredientIds);

app/Services/IngredientService.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,33 @@ public function rebuildMaterializedPath(int $barId): void
331331
$this->log->info('[INGREDIENT_SERVICE] Rebuilt materialized path for ingredients for bar ' . $barId . ' in ' . round($endTime - $startTime, 6) . ' seconds.');
332332
}
333333

334+
/**
335+
* Resolve complex ingredients that can be made with the given ingredient IDs.
336+
*
337+
* @param array<int> $ingredientIds
338+
* @return array<int>
339+
*/
340+
public function resolveComplexIngredients(array $ingredientIds): array
341+
{
342+
return $this->db->table('complex_ingredients AS ci')
343+
->distinct()
344+
->select('ci.main_ingredient_id')
345+
->join('ingredients AS i_main', 'ci.main_ingredient_id', '=', 'i_main.id')
346+
->whereIn('ci.id', function ($query) use ($ingredientIds) {
347+
$query->select('ci_inner.id')
348+
->from('complex_ingredients AS ci_inner')
349+
->whereNotExists(function ($query) use ($ingredientIds) {
350+
$query->select('i_ingredient.id')
351+
->from('complex_ingredients AS ci_sub')
352+
->join('ingredients AS i_ingredient', 'ci_sub.ingredient_id', '=', 'i_ingredient.id')
353+
->whereColumn('ci_sub.main_ingredient_id', 'ci_inner.main_ingredient_id')
354+
->whereNotIn('i_ingredient.id', $ingredientIds);
355+
});
356+
})
357+
->pluck('main_ingredient_id')
358+
->toArray();
359+
}
360+
334361
/**
335362
* Return array of all ingredients that are part of the user's shelf
336363
* and can be used to create cocktails.
@@ -366,23 +393,7 @@ public function getMemberIngredients(int $userId, int $barId): array
366393
->pluck('id')
367394
->toArray();
368395

369-
$complexIngredients = $this->db->table('complex_ingredients AS ci')
370-
->distinct()
371-
->select('ci.main_ingredient_id')
372-
->join('ingredients AS i_main', 'ci.main_ingredient_id', '=', 'i_main.id')
373-
->whereIn('ci.id', function ($query) use ($userIngredientIds) {
374-
$query->select('ci_inner.id')
375-
->from('complex_ingredients AS ci_inner')
376-
->whereNotExists(function ($query) use ($userIngredientIds) {
377-
$query->select('i_ingredient.id')
378-
->from('complex_ingredients AS ci_sub')
379-
->join('ingredients AS i_ingredient', 'ci_sub.ingredient_id', '=', 'i_ingredient.id')
380-
->whereColumn('ci_sub.main_ingredient_id', 'ci_inner.main_ingredient_id')
381-
->whereNotIn('i_ingredient.id', $userIngredientIds);
382-
});
383-
})
384-
->pluck('main_ingredient_id')
385-
->toArray();
396+
$complexIngredients = $this->resolveComplexIngredients($userIngredientIds);
386397

387398
return array_unique(array_merge(
388399
$descendantIngredientIds,

0 commit comments

Comments
 (0)