Skip to content

Commit 0c9e47f

Browse files
authored
Merge pull request #521 from karlomikus/develop
minor release
2 parents de08eb7 + 51a4678 commit 0c9e47f

23 files changed

+918
-170
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/public/uploads
66
/storage/*.key
77
/vendor
8+
.zed
89
.env
910
.env.backup
1011
.phpunit.result.cache

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# v5.8.0
2+
## New
3+
- Added `bars/{id}/sync-datapack` endpoint
4+
- This endpoint will sync existing bar data with the default datapack
5+
- Existing recipes and ingredients will not be overwritten, only new data will be added
6+
7+
## Changes
8+
- Recommendations now take into account bar shelf ingredients, recipe recency and negative tags
9+
110
# v5.7.0
211
## New
312
- Import parent cocktails via datapack

SECURITY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
| Version | Supported |
66
| ------- | ------------------ |
7-
| 3.x | :white_check_mark: |
8-
| < 2.x | :x: |
7+
| 5.x | :white_check_mark: |
8+
| 4.x | :white_check_mark: |
9+
| < 3.x | :x: |
910

1011
## Reporting a Vulnerability
1112

app/External/Import/FromDataPack.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ private function importCalculators(string $filepath, int $barId): void
125125
$data = [];
126126
}
127127

128+
$existing = DB::table('calculators')->select('name')->where('bar_id', $barId)->get()->keyBy(fn ($row) => strtolower($row->name))->toArray();
129+
$data = array_filter($data, function ($item) use ($existing) {
130+
if (array_key_exists(strtolower($item['name']), $existing)) {
131+
return false;
132+
}
133+
134+
return true;
135+
});
136+
128137
DB::beginTransaction();
129138
try {
130139
foreach ($data as $calculator) {
@@ -179,7 +188,7 @@ private function importIngredients(Filesystem $dataDisk, Bar $bar, User $user):
179188
foreach ($this->getDataFromDir('ingredients', $dataDisk) as $fromYield) {
180189
[$externalIngredient, $filePath] = $fromYield;
181190
$externalIngredient = IngredientExternal::fromDataPackArray($externalIngredient);
182-
if ($existingIngredients->has($externalIngredient->id)) {
191+
if ($existingIngredients->has(Str::slug($externalIngredient->name))) {
183192
continue;
184193
}
185194

@@ -236,6 +245,11 @@ private function importIngredients(Filesystem $dataDisk, Bar $bar, User $user):
236245

237246
Log::debug(sprintf('Ingredient image copy completed in %d ms', $imagesTimer * 1000));
238247

248+
if (empty($ingredientsToInsert)) {
249+
Log::debug('No ingredients to import');
250+
return;
251+
}
252+
239253
// Start inserting
240254
DB::beginTransaction();
241255

@@ -320,8 +334,7 @@ private function importBaseCocktails(Filesystem $dataDisk, Bar $bar, User $user)
320334
[$cocktail, $filePath] = $fromYield;
321335

322336
$externalCocktail = CocktailExternal::fromDataPackArray($cocktail);
323-
324-
if ($existingCocktails->has($externalCocktail->id)) {
337+
if ($existingCocktails->has(Str::slug($externalCocktail->name))) {
325338
continue;
326339
}
327340

@@ -425,6 +438,11 @@ private function importBaseCocktails(Filesystem $dataDisk, Bar $bar, User $user)
425438

426439
Log::debug(sprintf('Cocktail image copy completed in %d ms', $imagesTimer * 1000));
427440

441+
if (empty($newCocktails)) {
442+
Log::debug('No cocktails to import');
443+
return;
444+
}
445+
428446
DB::beginTransaction();
429447

430448
$tagsToInsert = [];

app/External/Model/Cocktail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ public function toDataPackArray(): array
123123
'glass' => $this->glass,
124124
'method' => $this->method,
125125
'utensils' => $this->utensils,
126-
'images' => array_map(fn ($model) => $model->toDataPackArray(), $this->images),
127-
'ingredients' => array_map(fn ($model) => $model->toDataPackArray(), $this->ingredients),
128126
'parent_cocktail_id' => $this->parentCocktailId,
129127
'year' => $this->year,
128+
'images' => array_map(fn ($model) => $model->toDataPackArray(), $this->images),
129+
'ingredients' => array_map(fn ($model) => $model->toDataPackArray(), $this->ingredients),
130130
];
131131
}
132132

app/External/Model/CocktailIngredient.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @param array<CocktailIngredientSubstitute> $substitutes
2020
*/
2121
private function __construct(
22-
public IngredientBasic $ingredient,
22+
public Ingredient $ingredient,
2323
public AmountValueObject $amount,
2424
public bool $optional = false,
2525
public bool $isSpecified = false,
@@ -41,7 +41,7 @@ public static function fromModel(CocktailIngredientModel $model, ?Units $toUnits
4141
}
4242

4343
return new self(
44-
IngredientBasic::fromModel($model->ingredient),
44+
Ingredient::fromModel($model->ingredient),
4545
$amount,
4646
(bool) $model->optional,
4747
(bool) $model->is_specified,
@@ -65,12 +65,9 @@ public static function fromDataPackArray(array $sourceArray): self
6565
}
6666

6767
return new self(
68-
IngredientBasic::fromDataPackArray([
68+
Ingredient::fromDataPackArray([
6969
'_id' => Str::slug($sourceArray['name']),
7070
'name' => $sourceArray['name'],
71-
'strength' => $sourceArray['strength'] ?? 0.0,
72-
'description' => $sourceArray['description'] ?? null,
73-
'origin' => $sourceArray['origin'] ?? null,
7471
]),
7572
new AmountValueObject(
7673
$sourceArray['amount'] ?? 0.0,
@@ -88,15 +85,16 @@ public static function fromDataPackArray(array $sourceArray): self
8885
public function toDataPackArray(): array
8986
{
9087
return [
91-
...$this->ingredient->toDataPackArray(),
88+
'_id' => $this->ingredient->id,
89+
'name' => $this->ingredient->name,
90+
'sort' => $this->sort,
9291
'amount' => $this->amount->amountMin,
9392
'units' => $this->amount->units->value,
9493
'optional' => $this->optional,
9594
'is_specified' => $this->isSpecified,
9695
'amount_max' => $this->amount->amountMax,
9796
'note' => $this->note,
9897
'substitutes' => array_map(fn ($model) => $model->toDataPackArray(), $this->substitutes),
99-
'sort' => $this->sort,
10098
];
10199
}
102100

@@ -114,7 +112,7 @@ public static function fromDraft2Array(array $sourceArray): self
114112
}
115113

116114
return new self(
117-
IngredientBasic::fromDraft2Array([
115+
Ingredient::fromDraft2Array([
118116
'_id' => $sourceArray['_id'],
119117
'name' => $sourceArray['name'] ?? '',
120118
'strength' => $sourceArray['strength'] ?? null,

app/External/Model/CocktailIngredientSubstitute.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
readonly class CocktailIngredientSubstitute implements SupportsDataPack, SupportsDraft2
1616
{
1717
private function __construct(
18-
public IngredientBasic $ingredient,
18+
public Ingredient $ingredient,
1919
public ?AmountValueObject $amount = null,
2020
) {
2121
}
@@ -29,20 +29,17 @@ public static function fromModel(CocktailIngredientSubstituteModel $model, ?Unit
2929
}
3030

3131
return new self(
32-
IngredientBasic::fromModel($model->ingredient),
32+
Ingredient::fromModel($model->ingredient),
3333
$amount,
3434
);
3535
}
3636

3737
public static function fromDataPackArray(array $sourceArray): self
3838
{
3939
return new self(
40-
IngredientBasic::fromDataPackArray([
40+
Ingredient::fromDataPackArray([
4141
'_id' => Str::slug($sourceArray['name']),
4242
'name' => $sourceArray['name'],
43-
'strength' => $sourceArray['strength'] ?? 0.0,
44-
'description' => $sourceArray['description'] ?? null,
45-
'origin' => $sourceArray['origin'] ?? null,
4643
]),
4744
new AmountValueObject(
4845
$sourceArray['amount'] ?? 0.0,
@@ -55,7 +52,8 @@ public static function fromDataPackArray(array $sourceArray): self
5552
public function toDataPackArray(): array
5653
{
5754
return [
58-
...$this->ingredient->toDataPackArray(),
55+
'_id' => $this->ingredient->id,
56+
'name' => $this->ingredient->name,
5957
'amount' => $this->amount->amountMin <= 0.0 ? null : $this->amount->amountMin,
6058
'units' => $this->amount->units->value === '' ? null : $this->amount->units->value,
6159
'amount_max' => $this->amount->amountMax,
@@ -65,7 +63,7 @@ public function toDataPackArray(): array
6563
public static function fromDraft2Array(array $sourceArray): self
6664
{
6765
return new self(
68-
IngredientBasic::fromDraft2Array([
66+
Ingredient::fromDraft2Array([
6967
'_id' => $sourceArray['_id'],
7068
'name' => $sourceArray['name'] ?? '',
7169
]),

app/External/Model/Ingredient.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
namespace Kami\Cocktail\External\Model;
66

77
use Kami\Cocktail\External\SupportsCSV;
8+
use Kami\Cocktail\External\SupportsDraft2;
89
use Kami\Cocktail\Models\ComplexIngredient;
910
use Kami\Cocktail\External\SupportsDataPack;
1011
use Kami\Cocktail\Models\Image as ImageModel;
1112
use Kami\Cocktail\Models\Ingredient as IngredientModel;
1213
use Kami\Cocktail\Models\IngredientPrice as IngredientPriceModel;
1314

14-
readonly class Ingredient implements SupportsDataPack, SupportsCSV
15+
readonly class Ingredient implements SupportsDataPack, SupportsDraft2, SupportsCSV
1516
{
1617
/**
1718
* @param array<Image> $images
18-
* @param array<IngredientBasic> $ingredientParts
19+
* @param array<Ingredient> $ingredientParts
1920
* @param array<IngredientPrice> $prices
2021
*/
2122
private function __construct(
@@ -47,7 +48,7 @@ public static function fromModel(IngredientModel $model, bool $useFileURI = fals
4748
})->toArray();
4849

4950
$ingredientParts = $model->ingredientParts->map(function (ComplexIngredient $part) {
50-
return IngredientBasic::fromModel($part->ingredient);
51+
return Ingredient::fromModel($part->ingredient);
5152
})->toArray();
5253

5354
$ingredientPrices = $model->prices->map(function (IngredientPriceModel $price) {
@@ -85,7 +86,12 @@ public static function fromDataPackArray(array $sourceArray): self
8586

8687
$ingredientParts = [];
8788
foreach ($sourceArray['ingredient_parts'] ?? [] as $ingredient) {
88-
$ingredientParts[] = IngredientBasic::fromDataPackArray($ingredient);
89+
$ingredientParts[] = Ingredient::fromDataPackArray($ingredient);
90+
}
91+
92+
$ingredientPrices = [];
93+
foreach ($sourceArray['prices'] ?? [] as $price) {
94+
$ingredientPrices[] = IngredientPrice::fromDataPackArray($price);
8995
}
9096

9197
return new self(
@@ -101,7 +107,7 @@ public static function fromDataPackArray(array $sourceArray): self
101107
updatedAt: $sourceArray['updated_at'] ?? null,
102108
images: $images,
103109
ingredientParts: $ingredientParts,
104-
prices: [],
110+
prices: $ingredientPrices,
105111
calculatorId: $sourceArray['calculator_id'] ?? null,
106112
sugarContent: $sourceArray['sugar_g_per_ml'] ?? null,
107113
acidity: $sourceArray['acidity'] ?? null,
@@ -158,4 +164,28 @@ public static function fromCSV(array $sourceArray): self
158164
units: blank($sourceArray['units']) ? null : $sourceArray['units'],
159165
);
160166
}
167+
168+
public static function fromDraft2Array(array $sourceArray): self
169+
{
170+
return new self(
171+
id: $sourceArray['_id'],
172+
name: $sourceArray['name'] ?? '',
173+
strength: $sourceArray['strength'] ?? 0.0,
174+
description: $sourceArray['description'] ?? null,
175+
origin: $sourceArray['origin'] ?? null,
176+
category: $sourceArray['category'] ?? null,
177+
);
178+
}
179+
180+
public function toDraft2Array(): array
181+
{
182+
return [
183+
'_id' => $this->id,
184+
'name' => $this->name,
185+
'strength' => $this->strength,
186+
'description' => $this->description,
187+
'origin' => $this->origin,
188+
'category' => $this->category,
189+
];
190+
}
161191
}

app/External/Model/IngredientBasic.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

app/External/Model/Schema.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public const SCHEMA_URL = 'https://barassistant.app/cocktail-02.schema.json';
2020

2121
/**
22-
* @param array<IngredientBasic> $ingredients
22+
* @param array<Ingredient> $ingredients
2323
*/
2424
public function __construct(
2525
public Cocktail $cocktail,
@@ -31,9 +31,9 @@ public static function fromCocktailModel(CocktailModel $model, ?Units $toUnits =
3131
{
3232
$ingredients = [];
3333
foreach ($model->ingredients as $cocktailIngredient) {
34-
$ingredients[] = IngredientBasic::fromModel($cocktailIngredient->ingredient);
34+
$ingredients[] = Ingredient::fromModel($cocktailIngredient->ingredient);
3535
foreach ($cocktailIngredient->substitutes as $substitute) {
36-
$ingredients[] = IngredientBasic::fromModel($substitute->ingredient);
36+
$ingredients[] = Ingredient::fromModel($substitute->ingredient);
3737
}
3838
}
3939

@@ -47,7 +47,7 @@ public static function fromDraft2Array(array $source): self
4747
{
4848
return new self(
4949
Cocktail::fromDraft2Array($source['recipe']),
50-
array_map(fn ($ingredient) => IngredientBasic::fromDraft2Array($ingredient), $source['ingredients']),
50+
array_map(fn ($ingredient) => Ingredient::fromDraft2Array($ingredient), $source['ingredients']),
5151
);
5252
}
5353

0 commit comments

Comments
 (0)