| @@ -1,74 +1,73 @@ | ||
| 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 | - public function index(Request $request) | |
| 11 | + /** | |
| 12 | + * index method will return the list of product | |
| 13 | + * @param Request $request | |
| 14 | + * @param Product $product | |
| 15 | + * @return array | |
| 16 | + */ | |
| 17 | + public function index( Request $request, Product $product ) | |
| 15 | 18 | { |
| 16 | - $products = Product::orderBy('id', 'DESC')->searchBy($request->get('search'))->paginate(); | |
| 17 | - | |
| 18 | - return [ | |
| 19 | - 'products' => $products | |
| 20 | - ]; | |
| 19 | + return $product->getProducts( $request->getSafe('search', 'sanitize_text_field') ); | |
| 21 | 20 | } |
| 22 | 21 | |
| 23 | - public function get(Request $request, $productId) | |
| 22 | + /** | |
| 23 | + * get method will get product by id and return | |
| 24 | + * @param Product $product | |
| 25 | + * @param int $product_id | |
| 26 | + * @return array | |
| 27 | + */ | |
| 28 | + public function get( Product $product, $product_id ) | |
| 24 | 29 | { |
| 25 | - $product = Product::findOrFail($productId); | |
| 26 | - return [ | |
| 27 | - 'product' => $product | |
| 28 | - ]; | |
| 30 | + return $product->getProduct( $product_id ); | |
| 29 | 31 | } |
| 30 | 32 | |
| 31 | - public function create(Request $request) | |
| 33 | + /** | |
| 34 | + * creare method will create new product | |
| 35 | + * @param Request $request | |
| 36 | + * @param Product $product | |
| 37 | + * @return array | |
| 38 | + * @throws \FluentSupport\Framework\Validator\ValidationException | |
| 39 | + */ | |
| 40 | + public function create( ProductRequest $request, Product $product ) | |
| 32 | 41 | { |
| 33 | 42 | $data = $request->all(); |
| 34 | - $this->validate($data, [ | |
| 35 | - 'title' => 'required' | |
| 36 | - ]); | |
| 37 | 43 | |
| 38 | - $data = wp_unslash($data); | |
| 39 | - $product = Product::create($data); | |
| 44 | + return $product->createProduct( $data ); | |
| 45 | + } | |
| 40 | 46 | |
| 41 | - return [ | |
| 42 | - 'message' => __('Product has been successfully created', 'fluent-support'), | |
| 43 | - 'product' => $product | |
| 44 | - ]; | |
| 45 | - } | |
| 46 | 47 | |
| 47 | - public function update(Request $request, $productId) | |
| 48 | + /** | |
| 49 | + * update methd will update an exiting product by id | |
| 50 | + * @param Request $request | |
| 51 | + * @param Product $product | |
| 52 | + * @param int $product_id | |
| 53 | + * @return array | |
| 54 | + * @throws \FluentSupport\Framework\Validator\ValidationException | |
| 55 | + */ | |
| 56 | + public function update( ProductRequest $request, Product $product, $product_id ) | |
| 48 | 57 | { |
| 49 | 58 | $data = $request->all(); |
| 50 | - $this->validate($data, [ | |
| 51 | - 'title' => 'required' | |
| 52 | - ]); | |
| 53 | 59 | |
| 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 | - ]; | |
| 60 | + return $product->updateProduct( $product_id, $data ); | |
| 62 | 61 | } |
| 63 | 62 | |
| 64 | - public function delete(Request $request, $productId) | |
| 63 | + /** | |
| 64 | + * delete method will delete an existing product by id | |
| 65 | + * @param Product $product | |
| 66 | + * @param int $product_id | |
| 67 | + * @return array | |
| 68 | + */ | |
| 69 | + public function delete( Product $product, $product_id ) | |
| 65 | 70 | { |
| 66 | - Product::where('id', $productId) | |
| 67 | - ->delete(); | |
| 68 | - | |
| 69 | - return [ | |
| 70 | - 'message' => __('Product has been deleted', 'fluent-support') | |
| 71 | - ]; | |
| 71 | + return $product->deleteProduct( $product_id ); | |
| 72 | 72 | } |
| 73 | - | |
| 74 | 73 | } |