LibraryController.php 4.2 KB

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