NameRuleController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\CombinationSettingRequest;
  5. use App\Http\Requests\API\NamingRule\CreateOrUpdateRequest;
  6. use App\Http\Resources\API\NamingRuleResource;
  7. use App\Models\NamingRule;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Auth;
  10. class NameRuleController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. */
  15. public function index(Request $request)
  16. {
  17. $namingRules = NamingRule::allowed()->with(['company'])->filter($request->all())->paginate();
  18. return NamingRuleResource::collection($namingRules);
  19. }
  20. /**
  21. * Store a newly created resource in storage.
  22. */
  23. public function store(CreateOrUpdateRequest $request)
  24. {
  25. $formData = $request->only(['name', 'status', 'company_id', 'global']);
  26. if (! Auth::user()->super_admin) {
  27. $formData['company_id'] = Auth::user()->company_id;
  28. unset($formData['global']);
  29. }
  30. NamingRule::create($formData);
  31. return $this->created();
  32. }
  33. /**
  34. * Display the specified resource.
  35. */
  36. public function show(string $id)
  37. {
  38. $namingRule = NamingRule::query()->allowed()->findOrFail($id);
  39. return new NamingRuleResource($namingRule);
  40. }
  41. /**
  42. * Update the specified resource in storage.
  43. */
  44. public function update(CreateOrUpdateRequest $request, string $id)
  45. {
  46. $namingRule = NamingRule::query()->allowed()->findOrFail($id);
  47. $namingRule->fill($request->only([
  48. 'name', 'status', 'global'
  49. ]));
  50. $namingRule->save();
  51. return $this->noContent();
  52. }
  53. /**
  54. * Remove the specified resource from storage.
  55. */
  56. public function destroy(string $id)
  57. {
  58. }
  59. public function enabled()
  60. {
  61. $namingRules = NamingRule::where('status', 1)->where(function ($query) {
  62. return $query->where("company_id", Auth::user()->company_id)->orWhere([
  63. 'company_id' => 0,
  64. 'global' => 1
  65. ]);
  66. })->select(['id', 'name'])->get();
  67. return $this->success([
  68. 'data' => $namingRules
  69. ]);
  70. }
  71. public function combinationSetting(CombinationSettingRequest $request, string $id)
  72. {
  73. $namingRule = NamingRule::query()->allowed()->findOrFail($id);
  74. $namingRule->combination_rules = $request->all();
  75. $namingRule->save();
  76. return $this->noContent();
  77. }
  78. }