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