AssetController.php 3.5 KB

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