LibraryController.php 3.8 KB

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