diff --git a/common/models/UserTgBotDialog.php b/common/models/UserTgBotDialog.php new file mode 100644 index 0000000..0c32212 --- /dev/null +++ b/common/models/UserTgBotDialog.php @@ -0,0 +1,78 @@ + TimestampBehavior::class, + 'createdAtAttribute' => 'created_at', + 'updatedAtAttribute' => 'updated_at', + 'value' => new Expression('NOW()'), + ], + ]; + } + + + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [['user_id', 'dialog_id'], 'integer'], + [['dialog_id'], 'required'], + [['created_at', 'updated_at'], 'safe'], + [['dialog_id'], 'unique'], + [['user_id'], 'unique'], + [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'user_id' => 'User ID', + 'dialog_id' => 'Dialog ID', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUser() + { + return $this->hasOne(User::className(), ['id' => 'user_id']); + } +} diff --git a/common/models/UserTgBotToken.php b/common/models/UserTgBotToken.php index 0c3f905..7dbc899 100644 --- a/common/models/UserTgBotToken.php +++ b/common/models/UserTgBotToken.php @@ -2,7 +2,9 @@ namespace common\models; +use yii\behaviors\TimestampBehavior; use yii\db\ActiveQuery; +use yii\db\Expression; /** * This is the model class for table "user_tg_bot_token". @@ -26,6 +28,18 @@ class UserTgBotToken extends \yii\db\ActiveRecord return 'user_tg_bot_token'; } + public function behaviors() + { + return [ + [ + 'class' => TimestampBehavior::class, + 'createdAtAttribute' => 'created_at', + 'updatedAtAttribute' => 'updated_at', + 'value' => new Expression('NOW()'), + ], + ]; + } + /** * {@inheritdoc} */ diff --git a/console/migrations/m231024_132757_create_user_tg_bot_dialog_table.php b/console/migrations/m231024_132757_create_user_tg_bot_dialog_table.php new file mode 100644 index 0000000..b99ebc5 --- /dev/null +++ b/console/migrations/m231024_132757_create_user_tg_bot_dialog_table.php @@ -0,0 +1,32 @@ +createTable('{{%user_tg_bot_dialog}}', [ + 'id' => $this->primaryKey(), + 'user_id' => $this->integer(), + 'dialog_id' => $this->integer()->notNull()->unique(), + 'created_at' => $this->dateTime(), + 'updated_at' => $this->dateTime(), + ]); + $this->addForeignKey('user_user_tg_bot_dialog', 'user_tg_bot_dialog', 'user_id', 'user', 'id'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable('{{%user_tg_bot_dialog}}'); + } +} diff --git a/frontend/config/main.php b/frontend/config/main.php index aebc7f2..4285610 100755 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -86,6 +86,16 @@ return [ 'api/profile/' => 'api/profile/index', 'api/reports/' => 'api/reports/view', '' => 'card/user-card/index', + + + 'GET api/tg-bot/token' => 'api/user-tg-bot/get-token', + 'GET api/tg-bot/user' => 'api/user-tg-bot/get-user', + + 'POST api/tg-bot/dialog/create' => 'api/user-tg-bot/set-dialog', + 'GET api/tg-bot/dialog/user/id' => 'api/user-tg-bot/get-user-id-by-dialog-id', + 'GET api/tg-bot/dialog/dialog/id' => 'api/user-tg-bot/get-dialog-id-by-user-id', + + ['class' => 'yii\rest\UrlRule', 'controller' => 'skills'], ], ], diff --git a/frontend/modules/api/controllers/UserController.php b/frontend/modules/api/controllers/UserController.php index 60f27af..bf07ba8 100755 --- a/frontend/modules/api/controllers/UserController.php +++ b/frontend/modules/api/controllers/UserController.php @@ -4,8 +4,8 @@ namespace frontend\modules\api\controllers; use common\models\User; -use frontend\modules\api\models\profile\ProfileChangeEmailForm; -use frontend\modules\api\models\profile\ProfileChangePersonalDataForm; +use frontend\modules\api\models\profile\forms\ProfileChangeEmailForm; +use frontend\modules\api\models\profile\forms\ProfileChangePersonalDataForm; use frontend\modules\api\services\UserService; use Yii; use yii\base\InvalidConfigException; diff --git a/frontend/modules/api/controllers/UserTgBotController.php b/frontend/modules/api/controllers/UserTgBotController.php index 4a9b4ed..f17d19b 100644 --- a/frontend/modules/api/controllers/UserTgBotController.php +++ b/frontend/modules/api/controllers/UserTgBotController.php @@ -5,27 +5,13 @@ namespace frontend\modules\api\controllers; use Exception; use frontend\modules\api\models\profile\User; -use frontend\modules\api\models\UserTgBotToken; +use frontend\modules\api\models\tg_bot\forms\TgBotDialogForm; +use frontend\modules\api\models\tg_bot\UserTgBotToken; use frontend\modules\api\services\UserTgBotTokenService; use Yii; -use yii\helpers\ArrayHelper; class UserTgBotController extends ApiController { - public function behaviors(): array - { - return ArrayHelper::merge(parent::behaviors(), [ - - 'verbs' => [ - 'class' => \yii\filters\VerbFilter::class, - 'actions' => [ - 'get-token' => ['get'], - 'get-user-by-token' => ['get'], - ], - ] - ]); - } - /** * @var UserTgBotTokenService */ @@ -43,13 +29,13 @@ class UserTgBotController extends ApiController } /** - * @OA\Get(path="/user-tg-bot/get-token", + * @OA\Get(path="/tg-bot/token", * summary="Токен ТГ бота", * description="Метод для возвращает токен для ТГ бота", * security={ * {"bearerAuth": {}} * }, - * tags={"UserTgBotToken"}, + * tags={"TgBot"}, * @OA\Response( * response=200, * description="Возвращает объект токен ТГ бота", @@ -63,20 +49,20 @@ class UserTgBotController extends ApiController * @return UserTgBotToken * @throws Exception */ - public function actionGetToken() + public function actionGetToken(): UserTgBotToken { return $this->userTgBotTokenService->getToken(Yii::$app->user->id); } /** * - * @OA\Get(path="/user-tg-bot/get-user", + * @OA\Get(path="/tg-bot/user", * summary="Получить данные пользователя", * description="Метод для получения данныех пользователя по токену ТГ бота", * security={ * {"bearerAuth": {}} * }, - * tags={"UserTgBotToken"}, + * tags={"TgBot"}, * @OA\Parameter( * name="token", * in="query", @@ -104,4 +90,124 @@ class UserTgBotController extends ApiController { return $this->userTgBotTokenService->getUserByToken($token); } + + /** + * + * @OA\Post(path="/tg-bot/dialog/create", + * summary="Сохранить новый id диалога", + * description="Метод создает новую запись с id пользователя и id диалога", + * security={ + * {"bearerAuth": {}} + * }, + * tags={"TgBot"}, + * @OA\Parameter( + * name="userId", + * in="query", + * example="1", + * required=true, + * description="id пользователя", + * @OA\Schema( + * type="integer", + * ) + * ), + * @OA\Parameter( + * name="dialogId", + * in="query", + * example="2355", + * required=true, + * description="id диалога", + * @OA\Schema( + * type="integer", + * ) + * ), + * @OA\Response( + * response=200, + * description="Возвращает сообщение об успехе", + * @OA\MediaType( + * mediaType="application/json", + * ), + * ), + * ) + * + * @return TgBotDialogForm|string[] + * @throws Exception + */ + public function actionSetDialog() + { + return $this->userTgBotTokenService->createDialog(Yii::$app->request->post()); + } + + /** + * + * @OA\Get(path="/tg-bot/dialog/dialog/id", + * summary="Получить id диалога по id пользователя", + * description="Метод для получения id пользователя по id пользователя", + * security={ + * {"bearerAuth": {}} + * }, + * tags={"TgBot"}, + * @OA\Parameter( + * name="userId", + * in="query", + * example="1", + * required=true, + * description="id пользователя", + * @OA\Schema( + * type="integer", + * ) + * ), + * @OA\Response( + * response=200, + * description="Возвращает dialog_id", + * @OA\MediaType( + * mediaType="application/json", + * ), + * ), + * ) + * + * @param string $userId + * @return array + * @throws Exception + */ + public function actionGetDialogIdByUserId(string $userId): array + { + return $this->userTgBotTokenService->getDialogIdByUserId($userId); + } + + /** + * + * @OA\Get(path="/tg-bot/dialog/user/id", + * summary="Получить id пользователя по id диалога", + * description="Метод для получения id пользователя по id диалога", + * security={ + * {"bearerAuth": {}} + * }, + * tags={"TgBot"}, + * @OA\Parameter( + * name="dialogId", + * in="query", + * example="225", + * required=true, + * description="id диалога", + * @OA\Schema( + * type="integer", + * ) + * ), + * @OA\Response( + * response=200, + * description="Возвращает user_id", + * @OA\MediaType( + * mediaType="application/json", + * ), + * ), + * ) + * + * @param string $dialogId + * @return array + * @throws Exception + */ + public function actionGetUserIdByDialogId(string $dialogId): array + { + return $this->userTgBotTokenService->getUserIdByDialogId($dialogId); + } } diff --git a/frontend/modules/api/models/profile/ProfileChangeEmailForm.php b/frontend/modules/api/models/profile/forms/ProfileChangeEmailForm.php similarity index 84% rename from frontend/modules/api/models/profile/ProfileChangeEmailForm.php rename to frontend/modules/api/models/profile/forms/ProfileChangeEmailForm.php index aabf8c1..6be7a66 100644 --- a/frontend/modules/api/models/profile/ProfileChangeEmailForm.php +++ b/frontend/modules/api/models/profile/forms/ProfileChangeEmailForm.php @@ -1,11 +1,11 @@ hasOne(User::class, ['id' => 'user_id']); + } +} \ No newline at end of file diff --git a/frontend/modules/api/models/UserTgBotToken.php b/frontend/modules/api/models/tg_bot/UserTgBotToken.php similarity index 96% rename from frontend/modules/api/models/UserTgBotToken.php rename to frontend/modules/api/models/tg_bot/UserTgBotToken.php index 66d7082..14c0fed 100644 --- a/frontend/modules/api/models/UserTgBotToken.php +++ b/frontend/modules/api/models/tg_bot/UserTgBotToken.php @@ -1,6 +1,6 @@ load($params); + + if (!$form->validate()){ + return $form; + } + + $dialog = new UserTgBotDialog(); + $dialog->user_id = $form->userId; + $dialog->dialog_id = $form->dialogId; + + if (!$dialog->save()) { + throw new Exception('User dont save'); + } + + return ['status' => 'success']; + } + + /** + * @param string $userId + * @return array + * @throws Exception + */ + public function getDialogIdByUserId(string $userId) + { + $model = UserTgBotDialog::findOne(['user_id' => $userId]); + + if (!$model) { + throw new \Exception('dialog_id не найден!'); + } + + return ['dialog_id' => $model->dialog_id]; + } + + /** + * @param string $dialogId + * @return array + * @throws Exception + */ + public function getUserIdByDialogId(string $dialogId) + { + $model = UserTgBotDialog::findOne(['dialog_id' => $dialogId]); + + if (!$model) { + throw new \Exception('user_id не найден!'); + } + + return ['user_id' => $model->user_id]; + } } \ No newline at end of file