LibraryController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. $library = Library::query()->allowed()->findOrFail($id);
  59. if ($library->container()->count() > 0) {
  60. return $this->badRequest("The container has been associated and is not allowed to be deleted");
  61. }
  62. $library->delete();
  63. return $this->noContent();
  64. }
  65. public function linkage(Request $request, string $type)
  66. {
  67. $libraryType = LibraryType::from($type);
  68. if (in_array($libraryType->value, [LibraryType::PROJECT->value, LibraryType::ASSET->value]) && !$request->get("id")) {
  69. return $this->badRequest("Parameter ID cannot be empty");
  70. }
  71. $where = match ($libraryType) {
  72. LibraryType::ASSET => ['asset_id' => $request->get("id")],
  73. LibraryType::PROJECT => ['project_id' => $request->get("id")],
  74. LibraryType::CUSTOM => []
  75. };
  76. $libraries = Library::query()
  77. ->allowed()
  78. ->where("type", $type)->when($where, fn($query) => $query->where($where))
  79. ->get(['id', 'name']);
  80. return $this->success([
  81. 'data' => $libraries
  82. ]);
  83. }
  84. }