Skip to content

Commit

Permalink
improved meal import and create handling
Browse files Browse the repository at this point in the history
  • Loading branch information
robjuz committed Dec 1, 2024
1 parent 0f34b25 commit eb88ba4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 78 deletions.
4 changes: 3 additions & 1 deletion app/Casts/MealProviderCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Casts;

use App\MealProviders\AbstractMealProvider;
use App\MealProviders\Internal;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class MealProviderCast implements CastsAttributes
Expand All @@ -18,7 +19,8 @@ class MealProviderCast implements CastsAttributes
*/
public function get($model, string $key, $value, array $attributes)
{
return $value ? app()->make($value) : null;

return app()->make($value ?? basename(Internal::class));
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/OrderItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function store(Request $request): View|RedirectResponse|JsonResponse
]
);

/** @var Meal $meal */
$meal = Meal::findOrFail($request->input('meal_id'));

Gate::authorize('create', [OrderItem::class, $meal->date]);
Expand Down
31 changes: 31 additions & 0 deletions app/MealProviders/Internal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\MealProviders;

use App\MealProviders\Interfaces\HasWeeklyOrders;
use DiDom\Exceptions\InvalidSelectorException;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Carbon;

class Internal extends AbstractMealProvider implements HasWeeklyOrders
{
public function supportsAutoOrder(): bool
{
return false;
}

public function supportsOrderUpdate(): bool
{
return false;
}

public function getMealsDataForDate(Carbon $date): array
{
return [];
}

public function configureSchedule(Schedule $schedule): void
{
return;
}
}
4 changes: 3 additions & 1 deletion app/Models/Meal.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ public function order(User|int $user, $quantity = 1): OrderItem
if ($this->provider) {
$order = $this->provider->getOrder($this->date);
} else {
$order = Order::query()->firstOrCreate(['provider' => $this->getKey()], ['status' => Order::STATUS_OPEN]);
$order = Order::query()
->whereHas('meals', fn(Builder $query) => $query->whereDate('date', $this->date))
->firstOrCreate(['provider' => null], ['status' => Order::STATUS_OPEN]);
}

/** @var OrderItem $orderItem */
Expand Down
3 changes: 3 additions & 0 deletions app/Providers/MealProvidersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\MealProviders\Flaschenpost;
use App\MealProviders\Gourmetta;
use App\MealProviders\Holzke;
use App\MealProviders\Internal;
use Illuminate\Support\ServiceProvider;

class MealProvidersServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -34,6 +35,8 @@ public function register()
*/
public function boot()
{
Internal::register($this->app);

$registeredProviders = [];

foreach ($this->bundledProviders as $provider) {
Expand Down
74 changes: 0 additions & 74 deletions app/Services/MealService.php

This file was deleted.

2 changes: 1 addition & 1 deletion database/factories/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OrderFactory extends Factory
public function definition()
{
return [
'provider' => array_rand(app('mealProviders')),
'provider' => null,
];
}
}
4 changes: 3 additions & 1 deletion tests/Feature/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function it_provides_a_list_of_not_empty_orders()
$orderItem = OrderItem::factory()->make();

/** @var Meal $meal */
$meal = Meal::factory()->create(['provider'=> $order->provider->getKey()]);
$meal = Meal::factory()->create();

$orderItem->meal()->associate($meal);

Expand Down Expand Up @@ -162,7 +162,9 @@ public function it_shows_order_details()
{
Carbon::setTestNow(now()->startOfWeek()->addDays(2));

/** @var User $user1 */
$user1 = User::factory()->create();
/** @var User $user2 */
$user2 = User::factory()->create();

$user2->delete();
Expand Down

0 comments on commit eb88ba4

Please sign in to comment.