| 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 |
public function index(Request $request) |
| 15 |
{ |
| 16 |
$products = Product::orderBy('id', 'DESC')->searchBy($request->get('search'))->paginate(); |
| 17 |
|
| 18 |
return [ |
| 19 |
'products' => $products |
| 20 |
]; |
| 21 |
} |
| 22 |
|
| 23 |
public function get(Request $request, $productId) |
| 24 |
{ |
| 25 |
$product = Product::findOrFail($productId); |
| 26 |
return [ |
| 27 |
'product' => $product |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
public function create(Request $request) |
| 32 |
{ |
| 33 |
$data = $request->all(); |
| 34 |
$this->validate($data, [ |
| 35 |
'title' => 'required' |
| 36 |
]); |
| 37 |
|
| 38 |
$data = wp_unslash($data); |
| 39 |
$product = Product::create($data); |
| 40 |
|
| 41 |
return [ |
| 42 |
'message' => __('Product has been successfully created', 'fluent-support'), |
| 43 |
'product' => $product |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
public function update(Request $request, $productId) |
| 48 |
{ |
| 49 |
$data = $request->all(); |
| 50 |
$this->validate($data, [ |
| 51 |
'title' => 'required' |
| 52 |
]); |
| 53 |
|
| 54 |
$product = Product::findOrFail($productId); |
| 55 |
$product->fill($data); |
| 56 |
$product->save(); |
| 57 |
|
| 58 |
return [ |
| 59 |
'message' => __('Product has been updated', 'fluent-support'), |
| 60 |
'product' => Product::find($productId) |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
public function delete(Request $request, $productId) |
| 65 |
{ |
| 66 |
Product::where('id', $productId) |
| 67 |
->delete(); |
| 68 |
|
| 69 |
return [ |
| 70 |
'message' => __('Product has been deleted', 'fluent-support') |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|