123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Http\Controllers\API;
- use App\Http\Controllers\Controller;
- use App\Http\Requests\API\Library\CreateOrUpdateRequest;
- use App\Http\Resources\API\LibraryResource;
- use App\Models\Container;
- use App\Models\Enums\LibraryType;
- use App\Models\Library;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- class LibraryController extends Controller
- {
- /**
- * Display a listing of the resource.
- */
- public function index(Request $request)
- {
- $libraries = Library::query()->filter($request->all())->allowed()->get(['id', 'name']);
- $containerCount = Container::query()->allowed()
- ->whereIn("library_id", $libraries->pluck("id")->toArray())
- ->selectRaw("count(*) as cut, library_id")
- ->groupBy("library_id")
- ->pluck("cut", "library_id");
- $libraries->map(fn(Library $library) => $library->container_count = $containerCount->get($library->id, 0));
- return $this->success([
- 'data' => $libraries
- ]);
- }
- /**
- * Store a newly created resource in storage.
- */
- public function store(CreateOrUpdateRequest $request)
- {
- Library::query()->create([
- ...$request->all(),
- 'created_by' => Auth::id(),
- 'company_id' => Auth::user()->company_id,
- 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
- ]);
- return $this->created();
- }
- /**
- * Display the specified resource.
- */
- public function show(string $id)
- {
- $library = Library::allowed()->findOrFail($id);
- return new LibraryResource($library);
- }
- /**
- * Update the specified resource in storage.
- */
- public function update(Request $request, string $id)
- {
- $library = Library::query()->allowed()->findOrFail($id);
- $library->fill($request->only([
- 'name', 'acl', 'asset_id', 'project_id',
- ]));
- $library->whitelist = $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null;
- $library->save();
- return $this->noContent();
- }
- /**
- * Remove the specified resource from storage.
- */
- public function destroy(string $id)
- {
- $library = Library::query()->allowed()->findOrFail($id);
- if ($library->container()->count() > 0) {
- return $this->badRequest("The container has been associated and is not allowed to be deleted");
- }
- $library->delete();
- return $this->noContent();
- }
- public function linkage(Request $request, string $type)
- {
- $libraryType = LibraryType::from($type);
- if (in_array($libraryType->value, [LibraryType::PROJECT->value, LibraryType::ASSET->value]) && !$request->get("id")) {
- return $this->badRequest("Parameter ID cannot be empty");
- }
- $where = match ($libraryType) {
- LibraryType::ASSET => ['asset_id' => $request->get("id")],
- LibraryType::PROJECT => ['project_id' => $request->get("id")],
- LibraryType::CUSTOM => []
- };
- $libraries = Library::query()
- ->allowed()
- ->where("type", $type)->when($where, fn($query) => $query->where($where))
- ->get(['id', 'name']);
- return $this->success([
- 'data' => $libraries
- ]);
- }
- }
|