From 96a8a530ed89d8e5a8747e16589f1345fd17d45d Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 25 Jun 2021 18:11:30 +0300 Subject: [PATCH] api search profile --- frontend/config/main.php | 1 + .../api/controllers/ProfileController.php | 49 ++++++++++++++ .../modules/api/models/ProfileSearchForm.php | 67 +++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 frontend/modules/api/controllers/ProfileController.php create mode 100644 frontend/modules/api/models/ProfileSearchForm.php diff --git a/frontend/config/main.php b/frontend/config/main.php index da4c0ca..7b2da72 100755 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -60,6 +60,7 @@ return [ 'showScriptName' => false, 'rules' => [ 'site/index' => 'card/user-card/index', + 'api/profile/' => 'api/profile/index', '' => 'card/user-card/index', ['class' => 'yii\rest\UrlRule', 'controller' => 'skills'], ], diff --git a/frontend/modules/api/controllers/ProfileController.php b/frontend/modules/api/controllers/ProfileController.php new file mode 100644 index 0000000..b015bd3 --- /dev/null +++ b/frontend/modules/api/controllers/ProfileController.php @@ -0,0 +1,49 @@ + \yii\filters\ContentNegotiator::className(), + 'formats' => [ + 'application/json' => \yii\web\Response::FORMAT_JSON, + ], + ], + 'corsFilter' => [ + 'class' => GsCors::class, + 'cors' => [ + 'Origin' => ['*'], + //'Access-Control-Allow-Credentials' => true, + 'Access-Control-Allow-Headers' => [ + 'Content-Type', + 'Access-Control-Allow-Headers', + 'Authorization', + 'X-Requested-With' + ], + ] + ] + ]; + } + + public function actionIndex($id = null) + { + $searchModel = new ProfileSearchForm(); + $searchModel->attributes = \Yii::$app->request->get(); + + if ($id){ + return $searchModel->byId(); + } + + return $searchModel->byParams(); + } + +} diff --git a/frontend/modules/api/models/ProfileSearchForm.php b/frontend/modules/api/models/ProfileSearchForm.php new file mode 100644 index 0000000..b63c4a4 --- /dev/null +++ b/frontend/modules/api/models/ProfileSearchForm.php @@ -0,0 +1,67 @@ +_task)) { + $this->addError('_task', 'X is not array!'); + } + } + + public function byId() + { + if ($this->id) { + return UserCard::find() + ->where(['id' => $this->id]) + ->with(['skillValues']) + ->asArray() + ->one(); + } + + return null; + } + + public function byParams() + { + $model = UserCard::find() + ->joinWith(['skillValues']); + + if($this->skills){ + $this->skills = explode(',', $this->skills); + $model->where(['card_skill.skill_id' => $this->skills]); + } + + return $model->limit($this->limit) + ->offset($this->offset)->asArray()->all(); + } + +} \ No newline at end of file