Controllers
Creation of a controller
class ThreadsApiController extends BaseApiController
{
private ThreadServiceInterface $threadService;
public function __construct(ThreadServiceInterface $threadService)
{
$this->threadService = $threadService;
}
public function index()
{
abort_if(Gate::denies('thread_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () {
return $this->sendResponse(
new BasePaginatedCollectionResource($this->threadService->index(), ThreadResource::class)
);
});
}
public function create()
{
abort_if(Gate::denies('thread_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () {
return $this->sendResponse(
[],
false
);
});
}
public function store(StoreThreadRequest $request)
{
abort_if(Gate::denies('thread_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(
function () use ($request) {
return $this->sendResponse(
new ThreadResource($this->threadService->store(
$request->input('name'),
$request->input('status'),
$request->input('description'),
$request->input('color'),
$request->input('channel_id')
))
);
},
true
);
}
public function edit(Thread $thread)
{
abort_if(Gate::denies('thread_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () use ($thread) {
return $this->sendResponse(
new ThreadResource($this->threadService->edit($thread))
);
});
}
public function show(Thread $thread)
{
abort_if(Gate::denies('thread_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () use ($thread) {
return $this->sendResponse(
new ThreadResource($this->threadService->show($thread))
);
});
}
public function update(UpdateThreadRequest $request, Thread $thread)
{
abort_if(Gate::denies('thread_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(
function () use ($request, $thread) {
return $this->sendResponse(
new ThreadResource($this->threadService->update(
$thread,
$request->input('name'),
$request->input('status'),
$request->input('description'),
$request->input('color'),
$request->input('channel_id')
))
);
},
true
);
}
public function destroy(Thread $thread)
{
abort_if(Gate::denies('thread_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () use ($thread) {
$this->threadService->destroy($thread);
return $this->sendResponse(
[],
false
);
}, true);
}
}Last updated