97 lines
2.5 KiB
PHP
Raw Normal View History

2017-11-18 12:47:57 +03:00
<?php
namespace App\Http\Controllers\Api\Settings;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\ApiController;
2017-11-18 12:47:57 +03:00
use App\Http\Requests\Setting\Setting as Request;
2022-06-01 10:15:55 +03:00
use App\Http\Resources\Setting\Setting as Resource;
2017-11-18 12:47:57 +03:00
use App\Models\Setting\Setting;
class Settings extends ApiController
{
2022-03-02 12:19:46 +03:00
/**
* Instantiate a new controller instance.
*/
public function __construct()
{
// Add CRUD permission check
$this->middleware('permission:create-settings-settings')->only('create', 'store', 'duplicate', 'import');
$this->middleware('permission:read-settings-settings')->only('index', 'show', 'edit', 'export');
$this->middleware('permission:update-settings-settings')->only('update', 'enable', 'disable', 'destroy');
}
2017-11-18 12:47:57 +03:00
/**
* Display a listing of the resource.
*
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-11-18 12:47:57 +03:00
*/
public function index()
{
$settings = Setting::all();
2022-06-01 10:15:55 +03:00
return Resource::collection($settings);
2017-11-18 12:47:57 +03:00
}
/**
* Display the specified resource.
*
* @param int|string $id
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-11-18 12:47:57 +03:00
*/
public function show($id)
{
// Check if we're querying by id or key
if (is_numeric($id)) {
$setting = Setting::find($id);
} else {
$setting = Setting::where('key', $id)->first();
}
if (! $setting instanceof Setting) {
return $this->errorInternal('No query results for model [' . Setting::class . '] ' . $id);
}
2022-06-01 10:15:55 +03:00
return new Resource($setting);
2017-11-18 12:47:57 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-11-18 12:47:57 +03:00
*/
public function store(Request $request)
{
$setting = Setting::create($request->all());
2022-06-01 10:15:55 +03:00
return $this->created(route('api.settings.show', $setting->id), new Resource($setting));
2017-11-18 12:47:57 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $setting
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-11-18 12:47:57 +03:00
*/
public function update(Setting $setting, Request $request)
{
$setting->update($request->all());
2022-06-01 10:15:55 +03:00
return new Resource($setting->fresh());
2017-11-18 12:47:57 +03:00
}
/**
* Remove the specified resource from storage.
*
* @param Setting $setting
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\Response
2017-11-18 12:47:57 +03:00
*/
public function destroy(Setting $setting)
{
$setting->delete();
2022-06-01 10:15:55 +03:00
return $this->noContent();
2017-11-18 12:47:57 +03:00
}
}