1212 * limitations under the License.
1313 **/
1414
15- use libs \ utils \ PaginationValidationRules ;
15+ use models \ oauth2 \ IResourceServerContext ;
1616use models \utils \IBaseRepository ;
17- use models \exceptions \EntityNotFoundException ;
18- use models \exceptions \ValidationException ;
19- use Illuminate \Support \Facades \Validator ;
20- use utils \Filter ;
21- use utils \FilterParser ;
22- use utils \OrderParser ;
23- use Illuminate \Support \Facades \Request ;
24- use Illuminate \Support \Facades \Log ;
25- use utils \PagingInfo ;
17+ use ModelSerializers \SerializerRegistry ;
18+
2619/**
2720 * Class AbstractCompanyServiceApiController
2821 * @package App\Http\Controllers
@@ -35,90 +28,63 @@ abstract class AbstractCompanyServiceApiController extends JsonController
3528 protected $ repository ;
3629
3730 /**
38- * AppliancesApiController constructor.
31+ * @var IResourceServerContext
32+ */
33+ protected $ resource_server_context ;
34+
35+ /**
3936 * @param IBaseRepository $repository
37+ * @param IResourceServerContext $resource_server_context
4038 */
41- public function __construct (IBaseRepository $ repository )
39+ public function __construct (IBaseRepository $ repository, IResourceServerContext $ resource_server_context )
4240 {
41+ parent ::__construct ();
4342 $ this ->repository = $ repository ;
43+ $ this ->resource_server_context = $ resource_server_context ;
4444 }
4545
46+
47+ protected function getResourceServerContext (): IResourceServerContext
48+ {
49+ return $ this ->resource_server_context ;
50+ }
51+
52+ protected function getRepository (): IBaseRepository
53+ {
54+ return $ this ->repository ;
55+ }
56+
57+ use ParametrizedGetAll;
4658 /**
4759 * @return mixed
4860 */
4961 public function getAll (){
50- $ values = Request::all ();
51-
52- $ rules = PaginationValidationRules::get ();
53-
54- try {
55-
56- $ validation = Validator::make ($ values , $ rules );
57-
58- if ($ validation ->fails ()) {
59- $ ex = new ValidationException ();
60- throw $ ex ->setMessages ($ validation ->messages ()->toArray ());
61- }
62-
63- // default values
64- $ page = 1 ;
65- $ per_page = PaginationValidationRules::PerPageMin;
66-
67- if (Request::has ('page ' )) {
68- $ page = intval (Request::input ('page ' ));
69- $ per_page = intval (Request::input ('per_page ' ));
70- }
71-
72- $ filter = null ;
73-
74- if (Request::has ('filter ' )) {
75- $ filter = FilterParser::parse (Request::input ('filter ' ), array
76- (
62+ return $ this ->_getAll (
63+ function () {
64+ return [
7765 'name ' => ['=@ ' , '== ' ],
7866 'company ' => ['=@ ' , '== ' ],
79- ));
80- }
81-
82- $ order = null ;
83-
84- if (Request::has ('order ' ))
85- {
86- $ order = OrderParser::parse (Request::input ('order ' ), array
87- (
67+ ];
68+ },
69+ function () {
70+ return [
71+ 'name ' => 'sometimes|string ' ,
72+ 'company ' => 'sometimes|string ' ,
73+ ];
74+ },
75+ function () {
76+ return [
8877 'name ' ,
8978 'company ' ,
9079 'id ' ,
91- ));
80+ ];
81+ },
82+ function ($ filter ) {
83+ return $ filter ;
84+ },
85+ function () {
86+ return SerializerRegistry::SerializerType_Public;
9287 }
93-
94- if (is_null ($ filter )) $ filter = new Filter ();
95-
96- $ data = $ this ->repository ->getAllByPage (new PagingInfo ($ page , $ per_page ), $ filter , $ order );
97- $ fields = Request::input ('fields ' , '' );
98- $ fields = !empty ($ fields ) ? explode (', ' , $ fields ) : [];
99- $ relations = Request::input ('relations ' , '' );
100- $ relations = !empty ($ relations ) ? explode (', ' , $ relations ) : [];
101- return $ this ->ok
102- (
103- $ data ->toArray
104- (
105- Request::input ('expand ' , '' ),
106- $ fields ,
107- $ relations
108- )
109- );
110- }
111- catch (EntityNotFoundException $ ex1 ) {
112- Log::warning ($ ex1 );
113- return $ this ->error404 ();
114- }
115- catch (ValidationException $ ex2 ) {
116- Log::warning ($ ex2 );
117- return $ this ->error412 ($ ex2 ->getMessages ());
118- }
119- catch (\Exception $ ex ) {
120- Log::error ($ ex );
121- return $ this ->error500 ($ ex );
122- }
88+ );
12389 }
12490}
0 commit comments