Files

99 lines
2.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\Game;
use App\Models\UserList;
use App\Models\UserListGame;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ListController extends Controller
{
2026-05-31 23:35:06 +03:00
public function community()
{
$lists = UserList::where('is_public', true)
->with('user')
->withCount('listGames')
->latest()
->paginate(24);
return view('lists.community', compact('lists'));
}
public function index()
{
$lists = auth()->user()->lists()->withCount('listGames')->latest()->get();
return view('lists.index', compact('lists'));
}
public function store(Request $request)
{
$data = $request->validate([
'title' => 'required|string|max:100',
'description' => 'nullable|string|max:1000',
'is_public' => 'boolean',
]);
$slug = Str::slug($data['title']);
$base = $slug;
$i = 1;
while (auth()->user()->lists()->where('slug', $slug)->exists()) {
$slug = $base . '-' . $i++;
}
$list = UserList::create([
'user_id' => auth()->id(),
'title' => $data['title'],
'slug' => $slug,
'description' => $data['description'] ?? null,
'is_public' => $data['is_public'] ?? true,
]);
return redirect()->route('lists.show', $list)->with('success', 'List created!');
}
public function show(UserList $list)
{
if (!$list->is_public && (!auth()->check() || auth()->id() !== $list->user_id)) {
abort(403);
}
$list->load(['listGames.game', 'user']);
return view('lists.show', compact('list'));
}
public function addGame(Request $request, UserList $list)
{
$this->authorize('manage', $list);
$data = $request->validate(['game_id' => 'required|exists:games,id']);
UserListGame::firstOrCreate([
'user_list_id' => $list->id,
'game_id' => $data['game_id'],
], ['order' => $list->listGames()->count()]);
return response()->json(['success' => true]);
}
public function removeGame(UserList $list, Game $game)
{
$this->authorize('manage', $list);
$list->listGames()->where('game_id', $game->id)->delete();
return response()->json(['success' => true]);
}
public function destroy(UserList $list)
{
$this->authorize('manage', $list);
$list->delete();
return redirect()->route('lists.index')->with('success', 'List deleted.');
}
}