AssetController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Asset\CreateOrUpdateRequest;
  5. use App\Http\Resources\API\AssetReportResource;
  6. use App\Http\Resources\API\AssetResource;
  7. use App\Models\Asset;
  8. use App\Models\User;
  9. use App\Services\File\ImageUrlService;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. class AssetController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. */
  17. public function index(Request $request)
  18. {
  19. $pageSize=$request->get('page_size') ?? 10;
  20. $assets = Asset::filter($request->all())
  21. ->where("parent_id", 0)
  22. ->where('company_id',Auth::user()->company_id)
  23. ->with(['children','children.children'])
  24. ->paginate($pageSize);
  25. return AssetResource::collection($assets);
  26. }
  27. /**
  28. * Store a newly created resource in storage.
  29. */
  30. public function store(CreateOrUpdateRequest $request)
  31. {
  32. $asset = Asset::create([
  33. ...$request->all(),
  34. 'company_id' => Auth::user()->company_id,
  35. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  36. 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null,
  37. 'created_by' => Auth::id(),
  38. ]);
  39. $parentAsset = $request->parent_id > 0
  40. ? Asset::query()->findOrFail($request->parent_id)
  41. : null;
  42. $asset->path = $parentAsset ? $parentAsset?->path . $asset->id . "," : sprintf(",%s,", $asset->id);
  43. $asset->save();
  44. return $this->created();
  45. }
  46. /**
  47. * Display the specified resource.
  48. */
  49. public function show(string $id)
  50. {
  51. $asset = Asset::allowed()->findOrFail($id);
  52. return new AssetResource($asset);
  53. }
  54. /**
  55. * Update the specified resource in storage.
  56. */
  57. public function update(Request $request, string $id)
  58. {
  59. $asset = Asset::allowed()->findOrFail($id);
  60. $parentAsset = $request->parent_id > 0
  61. ? Asset::query()->findOrFail($request->parent_id)
  62. : null;
  63. $isUpdateParent = $parentAsset?->id != $asset->parent_id;
  64. $path = $isUpdateParent ? $parentAsset->path . $asset->id . "," : $asset->path;
  65. $formData = [
  66. ...$request->all(),
  67. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  68. 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null,
  69. 'path' => $path
  70. ];
  71. if ($isUpdateParent) {
  72. $children = Asset::query()
  73. ->where("id", "!=", $asset->id)
  74. ->where("path", "like", "%," . $asset->id . ",%")
  75. ->get();
  76. foreach ($children as $child) {
  77. $child->fill([
  78. 'path' => str_replace($asset->path, $path, $child->path),
  79. ]);
  80. $child->save();
  81. }
  82. }
  83. $asset->fill($formData);
  84. $asset->save();
  85. return $this->noContent();
  86. }
  87. /**
  88. * Remove the specified resource from storage.
  89. */
  90. public function destroy(string $id)
  91. {
  92. $asset = Asset::allowed()->findOrFail($id);
  93. if(!empty($asset->children()->first())){
  94. throw new \Exception("Please remove the sub-level assets.");
  95. }
  96. $asset->delete();
  97. return $this->noContent();
  98. }
  99. public function report(string $id){
  100. $asset = Asset::allowed()->with(['requirements','plans','projects','children'])->findOrFail($id);
  101. //$asset = Asset::find($id);
  102. $assets=Asset::query()->where("path","like","%,".$id.",%")->pluck("id");
  103. $asset->setAttribute('child_id',$assets);
  104. return new AssetReportResource($asset);
  105. }
  106. public function tree()
  107. {
  108. $assets = Asset::allowed()->get(['id', 'name', 'parent_id'])->each(function ($assets){
  109. $assets->type='asset';
  110. $assets->uniId=$assets->type.'_'.$assets->id;
  111. });
  112. return $this->success([
  113. 'data' => make_tree($assets->toArray())
  114. ]);
  115. }
  116. }