152 lines
5.2 KiB
PHP
152 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* متحكم التقييمات (Rating Controller)
|
|
*
|
|
* الغرض من الملف:
|
|
* إدارة عمليات التقييم المتبادل بين السائقين والركاب، بالإضافة لتقييم التطبيق نفسه.
|
|
*
|
|
* كيفية العمل:
|
|
* 1. يستقبل التقييم (من 1 إلى 5) والتعليق من المستخدم.
|
|
* 2. يتحقق من عدم وجود تقييم مسبق لنفس الرحلة لمنع التكرار.
|
|
* 3. يحفظ التقييم في الجداول المناسبة (ratingDriver أو ratingPassenger).
|
|
*/
|
|
class RatingController extends Controller
|
|
{
|
|
/** POST /v2/ratings/driver — passenger rates a driver */
|
|
public function rateDriver(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'driver_id' => 'required|string',
|
|
'ride_id' => 'required|integer',
|
|
'rating' => 'required|numeric|min:1|max:5',
|
|
'comment' => 'nullable|string|max:500',
|
|
]);
|
|
|
|
$passengerId = $request->input('_jwt_user_id');
|
|
|
|
// Prevent duplicate ratings
|
|
$exists = DB::connection('primary')->table('ratingDriver')
|
|
->where('ride_id', $request->input('ride_id'))->exists();
|
|
if ($exists) {
|
|
return response()->json(['status' => 'failure', 'message' => 'Already rated'], 409);
|
|
}
|
|
|
|
DB::connection('primary')->table('ratingDriver')->insert([
|
|
'passenger_id' => $passengerId,
|
|
'driver_id' => $request->input('driver_id'),
|
|
'ride_id' => $request->input('ride_id'),
|
|
'rating' => $request->input('rating'),
|
|
'comment' => $request->input('comment', ''),
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
return response()->json(['status' => 'success'], 201);
|
|
}
|
|
|
|
/** POST /v2/ratings/passenger — driver rates a passenger */
|
|
public function ratePassenger(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'passenger_id' => 'required|string',
|
|
'ride_id' => 'required',
|
|
'rating' => 'required|numeric|min:1|max:5',
|
|
'comment' => 'nullable|string|max:500',
|
|
]);
|
|
|
|
$driverId = $request->input('_jwt_user_id');
|
|
|
|
$exists = DB::connection('primary')->table('ratingPassenger')
|
|
->where('rideId', $request->input('ride_id'))->exists();
|
|
if ($exists) {
|
|
return response()->json(['status' => 'failure', 'message' => 'Already rated'], 409);
|
|
}
|
|
|
|
DB::connection('primary')->table('ratingPassenger')->insert([
|
|
'passenger_id' => $request->input('passenger_id'),
|
|
'driverID' => $driverId,
|
|
'rideId' => $request->input('ride_id'),
|
|
'rating' => $request->input('rating'),
|
|
'comment' => $request->input('comment', ''),
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
return response()->json(['status' => 'success'], 201);
|
|
}
|
|
|
|
/** POST /v2/ratings/app */
|
|
public function rateApp(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'rating' => 'required|numeric|min:1|max:5',
|
|
'comment' => 'nullable|string|max:300',
|
|
]);
|
|
|
|
$userId = $request->input('_jwt_user_id');
|
|
$userType = $request->input('_jwt_user_type');
|
|
|
|
DB::connection('primary')->table('ratingApp')->insert([
|
|
'name' => $request->input('name', ''),
|
|
'email' => $request->input('email', ''),
|
|
'phone' => $request->input('phone', ''),
|
|
'userId' => $userId,
|
|
'userType' => $userType,
|
|
'rating' => $request->input('rating'),
|
|
'comment' => $request->input('comment', ''),
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
return response()->json(['status' => 'success'], 201);
|
|
}
|
|
|
|
/** GET /v2/ratings/driver/{id} */
|
|
public function driverRating(string $id): JsonResponse
|
|
{
|
|
$ratings = DB::connection('primary')->table('ratingDriver')
|
|
->where('driver_id', $id)
|
|
->orderBy('created_at', 'desc')
|
|
->limit(50)
|
|
->get();
|
|
|
|
$avg = DB::connection('primary')->table('ratingDriver')
|
|
->where('driver_id', $id)->avg('rating');
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'data' => [
|
|
'average' => round($avg ?? 5.0, 2),
|
|
'count' => $ratings->count(),
|
|
'ratings' => $ratings,
|
|
],
|
|
]);
|
|
}
|
|
|
|
/** GET /v2/ratings/passenger/{id} */
|
|
public function passengerRating(string $id): JsonResponse
|
|
{
|
|
$ratings = DB::connection('primary')->table('ratingPassenger')
|
|
->where('passenger_id', $id)
|
|
->orderBy('created_at', 'desc')
|
|
->limit(50)
|
|
->get();
|
|
|
|
$avg = DB::connection('primary')->table('ratingPassenger')
|
|
->where('passenger_id', $id)->avg('rating');
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'data' => [
|
|
'average' => round($avg ?? 5.0, 2),
|
|
'count' => $ratings->count(),
|
|
'ratings' => $ratings,
|
|
],
|
|
]);
|
|
}
|
|
}
|