tries to add api requests

This commit is contained in:
unurled 2025-07-05 10:22:33 +02:00
parent e841c38573
commit 73c32b4fb6
26 changed files with 962 additions and 39 deletions

View file

@ -10,6 +10,7 @@ use App\Models\User;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
@ -201,19 +202,23 @@ class TournamentController extends Controller
->with('success', 'Tournament deleted successfully!');
}
public function addTeam(Competition $tournament, Request $request)
public function addTeam(Request $request, Competition $tournament)
{
Log::info($request);
// Check if user is tournament owner
if ($tournament->owner != Auth::id()) {
Log::error('User is not authorized to add a team to this tournament.');
Log::error('userId: ' . Auth::id() . ' tournamentOwnerId: ' . $tournament->owner);
return redirect()->back()->with('error', 'You are not authorized to add a team to this tournament.');
}
// Validate the request data
$validator = Validator::make($request->all(), [
'team_id' => 'required|exists:teams,id',
'teamName' => 'required|exists:teams,string',
]);
if ($validator->fails()) {
Log::error('Validation failed for addTeam', $validator->errors());
return redirect()->back()->withErrors($validator)->withInput();
}
@ -228,9 +233,8 @@ class TournamentController extends Controller
}
$tournament->teams()->attach($team->id);
return redirect()->route('tournaments.show', $tournament->id)
->with('success', 'Team added successfully!');
return response()->json([
'tournament' => $tournament,
]);
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ApiCreateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$rules = [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'unique:users', 'max:255'],
'password' => ['required', 'string', 'min:8', 'max:255', 'confirmed'],
];
return $rules;
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ApiLoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$rules = [
'email' => ['required', 'email', 'max:255'],
'password' => ['required', 'min:8'],
];
return $rules;
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'email_verified_at' => $this->email_verified_at,
];
}
}