| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Customer; |
| 6 |
use FluentSupport\App\Models\Product; |
| 7 |
use FluentSupport\App\Models\Conversation; |
| 8 |
use FluentSupport\App\Models\Ticket; |
| 9 |
use FluentSupport\Framework\Request\Request; |
| 10 |
use FluentSupport\Framework\Support\Arr; |
| 11 |
|
| 12 |
class ProductController extends Controller |
| 13 |
{ |
| 14 |
/** |
| 15 |
* index method will return the list of product |
| 16 |
* @param Request $request |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
public function index(Request $request) |
| 20 |
{ |
| 21 |
$products = Product::orderBy('id', 'DESC')->searchBy($request->get('search'))->paginate(); |
| 22 |
|
| 23 |
return [ |
| 24 |
'products' => $products |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* get method will get product by id and return |
| 30 |
* @param Request $request |
| 31 |
* @param $productId |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function get(Request $request, $productId) |
| 35 |
{ |
| 36 |
$product = Product::findOrFail($productId); |
| 37 |
return [ |
| 38 |
'product' => $product |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* creare method will create new product |
| 44 |
* @param Request $request |
| 45 |
* @return array |
| 46 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 47 |
*/ |
| 48 |
public function create(Request $request) |
| 49 |
{ |
| 50 |
$data = $request->all(); |
| 51 |
$this->validate($data, [ |
| 52 |
'title' => 'required' |
| 53 |
]); |
| 54 |
|
| 55 |
$data = wp_unslash($data); |
| 56 |
$product = Product::create($data); |
| 57 |
|
| 58 |
return [ |
| 59 |
'message' => __('Product has been successfully created', 'fluent-support'), |
| 60 |
'product' => $product |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* update methd will update an exiting product by id |
| 67 |
* @param Request $request |
| 68 |
* @param $productId |
| 69 |
* @return array |
| 70 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 71 |
*/ |
| 72 |
public function update(Request $request, $productId) |
| 73 |
{ |
| 74 |
$data = $request->all(); |
| 75 |
$this->validate($data, [ |
| 76 |
'title' => 'required' |
| 77 |
]); |
| 78 |
|
| 79 |
$product = Product::findOrFail($productId); |
| 80 |
$product->fill($data); |
| 81 |
$product->save(); |
| 82 |
|
| 83 |
return [ |
| 84 |
'message' => __('Product has been updated', 'fluent-support'), |
| 85 |
'product' => Product::find($productId) |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* delete method will delete an existing product by id |
| 91 |
* @param Request $request |
| 92 |
* @param $productId |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function delete(Request $request, $productId) |
| 96 |
{ |
| 97 |
Product::where('id', $productId) |
| 98 |
->delete(); |
| 99 |
|
| 100 |
return [ |
| 101 |
'message' => __('Product has been deleted', 'fluent-support') |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|