Xboard/app/Http/Controllers/V1/Admin/KnowledgeController.php

115 lines
3.1 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Http\Controllers\V1\Admin;
2023-12-04 07:40:49 -05:00
use App\Exceptions\ApiException;
2023-11-17 01:44:01 -05:00
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\KnowledgeSave;
use App\Http\Requests\Admin\KnowledgeSort;
use App\Models\Knowledge;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class KnowledgeController extends Controller
{
public function fetch(Request $request)
{
if ($request->input('id')) {
$knowledge = Knowledge::find($request->input('id'))->toArray();
2023-12-04 07:40:49 -05:00
if (!$knowledge) throw new ApiException(500, '知识不存在');
2023-11-17 01:44:01 -05:00
return response([
'data' => $knowledge
]);
}
return response([
'data' => Knowledge::select(['title', 'id', 'updated_at', 'category', 'show'])
->orderBy('sort', 'ASC')
->get()
]);
}
public function getCategory(Request $request)
{
return response([
'data' => array_keys(Knowledge::get()->groupBy('category')->toArray())
]);
}
public function save(KnowledgeSave $request)
{
$params = $request->validated();
if (!$request->input('id')) {
if (!Knowledge::create($params)) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '创建失败');
2023-11-17 01:44:01 -05:00
}
} else {
try {
Knowledge::find($request->input('id'))->update($params);
} catch (\Exception $e) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '保存失败');
2023-11-17 01:44:01 -05:00
}
}
return response([
'data' => true
]);
}
public function show(Request $request)
{
if (empty($request->input('id'))) {
2023-12-04 07:40:49 -05:00
throw new ApiException(422, '参数有误');
2023-11-17 01:44:01 -05:00
}
$knowledge = Knowledge::find($request->input('id'));
if (!$knowledge) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '知识不存在');
2023-11-17 01:44:01 -05:00
}
$knowledge->show = $knowledge->show ? 0 : 1;
if (!$knowledge->save()) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '保存失败');
2023-11-17 01:44:01 -05:00
}
return response([
'data' => true
]);
}
public function sort(KnowledgeSort $request)
{
DB::beginTransaction();
try {
foreach ($request->input('knowledge_ids') as $k => $v) {
$knowledge = Knowledge::find($v);
$knowledge->timestamps = false;
$knowledge->update(['sort' => $k + 1]);
}
} catch (\Exception $e) {
DB::rollBack();
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '保存失败');
2023-11-17 01:44:01 -05:00
}
DB::commit();
return response([
'data' => true
]);
}
public function drop(Request $request)
{
if (empty($request->input('id'))) {
2023-12-04 07:40:49 -05:00
throw new ApiException(422, '参数有误');
2023-11-17 01:44:01 -05:00
}
$knowledge = Knowledge::find($request->input('id'));
if (!$knowledge) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '知识不存在');
2023-11-17 01:44:01 -05:00
}
if (!$knowledge->delete()) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '删除失败');
2023-11-17 01:44:01 -05:00
}
return response([
'data' => true
]);
}
}