api search profile

This commit is contained in:
andrey 2021-06-25 18:11:30 +03:00
parent 1a4f0b6d5e
commit 96a8a530ed
3 changed files with 117 additions and 0 deletions

View File

@ -60,6 +60,7 @@ return [
'showScriptName' => false, 'showScriptName' => false,
'rules' => [ 'rules' => [
'site/index' => 'card/user-card/index', 'site/index' => 'card/user-card/index',
'api/profile/<id:\d+>' => 'api/profile/index',
'' => 'card/user-card/index', '' => 'card/user-card/index',
['class' => 'yii\rest\UrlRule', 'controller' => 'skills'], ['class' => 'yii\rest\UrlRule', 'controller' => 'skills'],
], ],

View File

@ -0,0 +1,49 @@
<?php
namespace frontend\modules\api\controllers;
use common\behaviors\GsCors;
use common\classes\Debug;
use frontend\modules\api\models\ProfileSearchForm;
class ProfileController extends \yii\rest\Controller
{
public function behaviors()
{
return [
[
'class' => \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();
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace frontend\modules\api\models;
use backend\modules\card\models\UserCard;
use yii\base\Model;
/**
* Class ProfileSearchForm
* @property integer $limit
* @property integer $offset
* @property integer $id
* @package frontend\modules\api\models
*/
class ProfileSearchForm extends Model
{
public $limit = 10;
public $offset = 0;
public $skills;
public $id;
public function rules()
{
return [
[['id', 'limit', 'offset'], 'safe'],
[['skills'], 'checkIsArray'],
];
}
public function checkIsArray()
{
if (!is_array($this->_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();
}
}