113 lines
2.7 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2018-06-10 02:48:51 +03:00
namespace App\Http\Controllers\Api\Common;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\ApiController;
2018-06-10 02:48:51 +03:00
use App\Http\Requests\Common\Item as Request;
2019-11-16 10:21:14 +03:00
use App\Jobs\Common\CreateItem;
use App\Jobs\Common\DeleteItem;
use App\Jobs\Common\UpdateItem;
2018-06-10 02:48:51 +03:00
use App\Models\Common\Item;
use App\Transformers\Common\Item as Transformer;
2019-11-16 10:21:14 +03:00
use App\Traits\Uploads;
2017-09-14 22:21:00 +03:00
class Items extends ApiController
{
2019-11-16 10:21:14 +03:00
use Uploads;
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return \Dingo\Api\Http\Response
*/
public function index()
{
$items = Item::with(['category', 'tax'])->collect();
return $this->response->paginator($items, new Transformer());
}
/**
* Display the specified resource.
*
2017-10-08 23:25:44 +03:00
* @param int|string $id
2017-09-14 22:21:00 +03:00
* @return \Dingo\Api\Http\Response
*/
2017-10-08 23:25:44 +03:00
public function show($id)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
$item = Item::with(['category', 'tax'])->find($id);
2017-10-08 23:25:44 +03:00
2017-09-14 22:21:00 +03:00
return $this->response->item($item, 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
$item = $this->dispatch(new CreateItem($request));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $this->response->created(url('api/items/' . $item->id));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $item
* @param $request
* @return \Dingo\Api\Http\Response
*/
public function update(Item $item, Request $request)
{
2019-11-16 10:21:14 +03:00
$item = $this->dispatch(new UpdateItem($item, $request));
return $this->item($item->fresh(), new Transformer());
}
/**
* Enable the specified resource in storage.
*
* @param Item $item
* @return \Dingo\Api\Http\Response
*/
public function enable(Item $item)
{
$item = $this->dispatch(new UpdateItem($item, request()->merge(['enabled' => 1])));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $this->item($item->fresh(), new Transformer());
}
/**
* Disable the specified resource in storage.
*
* @param Item $item
* @return \Dingo\Api\Http\Response
*/
public function disable(Item $item)
{
$item = $this->dispatch(new UpdateItem($item, request()->merge(['enabled' => 0])));
return $this->item($item->fresh(), new Transformer());
2017-09-14 22:21:00 +03:00
}
/**
* Remove the specified resource from storage.
*
* @param Item $item
* @return \Dingo\Api\Http\Response
*/
public function destroy(Item $item)
{
2019-11-16 10:21:14 +03:00
try {
$this->dispatch(new DeleteItem($item));
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
}
}