140 lines
4.5 KiB
PHP
140 lines
4.5 KiB
PHP
<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;
|
|
|
|
use Cms\Classes\Controller;
|
|
use BackendMenu;
|
|
|
|
use Illuminate\Http\Request;
|
|
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Lovata\Shopaholic\Models\Product;
|
|
class ProductAPIController extends Controller
|
|
{
|
|
protected $Product;
|
|
|
|
protected $helpers;
|
|
|
|
public function __construct(Product $Product, Helpers $helpers)
|
|
{
|
|
parent::__construct();
|
|
$this->Product = $Product;
|
|
$this->helpers = $helpers;
|
|
}
|
|
|
|
public function index(){
|
|
|
|
$data = $this->Product
|
|
->select("id", "name", "slug", "category_id", "featured", "description")
|
|
//->with(['preview_image' => function ($query) {
|
|
// $query->select("id", "path");
|
|
// }])
|
|
->with('preview_image')
|
|
//->with('offer:id,main_price')
|
|
//->with('images')
|
|
->with('translations:locale,model_id,attribute_data')
|
|
->with(['offer' => function ($query) {
|
|
$query->with('main_price');
|
|
}])
|
|
->active()
|
|
->paginate(10);
|
|
|
|
if($data) {
|
|
$data->each(function ($item, $key) {
|
|
$item->icon = $this->pageUrl('index') . \Config::get('cms.storage.media.path') . $item->icon;
|
|
});
|
|
}
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
|
}
|
|
|
|
public function ByCatId($id){
|
|
|
|
$data = $this->Product
|
|
->select("id", "name", "slug", "category_id", "featured")
|
|
->with('preview_image')
|
|
->with('translations:locale,model_id,attribute_data')
|
|
->where("category_id", $id)
|
|
->with(['offer' => function ($query) {
|
|
$query->with('main_price');
|
|
}])
|
|
->active()
|
|
->paginate(10);
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
|
}
|
|
|
|
|
|
public function show($id){
|
|
|
|
$data = $this->Product::with('translations:locale,model_id,attribute_data')
|
|
->with(['category' => function ($query) {
|
|
$query->with('translations:locale,model_id,attribute_data');
|
|
}])
|
|
->find($id);
|
|
|
|
|
|
if ($data){
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]);
|
|
} else {
|
|
$this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
|
|
}
|
|
|
|
}
|
|
|
|
// public function store(Request $request){
|
|
|
|
// $arr = $request->all();
|
|
|
|
// while ( $data = current($arr)) {
|
|
// $this->Product->{key($arr)} = $data;
|
|
// next($arr);
|
|
// }
|
|
|
|
// $validation = Validator::make($request->all(), $this->Product->rules);
|
|
|
|
// if( $validation->passes() ){
|
|
// $this->Product->save();
|
|
// return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->Product->id]);
|
|
// }else{
|
|
// return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors() );
|
|
// }
|
|
|
|
// }
|
|
|
|
// public function update($id, Request $request){
|
|
|
|
// $status = $this->Product->where('id',$id)->update($data);
|
|
|
|
// if( $status ){
|
|
|
|
// return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been updated successfully.');
|
|
|
|
// }else{
|
|
|
|
// return $this->helpers->apiArrayResponseBuilder(400, 'bad request', 'Error, data failed to update.');
|
|
|
|
// }
|
|
// }
|
|
|
|
// public function delete($id){
|
|
|
|
// $this->Product->where('id',$id)->delete();
|
|
|
|
// return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
|
|
// }
|
|
|
|
// public function destroy($id){
|
|
|
|
// $this->Product->where('id',$id)->delete();
|
|
|
|
// return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
|
|
// }
|
|
|
|
|
|
public static function getAfterFilters() {return [];}
|
|
public static function getBeforeFilters() {return [];}
|
|
public static function getMiddleware() {return [];}
|
|
public function callAction($method, $parameters=false) {
|
|
return call_user_func_array(array($this, $method), $parameters);
|
|
}
|
|
|
|
} |