LibraryController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Library\CreateOrUpdateRequest;
  5. use App\Http\Resources\API\LibraryResource;
  6. use App\Models\Enums\LibraryType;
  7. use App\Models\Library;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Auth;
  10. class LibraryController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. */
  15. public function index()
  16. {
  17. //
  18. }
  19. /**
  20. * Store a newly created resource in storage.
  21. */
  22. public function store(CreateOrUpdateRequest $request)
  23. {
  24. Library::query()->create([
  25. ...$request->all(),
  26. 'created_by' => Auth::id(),
  27. 'company_id' => Auth::user()->company_id,
  28. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  29. ]);
  30. return $this->created();
  31. }
  32. /**
  33. * Display the specified resource.
  34. */
  35. public function show(string $id)
  36. {
  37. $library = Library::allowed()->findOrFail($id);
  38. return new LibraryResource($library);
  39. }
  40. /**
  41. * Update the specified resource in storage.
  42. */
  43. public function update(Request $request, string $id)
  44. {
  45. $library = Library::query()->allowed()->findOrFail($id);
  46. $library->fill($request->only([
  47. 'name', 'acl', 'asset_id', 'project_id',
  48. ]));
  49. $library->whitelist = $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null;
  50. $library->save();
  51. return $this->noContent();
  52. }
  53. /**
  54. * Remove the specified resource from storage.
  55. */
  56. public function destroy(string $id)
  57. {
  58. //
  59. }
  60. public function linkage(Request $request, string $type)
  61. {
  62. $libraryType = LibraryType::from($type);
  63. if (in_array($libraryType->value, [LibraryType::PROJECT->value, LibraryType::ASSET->value]) && !$request->get("id")) {
  64. return $this->badRequest("Parameter ID cannot be empty");
  65. }
  66. $where = match ($libraryType) {
  67. LibraryType::ASSET => ['asset_id' => $request->get("id")],
  68. LibraryType::PROJECT => ['project_id' => $request->get("id")],
  69. LibraryType::CUSTOM => []
  70. };
  71. $libraries = Library::query()
  72. ->allowed()
  73. ->where("type", $type)->when($where, fn($query) => $query->where($where))
  74. ->get(['id', 'name']);
  75. return $this->success([
  76. 'data' => $libraries
  77. ]);
  78. }
  79. }