145 lines
4.6 KiB
PHP
145 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\User;
|
|
use App\Models\Worker;
|
|
use App\Models\Review;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class EmployerReviewController extends Controller
|
|
{
|
|
/**
|
|
* Add a new review for a worker.
|
|
* POST /api/employers/reviews
|
|
*/
|
|
public function addReview(Request $request)
|
|
{
|
|
/** @var User $employer */
|
|
$employer = $request->attributes->get('employer');
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'worker_id' => 'required|exists:workers,id',
|
|
'rating' => 'required|integer|min:1|max:5',
|
|
'comment' => 'nullable|string|max:1000',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Validation error.',
|
|
'errors' => $validator->errors()
|
|
], 422);
|
|
}
|
|
|
|
try {
|
|
// Check if employer has already reviewed this worker
|
|
$existingReview = Review::where('employer_id', $employer->id)
|
|
->where('worker_id', $request->worker_id)
|
|
->first();
|
|
|
|
if ($existingReview) {
|
|
// If they already have a review, we can update it or direct them to the edit endpoint.
|
|
// To be robust, let's update it directly and inform them it was updated (acting as an edit).
|
|
$existingReview->update([
|
|
'rating' => $request->rating,
|
|
'comment' => $request->comment,
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Review updated successfully (existing review updated).',
|
|
'data' => [
|
|
'review' => $existingReview
|
|
]
|
|
], 200);
|
|
}
|
|
|
|
// Create new review
|
|
$review = Review::create([
|
|
'employer_id' => $employer->id,
|
|
'worker_id' => $request->worker_id,
|
|
'rating' => $request->rating,
|
|
'comment' => $request->comment,
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Review added successfully.',
|
|
'data' => [
|
|
'review' => $review
|
|
]
|
|
], 201);
|
|
|
|
} catch (\Exception $e) {
|
|
logger()->error('Mobile API Add Review Failure: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred while adding the review.',
|
|
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Edit/update an existing review.
|
|
* PUT /api/employers/reviews/{id}
|
|
*/
|
|
public function editReview(Request $request, $id)
|
|
{
|
|
/** @var User $employer */
|
|
$employer = $request->attributes->get('employer');
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'rating' => 'required|integer|min:1|max:5',
|
|
'comment' => 'nullable|string|max:1000',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Validation error.',
|
|
'errors' => $validator->errors()
|
|
], 422);
|
|
}
|
|
|
|
try {
|
|
$review = Review::where('id', $id)
|
|
->where('employer_id', $employer->id)
|
|
->first();
|
|
|
|
if (!$review) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Review not found or unauthorized.'
|
|
], 404);
|
|
}
|
|
|
|
$review->update([
|
|
'rating' => $request->rating,
|
|
'comment' => $request->comment,
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Review edited successfully.',
|
|
'data' => [
|
|
'review' => $review
|
|
]
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
logger()->error('Mobile API Edit Review Failure: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred while updating the review.',
|
|
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
|
|
], 500);
|
|
}
|
|
}
|
|
}
|