This commit is contained in:
AfonsoCMSousa
2026-01-03 13:14:59 +00:00
parent 0fb64ff30c
commit ceba5178f3
6 changed files with 316 additions and 279 deletions
+50 -1
View File
@@ -255,7 +255,6 @@ class MatchController extends Controller
public function open(Request $request)
{
// FIX: Allow matches where P2 is NULL OR 'Ghost' (ID 1)
$query = MatchGame::where('status', 'Pending')
->where(function($q) {
$q->whereNull('player2_user_id')
@@ -272,4 +271,54 @@ class MatchController extends Controller
return response()->json($matches);
}
// --- FIX: Safely Delete Match by removing dependencies first ---
public function destroy(MatchGame $match)
{
$user = request()->user();
if ($match->player1_user_id !== $user->id) {
return response()->json(['message' => 'Unauthorized'], 403);
}
if ($match->status !== 'Pending') {
return response()->json(['message' => 'Cannot cancel a match that has already started'], 400);
}
return DB::transaction(function () use ($match, $user) {
// 1. Delete associated Games first (The FK constraint cause)
DB::table('games')->where('match_id', $match->id)->delete();
// 2. Unlink existing Coin Transactions (Set match_id to NULL)
CoinTransaction::where('match_id', $match->id)->update(['match_id' => null]);
// 3. Process Refund
if ($match->stake > 0) {
$user->increment('coins_balance', $match->stake);
$refundType = CoinTransactionType::firstOrCreate(
['name' => 'Refund'],
['type' => 'C']
);
// Create Refund Transaction with NO LINK to the deleted match
CoinTransaction::create([
'user_id' => $user->id,
'match_id' => null, // IMPORTANT: Must be null
'coin_transaction_type_id' => $refundType->id,
'transaction_datetime' => now(),
'coins' => $match->stake,
'custom' => "Refund for Match #{$match->id}"
]);
}
// 4. Finally delete the match
$match->delete();
return response()->json([
'message' => 'Match canceled and refunded',
'balance' => $user->coins_balance
]);
});
}
}
+8 -8
View File
@@ -4,17 +4,17 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class CoinTransactionType extends Model
{
use HasFactory, SoftDeletes;
use HasFactory;
protected $table = 'coin_transaction_types';
// FIX: Disable auto-timestamps because the table doesn't have updated_at/created_at columns
public $timestamps = false;
protected $fillable = ['name', 'type', 'custom'];
protected $casts = [
'custom' => 'array',
protected $fillable = [
'name',
'type',
'description' // Include description if your table has it
];
}
}
+1
View File
@@ -62,6 +62,7 @@ Route::prefix('v1')->group(function () {
Route::get('open', [MatchController::class, 'open']);
Route::post('/{match}/join', [MatchController::class, 'join']);
Route::post('/{match}/start', [MatchController::class, 'start']); // Changed to /start to be explicit
Route::delete('/{match}', [MatchController::class, 'destroy']);
Route::apiResource('/', MatchController::class)->parameters(['' => 'match']);
});