116 lines
3.0 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Api\Settings;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\ApiController;
2017-09-14 22:21:00 +03:00
use App\Http\Requests\Setting\Category as Request;
2019-11-16 10:21:14 +03:00
use App\Jobs\Setting\CreateCategory;
use App\Jobs\Setting\DeleteCategory;
use App\Jobs\Setting\UpdateCategory;
2017-09-14 22:21:00 +03:00
use App\Models\Setting\Category;
2017-10-16 21:02:32 +03:00
use App\Transformers\Setting\Category as Transformer;
2017-09-14 22:21:00 +03:00
class Categories extends ApiController
{
/**
* Display a listing of the resource.
*
* @return \Dingo\Api\Http\Response
*/
public function index()
{
$categories = Category::collect();
return $this->response->paginator($categories, new Transformer());
}
/**
* Display the specified resource.
*
* @param Category $category
* @return \Dingo\Api\Http\Response
*/
public function show(Category $category)
{
return $this->response->item($category, new Transformer());
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return \Dingo\Api\Http\Response
*/
public function store(Request $request)
{
2019-11-16 10:21:14 +03:00
$category = $this->dispatch(new CreateCategory($request));
2017-09-14 22:21:00 +03:00
2020-08-11 16:52:10 +03:00
return $this->response->created(route('api.categories.show', $category->id));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $category
* @param $request
* @return \Dingo\Api\Http\Response
*/
public function update(Category $category, Request $request)
{
2019-11-16 10:21:14 +03:00
try {
$category = $this->dispatch(new UpdateCategory($category, $request));
return $this->item($category->fresh(), new Transformer());
} catch(\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
}
/**
* Enable the specified resource in storage.
*
* @param Category $category
* @return \Dingo\Api\Http\Response
*/
public function enable(Category $category)
{
$category = $this->dispatch(new UpdateCategory($category, request()->merge(['enabled' => 1])));
return $this->item($category->fresh(), new Transformer());
}
/**
* Disable the specified resource in storage.
*
* @param Category $category
* @return \Dingo\Api\Http\Response
*/
public function disable(Category $category)
{
try {
$category = $this->dispatch(new UpdateCategory($category, request()->merge(['enabled' => 0])));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $this->item($category->fresh(), new Transformer());
} catch(\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
2017-09-14 22:21:00 +03:00
}
/**
* Remove the specified resource from storage.
*
* @param Category $category
* @return \Dingo\Api\Http\Response
*/
public function destroy(Category $category)
{
2019-11-16 10:21:14 +03:00
try {
$this->dispatch(new DeleteCategory($category));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $this->response->noContent();
} catch(\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
2017-09-14 22:21:00 +03:00
}
}