ApplicationApiController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. use Illuminate\Support\Str;
  14. class ApplicationApiController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return Application|Factory|View|Response
  20. */
  21. public function index()
  22. {
  23. return view('admin.api.index');
  24. }
  25. /**
  26. * Show the form for creating a new resource.
  27. *
  28. * @return Application|Factory|View|Response
  29. */
  30. public function create()
  31. {
  32. return view('admin.api.create');
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param Request $request
  38. * @return RedirectResponse
  39. */
  40. public function store(Request $request)
  41. {
  42. $request->validate([
  43. 'memo' => 'nullable|string|max:60'
  44. ]);
  45. ApplicationApi::create([
  46. 'memo' => $request->input('memo')
  47. ]);
  48. return redirect()->route('admin.api.index')->with('success', __('api key created!'));
  49. }
  50. /**
  51. * Display the specified resource.
  52. *
  53. * @param ApplicationApi $applicationApi
  54. * @return Response
  55. */
  56. public function show(ApplicationApi $applicationApi)
  57. {
  58. //
  59. }
  60. /**
  61. * Show the form for editing the specified resource.
  62. *
  63. * @param ApplicationApi $applicationApi
  64. * @return Application|Factory|View|Response
  65. */
  66. public function edit(ApplicationApi $applicationApi)
  67. {
  68. return view('admin.api.edit' , [
  69. 'applicationApi' => $applicationApi
  70. ]);
  71. }
  72. /**
  73. * Update the specified resource in storage.
  74. *
  75. * @param Request $request
  76. * @param ApplicationApi $applicationApi
  77. * @return RedirectResponse
  78. */
  79. public function update(Request $request, ApplicationApi $applicationApi)
  80. {
  81. $request->validate([
  82. 'memo' => 'nullable|string|max:60'
  83. ]);
  84. $applicationApi->update($request->all());
  85. return redirect()->route('admin.api.index')->with('success', __('api key updated!'));
  86. }
  87. /**
  88. * Remove the specified resource from storage.
  89. *
  90. * @param ApplicationApi $applicationApi
  91. * @return RedirectResponse
  92. */
  93. public function destroy(ApplicationApi $applicationApi)
  94. {
  95. $applicationApi->delete();
  96. return redirect()->back()->with('success', __('api key has been removed!'));
  97. }
  98. /**
  99. * @param Request $request
  100. * @return JsonResponse|mixed
  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. }