LabelController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Controllers\Admin\Config;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Label;
  5. use Exception;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Http\Request;
  8. use Log;
  9. use Validator;
  10. class LabelController extends Controller
  11. {
  12. public function store(Request $request): JsonResponse
  13. { // 添加标签
  14. $validator = Validator::make($request->all(), [
  15. 'name' => 'required|string|unique:label,name',
  16. 'sort' => 'required|numeric',
  17. ]);
  18. if ($validator->fails()) {
  19. return response()->json(['status' => 'fail', 'message' => $validator->errors()->all()]);
  20. }
  21. try {
  22. if (Label::create($validator->validated())) {
  23. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.add')])]);
  24. }
  25. } catch (Exception $e) {
  26. Log::error(trans('common.error_action_item', ['action' => trans('common.add'), 'attribute' => trans('model.node.label')]).': '.$e->getMessage());
  27. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')]).', '.$e->getMessage()]);
  28. }
  29. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')])]);
  30. }
  31. public function update(Request $request, Label $label): JsonResponse
  32. { // 编辑标签
  33. $validator = Validator::make($request->all(), [
  34. 'name' => 'required|string|unique:label,name,'.$label->id,
  35. 'sort' => 'required|numeric',
  36. ]);
  37. if ($validator->fails()) {
  38. return response()->json(['status' => 'fail', 'message' => $validator->errors()->all()]);
  39. }
  40. try {
  41. if ($label->update($validator->validated())) {
  42. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]);
  43. }
  44. } catch (Exception $e) {
  45. Log::error(trans('common.error_action_item', ['action' => trans('common.edit'), 'attribute' => trans('model.node.label')]).': '.$e->getMessage());
  46. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')]).', '.$e->getMessage()]);
  47. }
  48. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]);
  49. }
  50. public function destroy(Label $label): JsonResponse
  51. { // 删除标签
  52. try {
  53. // 先从所有节点中移除该标签
  54. $label->nodes()->detach();
  55. // 然后删除标签
  56. if ($label->delete()) {
  57. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  58. }
  59. } catch (Exception $e) {
  60. Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('model.node.label')]).': '.$e->getMessage());
  61. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]);
  62. }
  63. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  64. }
  65. }