Skip to content

Commit

Permalink
Add protection to isBusy method to return false if it's a new resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
murdercode committed Jan 22, 2024
1 parent 5de1821 commit 96e306c
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/App/Http/Controllers/BusyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace The3labsTeam\NovaBusyResourceField\App\Http\Controllers;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Carbon\Carbon;


class BusyController extends Controller
Expand All @@ -17,8 +17,25 @@ public function storeBusy(Request $request)
$resource->busyFrom($user);
}

protected function getResource(string $id, string $name)
{
$modelId = $id;
$modelName = '\App\Models\\'.Str::studly(Str::singular($name));

return $modelName::find($modelId);
}

public function isBusy(Request $request)
{
// If is a new resource, return false
if (! $request['model-id'] || ! $request['model-name']) {
return response()->json([
'success' => false,
'data' => null,
'lastUpdate' => null,
]);
}

$resource = $this->getResource($request['model-id'], $request['model-name']);

return response()->json([
Expand All @@ -27,12 +44,4 @@ public function isBusy(Request $request)
'lastUpdate' => $resource->isBusy() ? Carbon::parse($resource->busyData()['pivot']['updated_at'])->diffForHumans() : null,
]);
}

protected function getResource(string $id, string $name)
{
$modelId = $id;
$modelName = '\App\Models\\'.Str::studly(Str::singular($name));

return $modelName::find($modelId);
}
}

0 comments on commit 96e306c

Please sign in to comment.