44
55use App \Entities \User ;
66use League \Fractal \TransformerAbstract ;
7+ use Dingo \Api \Exception \ResourceException ;
78use App \Transformers \Users \UserTransformer ;
89use App \Contracts \Users \UsersServiceContract ;
910use Joselfonseca \LaravelApiTools \Contracts \FractalAble ;
1011use Joselfonseca \LaravelApiTools \Contracts \ValidateAble ;
12+ use Joselfonseca \LaravelApiTools \Traits \FilterableTrait ;
1113use Joselfonseca \LaravelApiTools \Traits \FractalAbleTrait ;
1214use Joselfonseca \LaravelApiTools \Traits \ValidateAbleTrait ;
13- use Joselfonseca \LaravelApiTools \Exceptions \ValidationException ;
15+ use Joselfonseca \LaravelApiTools \Traits \OrderQueryResultHelper ;
16+ use Joselfonseca \LaravelApiTools \Traits \ProcessMultipleParameterHelper ;
1417
1518/**
1619 * Class UsersService
1922class UsersService implements FractalAble, ValidateAble, UsersServiceContract
2023{
2124
22- use FractalAbleTrait, ValidateAbleTrait;
25+ use FractalAbleTrait, ValidateAbleTrait, FilterableTrait, OrderQueryResultHelper, ProcessMultipleParameterHelper ;
2326
2427 /**
2528 * @var array
@@ -35,7 +38,7 @@ class UsersService implements FractalAble, ValidateAble, UsersServiceContract
3538 */
3639 protected $ validationUpdateRules = [
3740 'name ' => 'sometimes|required ' ,
38- 'email ' => 'sometimes|required|unique:users,email '
41+ 'password ' => 'sometimes|required|min:8|confirmed '
3942 ];
4043
4144 /**
@@ -78,13 +81,21 @@ public function setTransformer() : TransformerAbstract
7881 return app (UserTransformer::class);
7982 }
8083
84+
8185 /**
86+ * @param array $attributes
8287 * @param int $limit
83- * @return mixed
88+ * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection|static[]
8489 */
85- public function get ($ limit = 20 )
90+ public function get (Array $ attributes = [], $ limit = 20 )
8691 {
8792 $ model = $ this ->model ->with ($ this ->includes );
93+ $ this ->addFilter ($ attributes );
94+ $ this ->applyFilters ($ model , $ attributes );
95+
96+ $ this ->processOrderingRules ($ attributes );
97+ $ this ->applyOrderingRules ($ model );
98+
8899 if (!empty ($ limit )) {
89100 return $ model ->paginate ($ limit );
90101 }
@@ -103,7 +114,7 @@ public function find($id)
103114 /**
104115 * @param array $attributes
105116 * @return User
106- * @throws ValidationException
117+ * @throws ResourceException
107118 */
108119 public function create (array $ attributes = [])
109120 {
@@ -117,31 +128,48 @@ public function create(array $attributes = [])
117128 * @param int|string $id
118129 * @param array $attributes
119130 * @return User
120- * @throws ValidationException
131+ * @throws ResourceException
121132 */
122133 public function update ($ id , array $ attributes = [])
123134 {
124135 $ model = $ this ->find ($ id );
125136 if (isset ($ attributes ['email ' ]) && $ attributes ['email ' ] != $ model ->email ) {
126137 $ this ->validationUpdateRules ['email ' ] = 'sometimes|required|unique:users,email, ' .$ model ->id ;
127138 }
139+ $ this ->runValidator ($ attributes , $ this ->validationUpdateRules , $ this ->validationMessages );
128140 if (isset ($ attributes ['password ' ])) {
129141 $ attributes ['password ' ] = bcrypt ($ attributes ['password ' ]);
130142 }
131- $ this ->runValidator ($ attributes , $ this ->validationUpdateRules , $ this ->validationMessages );
132143 $ model ->fill ($ attributes );
133144 $ model ->save ();
134145 return $ model ->fresh ();
135146 }
136147
137148 /**
138- * @param int|string $id
149+ * @param int|string $ids
139150 * @return bool
140151 */
141- public function delete ($ id )
152+ public function delete ($ ids )
142153 {
143- $ model = $ this ->find ($ id );
144- $ model ->delete ();
154+ $ parameters = $ this ->processParameter ($ ids );
155+
156+ foreach ($ parameters as $ id ) {
157+ $ model = $ this ->find ($ id );
158+ $ model ->delete ();
159+ }
160+
145161 return true ;
146162 }
163+
164+ /**
165+ * Filterable fields
166+ * @return array
167+ */
168+ public function getFilterableFields ()
169+ {
170+ return [
171+ 'name ' => 'partial ' ,
172+ 'email ' => 'partial ' ,
173+ ];
174+ }
147175}
0 commit comments