Skip to content

Commit

Permalink
Add use_parent_as_substitute column
Browse files Browse the repository at this point in the history
  • Loading branch information
karlomikus committed Jan 1, 2024
1 parent c5fbb15 commit b3df109
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 29 deletions.
3 changes: 0 additions & 3 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=
MEILISEARCH_KEY=

# [Experimental] Use parent ingredient as substitutes GH#95
PARENT_INGREDIENT_SUBSTITUTE=false

# Mail
# MAIL_MAILER=
# MAIL_HOST=
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# v3.7.0
## New
- Added `missing_ingredients` cocktails filter
- Added `use_parent_as_substitute` setting to bar memberships
- With this you can toggle using parent ingredient of a specific ingredient as a substitute in your shelf
- Before, this was available via experimental `PARENT_INGREDIENT_SUBSTITUTE` env variable

## Changes
- Removed `PARENT_INGREDIENT_SUBSTITUTE` env variable
- Update login error message depending on mail confirmation config
- Update min log level to "warning"

# v3.6.0
## New
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function update(UpdateUserRequest $request): JsonResource
$barMembership = $currentUser->getBarMembership((int) $barId);
if ($barMembership) {
$barMembership->is_shelf_public = (bool) $request->post('is_shelf_public');
$barMembership->use_parent_as_substitute = (bool) $request->post('use_parent_as_substitute');
$barMembership->save();
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/ShelfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function cocktails(CocktailRepository $cocktailRepo, Request $request): J

$cocktailIds = $cocktailRepo->getCocktailsByIngredients(
$barMembership->userIngredients->pluck('ingredient_id')->toArray(),
$limit
$limit,
$barMembership->use_parent_as_substitute,
);

return response()->json([
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/StatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function index(CocktailRepository $cocktailRepo, Request $request): JsonR
$stats['total_ingredients'] = Ingredient::where('bar_id', $bar->id)->count();
$stats['total_favorited_cocktails'] = CocktailFavorite::where('bar_membership_id', $barMembership->id)->count();
$stats['total_shelf_cocktails'] = $cocktailRepo->getCocktailsByIngredients(
$barMembership->userIngredients->pluck('ingredient_id')->toArray()
$barMembership->userIngredients->pluck('ingredient_id')->toArray(),
useParentIngredientAsSubstitute: $barMembership->use_parent_as_substitute,
)->count();
$stats['total_shelf_ingredients'] = UserIngredient::where('bar_membership_id', $barMembership->id)->count();
$stats['most_popular_ingredients'] = $popularIngredientIds;
Expand Down
19 changes: 13 additions & 6 deletions app/Http/Filters/CocktailQueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function __construct(CocktailRepository $cocktailRepo)

$barMembership = $this->request->user()->getBarMembership(bar()->id);

$useParentIngredientAsSubstitute = $barMembership->use_parent_as_substitute;

$this
->allowedFilters([
AllowedFilter::exact('id'),
Expand Down Expand Up @@ -50,14 +52,15 @@ public function __construct(CocktailRepository $cocktailRepo)
$query->userFavorites($barMembership->id);
}
}),
AllowedFilter::callback('on_shelf', function ($query, $value) use ($cocktailRepo) {
AllowedFilter::callback('on_shelf', function ($query, $value) use ($cocktailRepo, $useParentIngredientAsSubstitute) {
if ($value === true) {
$query->whereIn('cocktails.id', $cocktailRepo->getCocktailsByIngredients(
$this->request->user()->getShelfIngredients(bar()->id)->pluck('ingredient_id')->toArray()
$this->request->user()->getShelfIngredients(bar()->id)->pluck('ingredient_id')->toArray(),
useParentIngredientAsSubstitute: $useParentIngredientAsSubstitute,
));
}
}),
AllowedFilter::callback('user_shelves', function ($query, $value) use ($cocktailRepo) {
AllowedFilter::callback('user_shelves', function ($query, $value) use ($cocktailRepo, $useParentIngredientAsSubstitute) {
if (!is_array($value)) {
$value = [$value];
}
Expand All @@ -71,15 +74,19 @@ public function __construct(CocktailRepository $cocktailRepo)
->get();

$query->whereIn('cocktails.id', $cocktailRepo->getCocktailsByIngredients(
$ingredients->pluck('ingredient_id')->toArray()
$ingredients->pluck('ingredient_id')->toArray(),
useParentIngredientAsSubstitute: $useParentIngredientAsSubstitute,
));
}),
AllowedFilter::callback('shelf_ingredients', function ($query, $value) use ($cocktailRepo) {
AllowedFilter::callback('shelf_ingredients', function ($query, $value) use ($cocktailRepo, $useParentIngredientAsSubstitute) {
if (!is_array($value)) {
$value = [$value];
}

$query->whereIn('cocktails.id', $cocktailRepo->getCocktailsByIngredients($value));
$query->whereIn('cocktails.id', $cocktailRepo->getCocktailsByIngredients(
$value,
useParentIngredientAsSubstitute: $useParentIngredientAsSubstitute
));
}),
AllowedFilter::callback('is_public', function ($query, $value) {
if ($value === true) {
Expand Down
1 change: 1 addition & 0 deletions app/Models/BarMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class BarMembership extends Model
{
protected $casts = [
'is_shelf_public' => 'boolean',
'use_parent_as_substitute' => 'boolean',
];

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Repository/CocktailRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public function __construct(private DatabaseManager $db)
* @param array<int> $ingredientIds
* @return \Illuminate\Support\Collection<string, mixed>
*/
public function getCocktailsByIngredients(array $ingredientIds, ?int $limit = null): Collection
public function getCocktailsByIngredients(array $ingredientIds, ?int $limit = null, bool $useParentIngredientAsSubstitute = false): Collection
{
$query = $this->db->table('cocktails AS c')
->select('c.id')
->join('cocktail_ingredients AS ci', 'ci.cocktail_id', '=', 'c.id')
->leftJoin('cocktail_ingredient_substitutes AS cis', 'cis.cocktail_ingredient_id', '=', 'ci.id')
->where('optional', false);

if (config('bar-assistant.parent_ingredient_as_substitute')) {
if ($useParentIngredientAsSubstitute) {
$query->join('ingredients AS i', function ($join) {
$join->on('i.id', '=', 'ci.ingredient_id')->orOn('i.id', '=', 'i.parent_ingredient_id');
})
Expand Down
16 changes: 0 additions & 16 deletions config/bar-assistant.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,6 @@
true
),

/*
|--------------------------------------------------------------------------
| [Experimental] Use parent ingredient as substitutes GH#95
|--------------------------------------------------------------------------
|
| This option will modify how ingredients for cocktails you can make are
| shown. Enabling this option will use ingredients parent ingredient
| as a possible substitute.
|
*/

'parent_ingredient_as_substitute' => env(
'PARENT_INGREDIENT_SUBSTITUTE',
false
),

/*
|--------------------------------------------------------------------------
| Max bars per user
Expand Down
28 changes: 28 additions & 0 deletions database/migrations/2024_01_01_145353_add_parent_sub_column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('bar_memberships', function (Blueprint $table) {
$table->boolean('use_parent_as_substitute')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('bar_memberships', function (Blueprint $table) {
$table->dropColumn('use_parent_as_substitute');
});
}
};
3 changes: 3 additions & 0 deletions docs/open-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3040,6 +3040,9 @@ components:
is_shelf_public:
type: boolean
example: false
use_parent_as_substitute:
type: boolean
example: false
UserRequest:
type: object
required:
Expand Down

0 comments on commit b3df109

Please sign in to comment.