123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Http\Controllers\API;
- use App\Http\Controllers\Controller;
- use App\Http\Requests\API\Asset\CreateOrUpdateRequest;
- use App\Http\Resources\API\AssetReportResource;
- use App\Http\Resources\API\AssetResource;
- use App\Models\Asset;
- use App\Models\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- class AssetController extends Controller
- {
- /**
- * Display a listing of the resource.
- */
- public function index(Request $request)
- {
- $assets = Asset::filter($request->all())
- ->where("parent_id", 0)
- ->where('company_id',Auth::user()->company_id)
- ->with(['children','children.children'])
- ->paginate();
- return AssetResource::collection($assets);
- }
- /**
- * Store a newly created resource in storage.
- */
- public function store(CreateOrUpdateRequest $request)
- {
- $asset = Asset::create([
- ...$request->all(),
- 'company_id' => Auth::user()->company_id,
- 'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null,
- 'created_by' => Auth::id(),
- ]);
- $parentAsset = $request->parent_id > 0
- ? Asset::query()->findOrFail($request->parent_id)
- : null;
- $asset->path = $parentAsset ? $parentAsset?->path . $asset->id . "," : sprintf(",%s,", $asset->id);
- $asset->save();
- return $this->created();
- }
- /**
- * Display the specified resource.
- */
- public function show(string $id)
- {
- $asset = Asset::allowed()->findOrFail($id);
- return new AssetResource($asset);
- }
- /**
- * Update the specified resource in storage.
- */
- public function update(Request $request, string $id)
- {
- $asset = Asset::allowed()->findOrFail($id);
- $parentAsset = $request->parent_id > 0
- ? Asset::query()->findOrFail($request->parent_id)
- : null;
- $isUpdateParent = $parentAsset?->id != $asset->parent_id;
- $path = $isUpdateParent ? $parentAsset->path . $asset->id . "," : $asset->path;
- $formData = [
- ...$request->all(),
- 'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null,
- 'path' => $path
- ];
- if ($isUpdateParent) {
- $children = Asset::query()
- ->where("id", "!=", $asset->id)
- ->where("path", "like", "%," . $asset->id . ",%")
- ->get();
- foreach ($children as $child) {
- $child->fill([
- 'path' => str_replace($asset->path, $path, $child->path),
- ]);
- $child->save();
- }
- }
- $asset->fill($formData);
- $asset->save();
- return $this->noContent();
- }
- /**
- * Remove the specified resource from storage.
- */
- public function destroy(string $id)
- {
- $asset = Asset::allowed()->findOrFail($id);
- if(!empty($asset->children()->first())){
- throw new \Exception("Please remove the sub-level assets.");
- }
- $asset->delete();
- return $this->noContent();
- }
- public function report(string $id){
- $asset = Asset::allowed()->with(['requirements','plans','projects','children'])->findOrFail($id);
- //$asset = Asset::find($id);
- $assets=Asset::query()->where("path","like","%,".$id.",%")->pluck("id");
- $asset->setAttribute('child_id',$assets);
- return new AssetReportResource($asset);
- }
- }
|