| @@ -45,6 +45,88 @@ | ||
| 45 | 45 | |
| 46 | 46 | return $query; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | + /** | |
| 50 | + * Local scope to order products alphabetically by title | |
| 51 | + * @param $query | |
| 52 | + * @return mixed | |
| 53 | + */ | |
| 54 | + public function scopeOrderedByTitle($query) | |
| 55 | + { | |
| 56 | + return $query->orderBy('title', 'ASC'); | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * This `getProducts` method will return the list of products | |
| 61 | + * @param string $search | |
| 62 | + * @return array | |
| 63 | + */ | |
| 64 | + public function getProducts ( $search ) | |
| 65 | + { | |
| 66 | + return [ | |
| 67 | + 'products' => $this->orderedByTitle()->searchBy($search)->paginate() | |
| 68 | + ]; | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * This `getProduct` method will get product by id and return | |
| 73 | + * @param int $productId | |
| 74 | + * @return array | |
| 75 | + */ | |
| 76 | + public function getProduct ( $productId ) | |
| 77 | + { | |
| 78 | + return [ | |
| 79 | + 'product' => $this->findOrFail($productId) | |
| 80 | + ]; | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * This `createProduct` method will create a new product | |
| 85 | + * @param array $data | |
| 86 | + * @return array | |
| 87 | + */ | |
| 88 | + public function createProduct ( $data ) | |
| 89 | + { | |
| 90 | + $data = wp_unslash($data); | |
| 91 | + $product = $this->create($data); | |
| 92 | + | |
| 93 | + return [ | |
| 94 | + 'message' => __('Product has been successfully created', 'fluent-support'), | |
| 95 | + 'product' => $product | |
| 96 | + ]; | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * This `updateProduct` method will update an exiting product by id | |
| 101 | + * @param int $productId | |
| 102 | + * @param array $data | |
| 103 | + * @return array | |
| 104 | + */ | |
| 105 | + public function updateProduct ( $productId, $data ) | |
| 106 | + { | |
| 107 | + $data = wp_unslash($data); | |
| 108 | + $product = $this->findOrFail($productId); | |
| 109 | + $product->fill($data); | |
| 110 | + $product->save(); | |
| 111 | + | |
| 112 | + return [ | |
| 113 | + 'message' => __('Product has been updated', 'fluent-support'), | |
| 114 | + 'product' => $product | |
| 115 | + ]; | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * This `deleteProduct` method will delete an exiting product by id | |
| 120 | + * @param int $productId | |
| 121 | + * @return array | |
| 122 | + */ | |
| 123 | + public function deleteProduct ( $productId ) | |
| 124 | + { | |
| 125 | + $this->where('id', $productId)->delete(); | |
| 126 | + | |
| 127 | + return [ | |
| 128 | + 'message' => __('Product has been deleted', 'fluent-support') | |
| 129 | + ]; | |
| 130 | + } | |
| 49 | 131 | |
| 50 | 132 | } |