You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.6 KiB
114 lines
3.6 KiB
|
1 month ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Api\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Http\Requests\Admin\StoreReviewerRequest;
|
||
|
|
use App\Http\Requests\Admin\UpdateReviewerRequest;
|
||
|
|
use App\Models\Reviewer;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
|
|
||
|
|
class ReviewerController extends Controller
|
||
|
|
{
|
||
|
|
public function index(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$perPage = min((int) $request->query('per_page', 15), 100);
|
||
|
|
|
||
|
|
$q = Reviewer::query()
|
||
|
|
->withCount('reviewerScopes')
|
||
|
|
->orderByDesc('id');
|
||
|
|
|
||
|
|
$keyword = trim((string) $request->query('q', ''));
|
||
|
|
if ($keyword !== '') {
|
||
|
|
$q->where(function ($w) use ($keyword): void {
|
||
|
|
$w->where('mobile', 'like', '%'.$keyword.'%')
|
||
|
|
->orWhere('name', 'like', '%'.$keyword.'%')
|
||
|
|
->orWhere('username', 'like', '%'.$keyword.'%');
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
$status = $request->query('status');
|
||
|
|
if (\in_array($status, ['active', 'disabled'], true)) {
|
||
|
|
$q->where('status', $status);
|
||
|
|
}
|
||
|
|
|
||
|
|
$paginator = $q->paginate($perPage);
|
||
|
|
$paginator->getCollection()->transform(function (Reviewer $r): array {
|
||
|
|
return $this->toArray($r, (int) $r->reviewer_scopes_count);
|
||
|
|
});
|
||
|
|
|
||
|
|
return response()->json($paginator);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(StoreReviewerRequest $request): JsonResponse
|
||
|
|
{
|
||
|
|
/** @var array<string, mixed> $validated */
|
||
|
|
$validated = $request->validated();
|
||
|
|
$password = $validated['password'];
|
||
|
|
unset($validated['password']);
|
||
|
|
$validated['status'] ??= 'active';
|
||
|
|
|
||
|
|
/** @var Reviewer $reviewer */
|
||
|
|
$reviewer = new Reviewer($validated);
|
||
|
|
$reviewer->password_hash = $password;
|
||
|
|
$reviewer->save();
|
||
|
|
|
||
|
|
return response()->json($this->toArray($reviewer->fresh(), 0), Response::HTTP_CREATED);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function show(Reviewer $reviewer): JsonResponse
|
||
|
|
{
|
||
|
|
$reviewer->loadCount('reviewerScopes');
|
||
|
|
|
||
|
|
return response()->json($this->toArray($reviewer, (int) $reviewer->reviewer_scopes_count));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(UpdateReviewerRequest $request, Reviewer $reviewer): JsonResponse
|
||
|
|
{
|
||
|
|
/** @var array<string, mixed> $validated */
|
||
|
|
$validated = $request->validated();
|
||
|
|
|
||
|
|
if (\array_key_exists('password', $validated)) {
|
||
|
|
$pw = $validated['password'];
|
||
|
|
unset($validated['password']);
|
||
|
|
if ($pw !== null && $pw !== '') {
|
||
|
|
$reviewer->password_hash = $pw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$reviewer->fill($validated);
|
||
|
|
$reviewer->save();
|
||
|
|
$reviewer->loadCount('reviewerScopes');
|
||
|
|
|
||
|
|
return response()->json($this->toArray($reviewer, (int) $reviewer->reviewer_scopes_count));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(Reviewer $reviewer): Response
|
||
|
|
{
|
||
|
|
$reviewer->delete();
|
||
|
|
|
||
|
|
return response()->noContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
private function toArray(Reviewer $r, int $scopesCount): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'id' => $r->id,
|
||
|
|
'mobile' => $r->mobile,
|
||
|
|
'username' => $r->username,
|
||
|
|
'name' => $r->name,
|
||
|
|
'status' => $r->status,
|
||
|
|
'reviewer_scopes_count' => $scopesCount,
|
||
|
|
'password_set' => $r->passwordIsSet(),
|
||
|
|
'last_login_at' => $r->last_login_at?->toIso8601String(),
|
||
|
|
'created_at' => $r->created_at?->toIso8601String(),
|
||
|
|
'updated_at' => $r->updated_at?->toIso8601String(),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|