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;
2022-06-01 10:15:55 +03:00
use App\Http\Resources\Setting\Category as Resource;
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;
class Categories extends ApiController
{
/**
* Display a listing of the resource.
*
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
public function index()
{
2022-06-01 10:15:55 +03:00
$categories = Category::withSubCategory()->collect();
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return Resource::collection($categories);
2017-09-14 22:21:00 +03:00
}
/**
* Display the specified resource.
*
* @param Category $category
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
public function show(Category $category)
{
2022-06-01 10:15:55 +03:00
return new Resource($category);
2017-09-14 22:21:00 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
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
2022-06-01 10:15:55 +03:00
return $this->created(route('api.categories.show', $category->id), new Resource($category));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $category
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
public function update(Category $category, Request $request)
{
2019-11-16 10:21:14 +03:00
try {
$category = $this->dispatch(new UpdateCategory($category, $request));
2022-06-01 10:15:55 +03:00
return new Resource($category->fresh());
2019-11-16 10:21:14 +03:00
} catch(\Exception $e) {
2022-06-01 10:15:55 +03:00
$this->errorUnauthorized($e->getMessage());
2019-11-16 10:21:14 +03:00
}
}
/**
* Enable the specified resource in storage.
*
* @param Category $category
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function enable(Category $category)
{
$category = $this->dispatch(new UpdateCategory($category, request()->merge(['enabled' => 1])));
2022-06-01 10:15:55 +03:00
return new Resource($category->fresh());
2019-11-16 10:21:14 +03:00
}
/**
* Disable the specified resource in storage.
*
* @param Category $category
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function disable(Category $category)
{
try {
$category = $this->dispatch(new UpdateCategory($category, request()->merge(['enabled' => 0])));
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return new Resource($category->fresh());
2019-11-16 10:21:14 +03:00
} catch(\Exception $e) {
2022-06-01 10:15:55 +03:00
$this->errorUnauthorized($e->getMessage());
2019-11-16 10:21:14 +03:00
}
2017-09-14 22:21:00 +03:00
}
/**
* Remove the specified resource from storage.
*
* @param Category $category
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\Response
2017-09-14 22:21:00 +03:00
*/
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
2022-06-01 10:15:55 +03:00
return $this->noContent();
2019-11-16 10:21:14 +03:00
} catch(\Exception $e) {
2022-06-01 10:15:55 +03:00
$this->errorUnauthorized($e->getMessage());
2019-11-16 10:21:14 +03:00
}
2017-09-14 22:21:00 +03:00
}
}