ApplicationApiController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\ApplicationApi;
  5. use Exception;
  6. use Illuminate\Contracts\Foundation\Application;
  7. use Illuminate\Contracts\View\Factory;
  8. use Illuminate\Contracts\View\View;
  9. use Illuminate\Http\JsonResponse;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Http\Response;
  13. class ApplicationApiController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Application|Factory|View|Response
  19. */
  20. public function index()
  21. {
  22. return view('admin.api.index');
  23. }
  24. /**
  25. * Show the form for creating a new resource.
  26. *
  27. * @return Application|Factory|View|Response
  28. */
  29. public function create()
  30. {
  31. return view('admin.api.create');
  32. }
  33. /**
  34. * Store a newly created resource in storage.
  35. *
  36. * @param Request $request
  37. * @return RedirectResponse
  38. */
  39. public function store(Request $request)
  40. {
  41. $request->validate([
  42. 'memo' => 'nullable|string|max:60',
  43. ]);
  44. ApplicationApi::create([
  45. 'memo' => $request->input('memo'),
  46. ]);
  47. return redirect()->route('admin.api.index')->with('success', __('api key created!'));
  48. }
  49. /**
  50. * Display the specified resource.
  51. *
  52. * @param ApplicationApi $applicationApi
  53. * @return Response
  54. */
  55. public function show(ApplicationApi $applicationApi)
  56. {
  57. //
  58. }
  59. /**
  60. * Show the form for editing the specified resource.
  61. *
  62. * @param ApplicationApi $applicationApi
  63. * @return Application|Factory|View|Response
  64. */
  65. public function edit(ApplicationApi $applicationApi)
  66. {
  67. return view('admin.api.edit', [
  68. 'applicationApi' => $applicationApi,
  69. ]);
  70. }
  71. /**
  72. * Update the specified resource in storage.
  73. *
  74. * @param Request $request
  75. * @param ApplicationApi $applicationApi
  76. * @return RedirectResponse
  77. */
  78. public function update(Request $request, ApplicationApi $applicationApi)
  79. {
  80. $request->validate([
  81. 'memo' => 'nullable|string|max:60',
  82. ]);
  83. $applicationApi->update($request->all());
  84. return redirect()->route('admin.api.index')->with('success', __('api key updated!'));
  85. }
  86. /**
  87. * Remove the specified resource from storage.
  88. *
  89. * @param ApplicationApi $applicationApi
  90. * @return RedirectResponse
  91. */
  92. public function destroy(ApplicationApi $applicationApi)
  93. {
  94. $applicationApi->delete();
  95. return redirect()->back()->with('success', __('api key has been removed!'));
  96. }
  97. /**
  98. * @param Request $request
  99. * @return JsonResponse|mixed
  100. *
  101. * @throws Exception
  102. */
  103. public function dataTable(Request $request)
  104. {
  105. $query = ApplicationApi::query();
  106. return datatables($query)
  107. ->addColumn('actions', function (ApplicationApi $apiKey) {
  108. return '
  109. <a data-content="'.__('Edit').'" data-toggle="popover" data-trigger="hover" data-placement="top" href="'.route('admin.api.edit', $apiKey->token).'" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
  110. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.api.destroy', $apiKey->token).'">
  111. '.csrf_field().'
  112. '.method_field('DELETE').'
  113. <button data-content="'.__('Delete').'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
  114. </form>
  115. ';
  116. })
  117. ->editColumn('token', function (ApplicationApi $apiKey) {
  118. return "<code>{$apiKey->token}</code>";
  119. })
  120. ->editColumn('last_used', function (ApplicationApi $apiKey) {
  121. return $apiKey->last_used ? $apiKey->last_used->diffForHumans() : '';
  122. })
  123. ->rawColumns(['actions', 'token'])
  124. ->make();
  125. }
  126. }