Skip to content

Commit

Permalink
Merge pull request #291 from karlomikus/develop
Browse files Browse the repository at this point in the history
Framework update, fixes
  • Loading branch information
karlomikus authored Jun 1, 2024
2 parents 8d1a69c + 89a19f7 commit b16971e
Show file tree
Hide file tree
Showing 60 changed files with 1,098 additions and 1,104 deletions.
2 changes: 1 addition & 1 deletion .env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DB_FOREIGN_KEYS=true
DB_DATABASE=":memory:"

SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://localhost:7700
MEILISEARCH_HOST=http://meilisearch:7700
MEILISEARCH_KEY=masterKeyThatIsReallyReallyLong4Real

SPEC_PATH=./docs
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
pull_request:
branches: [ "master" ]

env:
MEILISEARCH_HOST: http://localhost:7700

jobs:
laravel-tests:

Expand All @@ -31,10 +34,10 @@ jobs:
run: php artisan key:generate

- name: Check coding style
run: vendor/bin/ecs --clear-cache
run: ./vendor/bin/pint --test

- name: Execute tests
run: vendor/bin/phpunit
run: composer test

- name: Execute PHPStan
run: composer static
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# v3.16.0
## New
- Upgraded framework to Laravel 11
- Added `bar:merge-ingredients` command
- Used for merging multiple ingredients into one

## Changes
- Improved ImbibeMagazine scraper, now supports recipes in "older" format
- Optimized ingredients list endpoint DB queries
- Optimized similar cocktails list endpoint DB queries
- [Internal] Moved to Laravel Pint for code style
- [Internal] Imagick extension is now used int tests
- [Internal] Tests are now run in parallel in CI

## Fixes
- Fixed a crash when importing ingredients from array without sort attribute

# v3.15.0
## New
- Added bar settings
Expand Down
4 changes: 2 additions & 2 deletions UPCOMING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Breaking
- Remove collections share
- Encode images into export file
- Bump php to 8.3
- Bump laravel to 11
- Compress / compare
- Bump php to 8.3
106 changes: 106 additions & 0 deletions app/Console/Commands/BarMergeIngredient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Kami\Cocktail\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Kami\Cocktail\Models\Ingredient;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\search;
use function Laravel\Prompts\multisearch;

class BarMergeIngredient extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bar:merge-ingredients {barId : ID of bar to search ingredients in}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Merge multiple ingredients into one';

/**
* Execute the console command.
*/
public function handle(): int
{
$barId = $this->argument('barId');

$toIngredientId = search(
'Search ingredient',
fn (string $value) => strlen($value) > 0
? DB::table('ingredients')->orderBy('name')->where('bar_id', $barId)->where('name', 'like', '%' . $value . '%')->pluck('name', 'id')->toArray()
: [],
scroll: 10,
required: true
);

try {
$mergeToIngredient = Ingredient::findOrFail($toIngredientId);
} catch (\Throwable) {
$this->error('Cannot find ingredient with ID: ' . $toIngredientId);

return Command::FAILURE;
}

$listOfIngredientsToMerge = multisearch(
'Search for ingredients to merge into "' . $mergeToIngredient->name . '"',
fn (string $value) => strlen($value) > 0
? DB::table('ingredients')->orderBy('name')->where('bar_id', $barId)->where('name', 'like', '%' . $value . '%')->pluck('name', 'id')->toArray()
: [],
scroll: 15,
required: true
);

$numberOfChanges = DB::table('cocktail_ingredients')
->whereIn('ingredient_id', $listOfIngredientsToMerge)
->get('cocktail_id')
->unique('cocktail_id')
->count();

$possibleIngredients = Ingredient::find($listOfIngredientsToMerge);
$possibleIngredients = $possibleIngredients->filter(fn ($i) => (int) $i->id !== (int) $toIngredientId);

if ($possibleIngredients->isEmpty()) {
$this->error('Cannot find find any ingredients to merge.');

return Command::FAILURE;
}

$this->line('This will remove the following ingredients:');
foreach ($possibleIngredients as $ing) {
$this->line('' . $ing->name);
}
$this->line(sprintf('and merge them into "%s"', $mergeToIngredient->name));
$this->line(sprintf('This will change %s cocktail recipes', $numberOfChanges));
$this->line('');

if (!confirm('Continue with merging?')) {
return Command::INVALID;
}

spin(
function () use ($listOfIngredientsToMerge, $mergeToIngredient, $possibleIngredients) {
DB::table('cocktail_ingredients')->whereIn('ingredient_id', $listOfIngredientsToMerge)->update([
'ingredient_id' => $mergeToIngredient->id
]);

// Delete orphan ingredients
DB::table('ingredients')->whereIn('id', $possibleIngredients->pluck('id'))->delete();
},
'Merging ingredients...'
);

$this->info('Finished!');

return Command::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion app/External/CocktailIngredient.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function fromArray(array $sourceArray): self
$sourceArray['amount_max'] ?? null,
$sourceArray['note'] ?? null,
$substitutes,
$sourceArray['sort'],
$sourceArray['sort'] ?? 0,
);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/CocktailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function share(Request $request, string $idOrSlug): Response

public function similar(CocktailRepository $cocktailRepo, Request $request, string $idOrSlug): JsonResource
{
$cocktail = Cocktail::where('id', $idOrSlug)->orWhere('slug', $idOrSlug)->with('ingredients')->firstOrFail();
$cocktail = Cocktail::where('id', $idOrSlug)->orWhere('slug', $idOrSlug)->with('ingredients.ingredient')->firstOrFail();

if ($request->user()->cannot('show', $cocktail)) {
abort(403);
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
use AuthorizesRequests;
use DispatchesJobs;
use ValidatesRequests;

public function getJsonEncodingOptions(): int
{
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Resources/IngredientResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public function toArray($request)
}),
'created_user' => new UserBasicResource($this->whenLoaded('createdUser')),
'updated_user' => new UserBasicResource($this->whenLoaded('updatedUser')),
'access' => [
'access' => $this->when($this->relationLoaded('createdUser'), fn () => [
'can_edit' => $request->user()->can('edit', $this->resource),
'can_delete' => $request->user()->can('delete', $this->resource),
],
]),
];
}
}
5 changes: 4 additions & 1 deletion app/Jobs/ImportCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

class ImportCollection implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

/**
* Create a new job instance.
Expand Down
5 changes: 4 additions & 1 deletion app/Jobs/RefreshSearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

class RefreshSearchIndex implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

/**
* Create a new job instance.
Expand Down
5 changes: 4 additions & 1 deletion app/Jobs/SetupBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

class SetupBar implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

/**
* Create a new job instance.
Expand Down
5 changes: 4 additions & 1 deletion app/Jobs/StartRecipesExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

class StartRecipesExport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public function __construct(
private readonly int $barId,
Expand Down
3 changes: 2 additions & 1 deletion app/Mail/AccountDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

class AccountDeleted extends Mailable
{
use Queueable, SerializesModels;
use Queueable;
use SerializesModels;

/**
* Create a new message instance.
Expand Down
3 changes: 2 additions & 1 deletion app/Mail/ConfirmAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

class ConfirmAccount extends Mailable
{
use Queueable, SerializesModels;
use Queueable;
use SerializesModels;

public string $url;

Expand Down
3 changes: 2 additions & 1 deletion app/Mail/PasswordChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

class PasswordChanged extends Mailable
{
use Queueable, SerializesModels;
use Queueable;
use SerializesModels;

/**
* Create a new message instance.
Expand Down
3 changes: 2 additions & 1 deletion app/Mail/PasswordReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

class PasswordReset extends Mailable
{
use Queueable, SerializesModels;
use Queueable;
use SerializesModels;

/**
* Create a new message instance.
Expand Down
3 changes: 2 additions & 1 deletion app/Mail/SubscriptionChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

class SubscriptionChanged extends Mailable
{
use Queueable, SerializesModels;
use Queueable;
use SerializesModels;

/**
* Create a new message instance.
Expand Down
3 changes: 2 additions & 1 deletion app/Mail/UserSubscribed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

class UserSubscribed extends Mailable
{
use Queueable, SerializesModels;
use Queueable;
use SerializesModels;

/**
* Create a new message instance.
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

class Bar extends Model
{
use HasFactory, HasAuthors, HasSlug;
use HasFactory;
use HasAuthors;
use HasSlug;

protected $casts = [
'settings' => 'array',
Expand Down
16 changes: 8 additions & 8 deletions app/Models/Cocktail.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@

class Cocktail extends Model implements UploadableInterface
{
use HasFactory,
Searchable,
HasImages,
HasSlug,
HasRating,
HasNotes,
HasBarAwareScope,
HasAuthors;
use HasFactory;
use Searchable;
use HasImages;
use HasSlug;
use HasRating;
use HasNotes;
use HasBarAwareScope;
use HasAuthors;

protected $casts = [
'public_at' => 'datetime',
Expand Down
4 changes: 3 additions & 1 deletion app/Models/CocktailMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

class CocktailMethod extends Model
{
use HasFactory, HasBarAwareScope, HasAuthors;
use HasFactory;
use HasBarAwareScope;
use HasAuthors;

/**
* @return HasMany<Cocktail>
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

class Export extends Model
{
use HasFactory, HasAuthors, HasBarAwareScope;
use HasFactory;
use HasAuthors;
use HasBarAwareScope;

/**
* @return BelongsTo<Bar, Export>
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Glass.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

class Glass extends Model
{
use HasFactory, HasBarAwareScope, HasAuthors;
use HasFactory;
use HasBarAwareScope;
use HasAuthors;

/**
* @return Attribute<?float, ?float>
Expand Down
Loading

0 comments on commit b16971e

Please sign in to comment.