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.
98 lines
2.8 KiB
98 lines
2.8 KiB
|
2 days ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\PastReview;
|
||
|
|
use App\Support\ApiResponse;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
class PastReviewController extends Controller
|
||
|
|
{
|
||
|
|
use ApiResponse;
|
||
|
|
|
||
|
|
public function index(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$query = PastReview::query();
|
||
|
|
|
||
|
|
if ($request->filled('status')) {
|
||
|
|
$query->where('status', (int) $request->query('status'));
|
||
|
|
}
|
||
|
|
if ($kw = $request->query('keyword')) {
|
||
|
|
$query->where('title', 'like', "%{$kw}%");
|
||
|
|
}
|
||
|
|
|
||
|
|
$paginator = $query
|
||
|
|
->orderBy('sort')
|
||
|
|
->orderByDesc('id')
|
||
|
|
->paginate((int) $request->query('page_size', 20))
|
||
|
|
->withQueryString();
|
||
|
|
|
||
|
|
$paginator->getCollection()->transform(fn (PastReview $row) => $this->serialize($row));
|
||
|
|
|
||
|
|
return $this->paginated($paginator);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function show(int $pastReview): JsonResponse
|
||
|
|
{
|
||
|
|
$model = PastReview::query()->findOrFail($pastReview);
|
||
|
|
|
||
|
|
return $this->ok($this->serialize($model));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$data = $this->validated($request);
|
||
|
|
$row = PastReview::query()->create($data);
|
||
|
|
|
||
|
|
return $this->ok(['id' => $row->id], '已创建');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(Request $request, int $pastReview): JsonResponse
|
||
|
|
{
|
||
|
|
$model = PastReview::query()->findOrFail($pastReview);
|
||
|
|
$data = $this->validated($request, $model);
|
||
|
|
$model->fill($data);
|
||
|
|
$model->save();
|
||
|
|
|
||
|
|
return $this->ok($this->serialize($model->fresh()), '已保存');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(int $pastReview): JsonResponse
|
||
|
|
{
|
||
|
|
PastReview::query()->findOrFail($pastReview)->delete();
|
||
|
|
|
||
|
|
return $this->ok(null, '已删除');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
protected function validated(Request $request, ?PastReview $existing = null): array
|
||
|
|
{
|
||
|
|
return $request->validate([
|
||
|
|
'title' => [$existing ? 'sometimes' : 'required', 'string', 'max:255'],
|
||
|
|
'cover_url' => ['nullable', 'string', 'max:512'],
|
||
|
|
'sort' => [$existing ? 'sometimes' : 'required', 'integer', 'min:0'],
|
||
|
|
'status' => ['sometimes', 'integer', 'in:0,1'],
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
protected function serialize(PastReview $row): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'id' => $row->id,
|
||
|
|
'title' => $row->title,
|
||
|
|
'cover_url' => $row->cover_url,
|
||
|
|
'sort' => $row->sort,
|
||
|
|
'status' => $row->status,
|
||
|
|
'created_at' => $row->created_at?->toIso8601String(),
|
||
|
|
'updated_at' => $row->updated_at?->toIso8601String(),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|