Xboard/app/Http/Controllers/V1/Staff/NoticeController.php

60 lines
1.5 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Http\Controllers\V1\Staff;
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\NoticeSave;
use App\Models\Notice;
use Illuminate\Http\Request;
class NoticeController extends Controller
{
public function fetch(Request $request)
{
return response([
'data' => Notice::orderBy('id', 'DESC')->get()
]);
}
public function save(NoticeSave $request)
{
$data = $request->only([
'title',
'content',
'img_url'
]);
if (!$request->input('id')) {
if (!Notice::create($data)) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '保存失败');
2023-11-17 01:44:01 -05:00
}
} else {
try {
Notice::find($request->input('id'))->update($data);
} 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 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
}
$notice = Notice::find($request->input('id'));
if (!$notice) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '公告不存在');
2023-11-17 01:44:01 -05:00
}
if (!$notice->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
]);
}
}