LibraryController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Container;
  7. use App\Models\Enums\LibraryType;
  8. use App\Models\Library;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. class LibraryController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. */
  16. public function index(Request $request)
  17. {
  18. $libraries = Library::query()->filter($request->all())->allowed()->get(['id', 'name']);
  19. $containerCount = Container::query()->allowed()
  20. ->whereIn("library_id", $libraries->pluck("id")->toArray())
  21. ->selectRaw("count(*) as cut, library_id")
  22. ->groupBy("library_id")
  23. ->pluck("cut", "library_id");
  24. $libraries->map(fn(Library $library) => $library->container_count = $containerCount->get($library->id, 0));
  25. return $this->success([
  26. 'data' => $libraries
  27. ]);
  28. }
  29. /**
  30. * Store a newly created resource in storage.
  31. */
  32. public function store(CreateOrUpdateRequest $request)
  33. {
  34. Library::query()->create([
  35. ...$request->all(),
  36. 'created_by' => Auth::id(),
  37. 'company_id' => Auth::user()->company_id,
  38. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  39. ]);
  40. return $this->created();
  41. }
  42. /**
  43. * Display the specified resource.
  44. */
  45. public function show(string $id)
  46. {
  47. $library = Library::allowed()->findOrFail($id);
  48. return new LibraryResource($library);
  49. }
  50. /**
  51. * Update the specified resource in storage.
  52. */
  53. public function update(Request $request, string $id)
  54. {
  55. $library = Library::query()->allowed()->findOrFail($id);
  56. $library->fill($request->only([
  57. 'name', 'acl', 'asset_id', 'project_id',
  58. ]));
  59. $library->whitelist = $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null;
  60. $library->save();
  61. return $this->noContent();
  62. }
  63. /**
  64. * Remove the specified resource from storage.
  65. */
  66. public function destroy(string $id)
  67. {
  68. $library = Library::query()->allowed()->findOrFail($id);
  69. if ($library->container()->count() > 0) {
  70. return $this->badRequest("The container has been associated and is not allowed to be deleted");
  71. }
  72. $library->delete();
  73. return $this->noContent();
  74. }
  75. public function linkage(Request $request, string $type)
  76. {
  77. $libraryType = LibraryType::from($type);
  78. if (in_array($libraryType->value, [LibraryType::PROJECT->value, LibraryType::ASSET->value]) && !$request->get("id")) {
  79. return $this->badRequest("Parameter ID cannot be empty");
  80. }
  81. $where = match ($libraryType) {
  82. LibraryType::ASSET => ['asset_id' => $request->get("id")],
  83. LibraryType::PROJECT => ['project_id' => $request->get("id")],
  84. LibraryType::CUSTOM => []
  85. };
  86. $libraries = Library::query()
  87. ->allowed()
  88. ->where("type", $type)->when($where, fn($query) => $query->where($where))
  89. ->get(['id', 'name']);
  90. return $this->success([
  91. 'data' => $libraries
  92. ]);
  93. }
  94. }