33namespace App \Services \Users ;
44
55use App \Entities \User ;
6+ use League \Fractal \Serializer \JsonApiSerializer ;
7+ use League \Fractal \Serializer \SerializerAbstract ;
68use League \Fractal \TransformerAbstract ;
79use Dingo \Api \Exception \ResourceException ;
810use App \Transformers \Users \UserTransformer ;
@@ -37,8 +39,8 @@ class UsersService implements FractalAble, ValidateAble, UsersServiceContract
3739 * @var array
3840 */
3941 protected $ validationUpdateRules = [
40- 'name ' => 'sometimes| required ' ,
41- 'password ' => 'sometimes| required|min:8|confirmed '
42+ 'name ' => 'required ' ,
43+ 'email ' => 'required|unique:users,email '
4244 ];
4345
4446 /**
@@ -62,7 +64,7 @@ class UsersService implements FractalAble, ValidateAble, UsersServiceContract
6264 * Declare the includes to use in the with query
6365 * @var array
6466 */
65- protected $ includes = [];
67+ protected $ includes = [' roles.permissions ' ];
6668
6769 /**
6870 * UsersService constructor.
@@ -76,18 +78,26 @@ public function __construct(User $model)
7678 /**
7779 * @return mixed
7880 */
79- public function setTransformer () : TransformerAbstract
81+ public function setTransformer (): TransformerAbstract
8082 {
8183 return app (UserTransformer::class);
8284 }
8385
86+ /**
87+ * Sets the serializer to be used in the transformation
88+ * @return SerializerAbstract
89+ */
90+ public function setSerializer ()
91+ {
92+ return new JsonApiSerializer (url ('api ' ));
93+ }
8494
8595 /**
8696 * @param array $attributes
8797 * @param int $limit
8898 * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection|static[]
8999 */
90- public function get (Array $ attributes = [], $ limit = 20 )
100+ public function get (array $ attributes = [], $ limit = 20 )
91101 {
92102 $ model = $ this ->model ->with ($ this ->includes );
93103 $ this ->addFilter ($ attributes );
@@ -127,17 +137,26 @@ public function create(array $attributes = [])
127137 /**
128138 * @param int|string $id
129139 * @param array $attributes
140+ * @param bool $partial
130141 * @return User
131142 * @throws ResourceException
132143 */
133- public function update ($ id , array $ attributes = [])
144+ public function update ($ id , array $ attributes = [], $ partial = false )
134145 {
135146 $ model = $ this ->find ($ id );
136- if (isset ($ attributes ['email ' ]) && $ attributes ['email ' ] != $ model ->email ) {
137- $ this ->validationUpdateRules ['email ' ] = 'sometimes|required|unique:users,email, ' .$ model ->id ;
147+ if (array_key_exists ('email ' , $ attributes )) {
148+ $ this ->validationUpdateRules ['email ' ] = 'required|unique:users,email, ' . $ model ->id ;
149+ }
150+ if (array_key_exists ('password ' , $ attributes )) {
151+ $ this ->validationUpdateRules ['password ' ] = 'required|min:8|confirmed ' ;
152+ }
153+ if ($ partial ) {
154+ $ this ->validationUpdateRules = array_map (function ($ value ) {
155+ return "sometimes| " . $ value ;
156+ }, $ this ->validationUpdateRules );
138157 }
139158 $ this ->runValidator ($ attributes , $ this ->validationUpdateRules , $ this ->validationMessages );
140- if (isset ($ attributes ['password ' ])) {
159+ if (isset ($ attributes ['password ' ])) {
141160 $ attributes ['password ' ] = bcrypt ($ attributes ['password ' ]);
142161 }
143162 $ model ->fill ($ attributes );
0 commit comments