LibraryController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = $libraries->map(function (Library $library) use ($containerCount) {
  25. $library->itemCount = $containerCount->get($library->id, 0);
  26. $library->type = 'library';
  27. $library->uniId = $library->type . '_' . $library->id;
  28. return $library;
  29. });
  30. return $this->success([
  31. 'data' => $libraries
  32. ]);
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. */
  37. public function store(CreateOrUpdateRequest $request)
  38. {
  39. Library::query()->create([
  40. ...$request->all(),
  41. 'created_by' => Auth::id(),
  42. 'company_id' => Auth::user()->company_id,
  43. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  44. ]);
  45. return $this->created();
  46. }
  47. /**
  48. * Display the specified resource.
  49. */
  50. public function show(string $id)
  51. {
  52. $library = Library::allowed()->findOrFail($id);
  53. return new LibraryResource($library);
  54. }
  55. /**
  56. * Update the specified resource in storage.
  57. */
  58. public function update(Request $request, string $id)
  59. {
  60. $library = Library::query()->allowed()->findOrFail($id);
  61. $library->fill($request->only([
  62. 'name', 'acl', 'asset_id', 'project_id',
  63. ]));
  64. $library->whitelist = $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null;
  65. $library->save();
  66. return $this->noContent();
  67. }
  68. /**
  69. * Remove the specified resource from storage.
  70. */
  71. public function destroy(string $id)
  72. {
  73. $library = Library::query()->allowed()->findOrFail($id);
  74. if ($library->container()->count() > 0) {
  75. return $this->badRequest("The container has been associated and is not allowed to be deleted");
  76. }
  77. $library->delete();
  78. return $this->noContent();
  79. }
  80. public function linkage(Request $request, string $type)
  81. {
  82. $libraryType = LibraryType::from($type);
  83. if (in_array($libraryType->value, [LibraryType::PROJECT->value, LibraryType::ASSET->value]) && !$request->get("id")) {
  84. return $this->badRequest("Parameter ID cannot be empty");
  85. }
  86. $where = match ($libraryType) {
  87. LibraryType::ASSET => ['asset_id' => $request->get("id")],
  88. LibraryType::PROJECT => ['project_id' => $request->get("id")],
  89. LibraryType::CUSTOM => []
  90. };
  91. $libraries = Library::query()
  92. ->allowed()
  93. ->where("type", $type)->when($where, fn($query) => $query->where($where))
  94. ->get(['id', 'name'])
  95. ->each(function ($libraries) {
  96. // 设置固定的type值
  97. $libraries->type = 'library';
  98. $libraries->uniId=$libraries->type.'_'.$libraries->id;
  99. });
  100. return $this->success([
  101. 'data' => $libraries
  102. ]);
  103. }
  104. }