Compare commits

..

2 Commits

4 changed files with 67 additions and 1 deletions

View File

@ -0,0 +1,10 @@
---
description: Git für dieses Repo immer per HTTPS (origin), kein SSH-Remote vorschlagen
alwaysApply: true
---
# Git: HTTPS für Pauker-Remote
- **Remote:** `origin` = `https://git.pauker.at/Stefan/Einkaufsliste.git` — Push/Pull immer über diese HTTPS-URL, Remote nicht auf SSH (`git@…`) umstellen.
- **Agent:** Bei `git push` / `git pull` keine SSH-Alternative vorschlagen, nur HTTPS beibehalten.
- **Auth-Fehler:** Hinweis auf Anmeldung, Personal Access Token oder Credential Manager unter HTTPS; nicht „auf SSH wechseln“ als Standard-Fix.

View File

@ -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;

View File

@ -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');
}
}

View File

@ -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');
});
}
};