AssetController.php 3.6 KB

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