LibraryController.php 4.0 KB

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