123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App\Http\Controllers\API;
- use App\Http\Controllers\Controller;
- use App\Http\Requests\API\Project\CreateOrUpdateRequest;
- use App\Http\Requests\API\Project\PostponeRequest;
- use App\Http\Resources\API\ProjectResource;
- use App\Models\Enums\ProjectStatus;
- use App\Models\Project;
- use App\Models\ProjectAsset;
- use App\Models\ProjectPlan;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use function MongoDB\BSON\toJSON;
- class ProjectController extends Controller
- {
- /**
- * Display a listing of the resource.
- */
- public function index(Request $request)
- {
- $projects = Project::filter($request->all())->get();
- return ProjectResource::collection($projects);
- }
- /**
- * Store a newly created resource in storage.
- */
- public function store(CreateOrUpdateRequest $request)
- {
- $project = new Project();
- $project->mergeFillable([
- 'company_id', 'created_by'
- ]);
- $project->fill([
- ...$request->all(),
- 'company_id' => Auth::user()->company_id,
- 'created_by' => Auth::id(),
- 'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null,
- ]);
- $project->save();
- if ($request->has("assets")) {
- foreach ($request->get("assets", []) as $assetId) {
- ProjectAsset::create([
- 'project_id' => $project->id,
- 'asset_id' => $assetId,
- ]);
- }
- }
- if ($request->has("plans")) {
- foreach ($request->get("plans", []) as $planId) {
- ProjectPlan::create([
- 'project_id' => $project->id,
- 'plan_id' => $planId,
- ]);
- }
- }
- return $this->created();
- }
- /**
- * Display the specified resource.
- */
- public function show(string $id)
- {
- //
- }
- /**
- * Update the specified resource in storage.
- */
- public function update(CreateOrUpdateRequest $request, string $id)
- {
- $project = Project::findOrFail($id);
- $project->fill([
- ...$request->all(),
- 'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null,
- ]);
- $project->save();
- if ($request->has("assets")) {
- ProjectAsset::where('project_id', $project->id)->delete();
- foreach ($request->get("assets", []) as $assetId) {
- ProjectAsset::create([
- 'project_id' => $project->id,
- 'asset_id' => $assetId,
- ]);
- }
- }
- if ($request->has("plans")) {
- ProjectPlan::where('project_id', $project->id)->delete();
- foreach ($request->get("plans", []) as $planId) {
- ProjectPlan::create([
- 'project_id' => $project->id,
- 'plan_id' => $planId,
- ]);
- }
- }
- return $this->noContent();
- }
- /**
- * Remove the specified resource from storage.
- */
- public function destroy(string $id)
- {
- $project = Project::findOrFail($id);
- $project->delete();
- return $this->noContent();
- }
- public function closed(string $id)
- {
- $project = Project::findOrFail($id);
- $project->status = ProjectStatus::CLOSED->value;
- $project->save();
- return $this->noContent();
- }
- public function start(string $id)
- {
- $project = Project::findOrFail($id);
- $project->status = ProjectStatus::DOING->value;
- $project->save();
- return $this->noContent();
- }
- public function pause(string $id)
- {
- $project = Project::findOrFail($id);
- $project->status = ProjectStatus::PAUSE->value;
- $project->save();
- return $this->noContent();
- }
- public function postpone(PostponeRequest $request, string $id)
- {
- $project = Project::findOrFail($id);
- $project->fill($request->only([
- 'begin', 'end', 'available_days'
- ]));
- $project->save();
- return $this->noContent();
- }
- }
|