From 003c671156fb14cdd327ac6354efaecea34c194f Mon Sep 17 00:00:00 2001 From: Kavalar Date: Wed, 11 Oct 2023 23:19:30 +0300 Subject: [PATCH] registration and get user info --- common/services/ProfileService.php | 8 +++ .../api/controllers/RegisterController.php | 2 +- .../api/controllers/UserController.php | 67 ++++++++++++------- frontend/modules/api/models/User.php | 26 +++++++ 4 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 frontend/modules/api/models/User.php diff --git a/common/services/ProfileService.php b/common/services/ProfileService.php index 95a7b51..b75a868 100644 --- a/common/services/ProfileService.php +++ b/common/services/ProfileService.php @@ -68,6 +68,14 @@ class ProfileService return $searchModel->byParams(); } + public static function getProfileById($id): ?array + { + $searchModel = new ProfileSearchForm(); + $searchModel->id = $id; + return $searchModel->byId(); + + } + /** * @throws ServerErrorHttpException */ diff --git a/frontend/modules/api/controllers/RegisterController.php b/frontend/modules/api/controllers/RegisterController.php index fa5a403..4c759dd 100644 --- a/frontend/modules/api/controllers/RegisterController.php +++ b/frontend/modules/api/controllers/RegisterController.php @@ -21,7 +21,7 @@ class RegisterController extends ApiController * @OA\Post(path="/register/sign-up", * summary="Регистрация", * description="Метод для регистрации", - * + * tags={"Registration"}, * @OA\RequestBody( * @OA\MediaType( * mediaType="multipart/form-data", diff --git a/frontend/modules/api/controllers/UserController.php b/frontend/modules/api/controllers/UserController.php index 36512af..a8c66db 100755 --- a/frontend/modules/api/controllers/UserController.php +++ b/frontend/modules/api/controllers/UserController.php @@ -4,6 +4,7 @@ namespace frontend\modules\api\controllers; use common\behaviors\GsCors; +use common\classes\Debug; use common\models\User; use frontend\modules\api\models\LoginForm; use Yii; @@ -13,35 +14,35 @@ use yii\rest\ActiveController; use yii\web\BadRequestHttpException; use yii\web\Response; -class UserController extends ActiveController +class UserController extends ApiController { public $modelClass = User::class; - public function behaviors() - { - return ArrayHelper::merge(parent::behaviors(), [ - [ - 'class' => ContentNegotiator::class, - 'formats' => [ - 'application/json' => Response::FORMAT_JSON, - ], - ], - 'corsFilter' => [ - 'class' => GsCors::class, - 'cors' => [ - 'Origin' => ['*'], - //'Access-Control-Allow-Credentials' => true, - 'Access-Control-Allow-Headers' => [ - 'Access-Control-Allow-Origin', - 'Content-Type', - 'Access-Control-Allow-Headers', - 'Authorization', - 'X-Requested-With' - ], - ] - ], - ]); - } +// public function behaviors() +// { +// return ArrayHelper::merge(parent::behaviors(), [ +// [ +// 'class' => ContentNegotiator::class, +// 'formats' => [ +// 'application/json' => Response::FORMAT_JSON, +// ], +// ], +// 'corsFilter' => [ +// 'class' => GsCors::class, +// 'cors' => [ +// 'Origin' => ['*'], +// //'Access-Control-Allow-Credentials' => true, +// 'Access-Control-Allow-Headers' => [ +// 'Access-Control-Allow-Origin', +// 'Content-Type', +// 'Access-Control-Allow-Headers', +// 'Authorization', +// 'X-Requested-With' +// ], +// ] +// ], +// ]); +// } public function actions() { @@ -75,4 +76,18 @@ class UserController extends ActiveController throw new BadRequestHttpException(json_encode($model->errors)); } } + + /** + * @return \frontend\modules\api\models\User + * @throws BadRequestHttpException + */ + public function actionMe(): \frontend\modules\api\models\User + { + $user = \frontend\modules\api\models\User::findOne(Yii::$app->user->id); + if (!$user){ + throw new BadRequestHttpException("User not found"); + } + + return $user; + } } diff --git a/frontend/modules/api/models/User.php b/frontend/modules/api/models/User.php new file mode 100644 index 0000000..d9ca54b --- /dev/null +++ b/frontend/modules/api/models/User.php @@ -0,0 +1,26 @@ + function () { + $userCard = new UserCardSearch(); + return ProfileService::getProfileById($this->userCard->id); + } + ]; + } + +} \ No newline at end of file