Aktuelle Einkaufsliste serverseitig merken (Fix iOS/PWA ohne Session)
Made-with: Cursor
This commit is contained in:
parent
f9993b4c73
commit
035c444dfb
@ -2,13 +2,16 @@
|
||||
|
||||
namespace App\Http\Controllers\Concerns;
|
||||
|
||||
use App\Models\ShoppingItem;
|
||||
use App\Models\ShoppingList;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
trait ResolvesCurrentShoppingList
|
||||
{
|
||||
protected function currentShoppingList(Request $request): ShoppingList
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $request->user();
|
||||
abort_if($user === null, 403);
|
||||
|
||||
@ -27,9 +30,29 @@ trait ResolvesCurrentShoppingList
|
||||
$sessionId = $request->session()->get('current_shopping_list_id');
|
||||
$current = $lists->firstWhere('id', $sessionId);
|
||||
|
||||
if ($current === null) {
|
||||
$persistedId = $user->last_active_shopping_list_id;
|
||||
$current = $lists->firstWhere('id', $persistedId);
|
||||
}
|
||||
|
||||
if ($current === null) {
|
||||
$listIds = $lists->pluck('id');
|
||||
$lastEditedListId = ShoppingItem::query()
|
||||
->whereIn('shopping_list_id', $listIds)
|
||||
->orderByDesc('updated_at')
|
||||
->orderByDesc('id')
|
||||
->value('shopping_list_id');
|
||||
$current = $lists->firstWhere('id', $lastEditedListId);
|
||||
}
|
||||
|
||||
if ($current === null) {
|
||||
$current = $lists->first();
|
||||
$request->session()->put('current_shopping_list_id', $current->id);
|
||||
}
|
||||
|
||||
$request->session()->put('current_shopping_list_id', $current->id);
|
||||
|
||||
if ((int) $user->last_active_shopping_list_id !== (int) $current->id) {
|
||||
$user->forceFill(['last_active_shopping_list_id' => $current->id])->saveQuietly();
|
||||
}
|
||||
|
||||
return $current;
|
||||
|
||||
@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@ -63,4 +64,9 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
{
|
||||
return $this->belongsToMany(ShoppingList::class, 'shopping_list_user')->withTimestamps();
|
||||
}
|
||||
|
||||
public function lastActiveShoppingList(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ShoppingList::class, 'last_active_shopping_list_id');
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->foreignId('last_active_shopping_list_id')
|
||||
->nullable()
|
||||
->after('remember_token')
|
||||
->constrained('shopping_lists')
|
||||
->nullOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropForeign(['last_active_shopping_list_id']);
|
||||
$table->dropColumn('last_active_shopping_list_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user