v2board/app/Http/Controllers/Admin/TutorialController.php

94 lines
2.3 KiB
PHP
Raw Normal View History

2020-01-02 21:38:32 +08:00
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\TutorialSave;
2020-04-16 23:01:18 +08:00
use App\Http\Requests\Admin\TutorialSort;
2020-01-02 21:38:32 +08:00
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Tutorial;
2020-04-16 23:01:18 +08:00
use Illuminate\Support\Facades\DB;
2020-01-02 21:38:32 +08:00
class TutorialController extends Controller
{
2020-01-11 13:36:52 +08:00
public function fetch(Request $request)
{
2020-01-02 21:38:32 +08:00
return response([
2020-04-20 16:07:06 +08:00
'data' => Tutorial::orderBy('sort', 'ASC')->get()
2020-01-02 21:38:32 +08:00
]);
}
2020-01-11 13:36:52 +08:00
public function save(TutorialSave $request)
{
2020-02-18 21:46:00 +08:00
$params = $request->only(array_keys(TutorialSave::RULES));
2020-01-02 21:38:32 +08:00
2020-01-11 13:36:52 +08:00
if (!$request->input('id')) {
if (!Tutorial::create($params)) {
abort(500, '创建失败');
}
} else {
2020-03-17 14:28:47 +08:00
try {
Tutorial::find($request->input('id'))->update($params);
} catch (\Exception $e) {
2020-01-11 13:36:52 +08:00
abort(500, '保存失败');
}
}
2020-01-02 21:38:32 +08:00
return response([
'data' => true
]);
}
2020-01-11 13:36:52 +08:00
public function show(Request $request)
{
2020-01-02 21:38:32 +08:00
if (empty($request->input('id'))) {
abort(500, '参数有误');
}
$tutorial = Tutorial::find($request->input('id'));
2020-01-03 01:12:23 +08:00
if (!$tutorial) {
abort(500, '教程不存在');
}
2020-01-02 21:38:32 +08:00
$tutorial->show = $tutorial->show ? 0 : 1;
if (!$tutorial->save()) {
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
2020-01-03 01:12:23 +08:00
2020-04-16 23:01:18 +08:00
public function sort(TutorialSort $request)
{
DB::beginTransaction();
2020-04-20 16:07:06 +08:00
foreach ($request->input('tutorial_ids') as $k => $v) {
if (!Tutorial::find($v)->update(['sort' => $k + 1])) {
DB::rollBack();
2020-04-16 23:01:18 +08:00
abort(500, '保存失败');
}
}
DB::commit();
return response([
'data' => true
]);
}
2020-01-11 13:36:52 +08:00
public function drop(Request $request)
{
2020-01-03 01:12:23 +08:00
if (empty($request->input('id'))) {
abort(500, '参数有误');
}
$tutorial = Tutorial::find($request->input('id'));
if (!$tutorial) {
abort(500, '教程不存在');
}
if (!$tutorial->delete()) {
abort(500, '删除失败');
}
return response([
'data' => true
]);
}
2020-01-11 13:36:52 +08:00
}