|
@@ -0,0 +1,201 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers\API;
|
|
|
+
|
|
|
+use App\Http\Controllers\Controller;
|
|
|
+use App\Http\Requests\API\Folder\CreateRequest;
|
|
|
+use App\Http\Requests\API\Folder\UpdateRequest;
|
|
|
+use App\Http\Resources\API\FolderDetailResource;
|
|
|
+use App\Models\Folder;
|
|
|
+use App\Models\Library;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+class FolderController extends Controller
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * Display a listing of the resource.
|
|
|
+ */
|
|
|
+ public function library($libraryId)
|
|
|
+ {
|
|
|
+ $folders = Folder::query()
|
|
|
+ ->where("library_id", $libraryId)
|
|
|
+ ->when(request("parent_id", 0) > 0, function ($query) {
|
|
|
+ return $query->where("path", "like", "%," . \request("parent_id") . ",%")->where("id", "!=", \request("parent_id"));
|
|
|
+ })
|
|
|
+ ->orderByDesc("sequence")
|
|
|
+ ->get([
|
|
|
+ 'id',
|
|
|
+ 'name',
|
|
|
+ 'parent_id'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return $this->success([
|
|
|
+ 'data' => make_tree($folders->toArray(), \request("parent_id", 0)),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Store a newly created resource in storage.
|
|
|
+ */
|
|
|
+ public function store(CreateRequest $request)
|
|
|
+ {
|
|
|
+ $library = Library::query()->findOrFail($request->library_id);
|
|
|
+
|
|
|
+ $parentFolder = $request->parent_id > 0
|
|
|
+ ? Folder::query()->where("library_id", $library->id)->findOrFail($request->parent_id)
|
|
|
+ : null;
|
|
|
+
|
|
|
+ $names = [];
|
|
|
+ $updateFolders = [];
|
|
|
+
|
|
|
+ foreach ($request->items as $item) {
|
|
|
+ if (! isset($item['name']) || !$item['name']) {
|
|
|
+ return $this->badRequest("Folder name cannot be empty");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (in_array($item['name'], $names)) {
|
|
|
+ return $this->badRequest("Folder names must be unique");
|
|
|
+ }
|
|
|
+ $names[] = $item['name'];
|
|
|
+
|
|
|
+ $isUpdate = isset($item['id']) && $item['id'];
|
|
|
+
|
|
|
+ $count = Folder::query()
|
|
|
+ ->where("library_id", $library->id)
|
|
|
+ ->where("parent_id", $request->parent_id)
|
|
|
+ ->when($isUpdate, function ($query) use ($item) {
|
|
|
+ return $query->where("id", "!=", $item['id']);
|
|
|
+ })
|
|
|
+ ->where("name", $item['name'])
|
|
|
+ ->count();
|
|
|
+ if ($count > 0) {
|
|
|
+ return $this->badRequest("Folder names must be unique");
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($isUpdate) {
|
|
|
+ $folder = Folder::query()
|
|
|
+ ->where("library_id", $library->id)
|
|
|
+ ->where("parent_id", $request->parent_id)
|
|
|
+ ->find($item['id']);
|
|
|
+ if (! $folder) {
|
|
|
+ return $this->badRequest("Illegal parameters or the file relationship that needs to be updated has changed.");
|
|
|
+ }
|
|
|
+
|
|
|
+ $updateFolders[$item['id']] = $folder;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($request->items as $item) {
|
|
|
+ $isUpdate = isset($item['id']) && $item['id'];
|
|
|
+ $data = [
|
|
|
+ 'name' => $item['name'],
|
|
|
+ 'sequence' => data_get($item, "sequence", 0),
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($isUpdate) {
|
|
|
+ $folder = $updateFolders[$item['id']];
|
|
|
+ $folder->fill($data);
|
|
|
+ $folder->save();
|
|
|
+ } else {
|
|
|
+ $folder = Folder::query()->create([
|
|
|
+ 'company_id' => Auth::user()->company_id,
|
|
|
+ 'library_id' => $library->id,
|
|
|
+ 'parent_id' => $request->parent_id,
|
|
|
+ ...$data
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $folder->path = $parentFolder ? $parentFolder?->path . $folder->id . "," : sprintf(",%s,", $folder->id);
|
|
|
+ $folder->save();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->created();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Display the specified resource.
|
|
|
+ */
|
|
|
+ public function show(string $id)
|
|
|
+ {
|
|
|
+ $folder = Folder::query()->findOrFail($id);
|
|
|
+
|
|
|
+
|
|
|
+ return new FolderDetailResource($folder);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Update the specified resource in storage.
|
|
|
+ */
|
|
|
+ public function update(UpdateRequest $request, string $id)
|
|
|
+ {
|
|
|
+ $folder = Folder::query()->findOrFail($id);
|
|
|
+
|
|
|
+ $library = Library::query()->findOrFail($request->library_id);
|
|
|
+
|
|
|
+ $parentFolder = $request->parent_id > 0
|
|
|
+ ? Folder::query()->where("library_id", $library->id)->findOrFail($request->parent_id)
|
|
|
+ : null;
|
|
|
+
|
|
|
+ $count = Folder::query()
|
|
|
+ ->where("library_id", $request->id)
|
|
|
+ ->where("parent_id", $request->parent_id)
|
|
|
+ ->where("name", $request->name)
|
|
|
+ ->where("id", "!=", $folder->id)
|
|
|
+ ->count();
|
|
|
+ if ($count > 0) {
|
|
|
+ return $this->badRequest("Folder names must be unique");
|
|
|
+ }
|
|
|
+
|
|
|
+ $fields = ['name', 'sequence'];
|
|
|
+ if ($parentFolder?->id == $folder->parent_id) {
|
|
|
+ $folder->fill($request->only($fields));
|
|
|
+ $folder->save();
|
|
|
+ } else {
|
|
|
+ $path = $parentFolder->path . $folder->id . ",";
|
|
|
+ $folderData = [
|
|
|
+ ...$request->only($fields),
|
|
|
+ 'path' => $path,
|
|
|
+ 'parent_id' => $request->parent_id,
|
|
|
+ 'library_id' => $library->id,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $children = Folder::query()
|
|
|
+ ->where("library_id", $library->id)
|
|
|
+ ->where("id", "!=", $folder->id)
|
|
|
+ ->where("path", "like", "%," . $folder->id . ",%")
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ foreach ($children as $child) {
|
|
|
+ $child->fill([
|
|
|
+ 'path' => str_replace($folder->path, $path, $child->path),
|
|
|
+ 'library_id' => $library->id,
|
|
|
+ ]);
|
|
|
+ $child->save();
|
|
|
+ }
|
|
|
+
|
|
|
+ $folder->fill($folderData);
|
|
|
+ $folder->save();
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->noContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Remove the specified resource from storage.
|
|
|
+ */
|
|
|
+ public function destroy(string $id)
|
|
|
+ {
|
|
|
+ $folder = Folder::query()->findOrFail($id);
|
|
|
+
|
|
|
+ $children = Folder::query()->where("parent_id", $folder->id)->count();
|
|
|
+ if ($children > 0) {
|
|
|
+ return $this->badRequest("Subordinate folders or containers exist and are not allowed to be deleted.");
|
|
|
+ }
|
|
|
+
|
|
|
+ $folder->delete();
|
|
|
+
|
|
|
+ return $this->noContent();
|
|
|
+ }
|
|
|
+}
|