ProjectController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Project\CreateOrUpdateRequest;
  5. use App\Http\Requests\API\Project\PostponeRequest;
  6. use App\Http\Resources\API\ProjectResource;
  7. use App\Models\Enums\ProjectStatus;
  8. use App\Models\Project;
  9. use App\Models\ProjectAsset;
  10. use App\Models\ProjectPlan;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use function MongoDB\BSON\toJSON;
  14. class ProjectController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. */
  19. public function index(Request $request)
  20. {
  21. $projects = Project::filter($request->all())->get();
  22. return ProjectResource::collection($projects);
  23. }
  24. /**
  25. * Store a newly created resource in storage.
  26. */
  27. public function store(CreateOrUpdateRequest $request)
  28. {
  29. $project = new Project();
  30. $project->mergeFillable([
  31. 'company_id', 'created_by'
  32. ]);
  33. $project->fill([
  34. ...$request->all(),
  35. 'company_id' => Auth::user()->company_id,
  36. 'created_by' => Auth::id(),
  37. 'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null,
  38. ]);
  39. $project->save();
  40. if ($request->has("assets")) {
  41. foreach ($request->get("assets", []) as $assetId) {
  42. ProjectAsset::create([
  43. 'project_id' => $project->id,
  44. 'asset_id' => $assetId,
  45. ]);
  46. }
  47. }
  48. if ($request->has("plans")) {
  49. foreach ($request->get("plans", []) as $planId) {
  50. ProjectPlan::create([
  51. 'project_id' => $project->id,
  52. 'plan_id' => $planId,
  53. ]);
  54. }
  55. }
  56. return $this->created();
  57. }
  58. /**
  59. * Display the specified resource.
  60. */
  61. public function show(string $id)
  62. {
  63. //
  64. }
  65. /**
  66. * Update the specified resource in storage.
  67. */
  68. public function update(CreateOrUpdateRequest $request, string $id)
  69. {
  70. $project = Project::findOrFail($id);
  71. $project->fill([
  72. ...$request->all(),
  73. 'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null,
  74. ]);
  75. $project->save();
  76. if ($request->has("assets")) {
  77. ProjectAsset::where('project_id', $project->id)->delete();
  78. foreach ($request->get("assets", []) as $assetId) {
  79. ProjectAsset::create([
  80. 'project_id' => $project->id,
  81. 'asset_id' => $assetId,
  82. ]);
  83. }
  84. }
  85. if ($request->has("plans")) {
  86. ProjectPlan::where('project_id', $project->id)->delete();
  87. foreach ($request->get("plans", []) as $planId) {
  88. ProjectPlan::create([
  89. 'project_id' => $project->id,
  90. 'plan_id' => $planId,
  91. ]);
  92. }
  93. }
  94. return $this->noContent();
  95. }
  96. /**
  97. * Remove the specified resource from storage.
  98. */
  99. public function destroy(string $id)
  100. {
  101. $project = Project::findOrFail($id);
  102. $project->delete();
  103. return $this->noContent();
  104. }
  105. public function closed(string $id)
  106. {
  107. $project = Project::findOrFail($id);
  108. $project->status = ProjectStatus::CLOSED->value;
  109. $project->save();
  110. return $this->noContent();
  111. }
  112. public function start(string $id)
  113. {
  114. $project = Project::findOrFail($id);
  115. $project->status = ProjectStatus::DOING->value;
  116. $project->save();
  117. return $this->noContent();
  118. }
  119. public function pause(string $id)
  120. {
  121. $project = Project::findOrFail($id);
  122. $project->status = ProjectStatus::PAUSE->value;
  123. $project->save();
  124. return $this->noContent();
  125. }
  126. public function postpone(PostponeRequest $request, string $id)
  127. {
  128. $project = Project::findOrFail($id);
  129. $project->fill($request->only([
  130. 'begin', 'end', 'available_days'
  131. ]));
  132. $project->save();
  133. return $this->noContent();
  134. }
  135. }