LibraryController.php 3.8 KB

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