la_bot_back/kernel/app_modules/tgbot/controllers/TgBotRestController.php

117 lines
3.5 KiB
PHP
Raw Normal View History

2025-01-03 02:11:09 +03:00
<?php
namespace kernel\app_modules\tgbot\controllers;
use app\modules\tgbot\models\Tgbot;
use JetBrains\PhpStorm\NoReturn;
2025-01-05 17:23:49 +03:00
use kernel\app_modules\card\models\Card;
2025-01-03 02:11:09 +03:00
use kernel\app_modules\card\models\forms\CreateCardForm;
use kernel\app_modules\card\services\CardService;
use kernel\helpers\Debug;
use kernel\modules\user\models\forms\CreateUserForm;
use kernel\modules\user\service\UserService;
use kernel\Request;
use kernel\RestController;
use kernel\services\TokenService;
class TgBotRestController extends RestController
{
public function __construct()
{
$this->model = new Tgbot();
}
#[NoReturn] public function actionStore(): void
{
$request = new Request();
$data = $request->post();
$tgBot = $this->model->where("bot_id", $data['bot_id'])->where("dialog_id", $data['dialog_id'])->first();
2025-01-05 17:23:49 +03:00
if (!$tgBot) {
foreach ($this->model->getFillable() as $item) {
2025-01-03 02:11:09 +03:00
$this->model->{$item} = $data[$item] ?? null;
}
$userService = new UserService();
$userForm = new CreateUserForm();
$username = $data['username'];
$userForm->load([
'username' => $username,
'password' => TokenService::random_bytes(20),
'email' => $username . "@hookahdnr.ru"
]);
$user = $userService->create($userForm);
if ($user) {
$this->model->user_id = $user->id;
}
$this->model->save();
2025-01-05 17:23:49 +03:00
$resArr = $this->model->toArray();
$cardService = new CardService();
$cardForm = new CreateCardForm();
$cardForm->load([
'user_id' => $user->id,
'username' => $username,
'card_template_id' => 105545,
'status' => 1,
]);
$cardService->create($cardForm);
2025-01-03 15:14:03 +03:00
$this->renderApi($resArr);
2025-01-03 02:11:09 +03:00
}
2025-01-03 15:14:03 +03:00
$resArr = $tgBot->toArray();
2025-01-03 02:11:09 +03:00
2025-01-03 15:14:03 +03:00
$this->renderApi($resArr);
2025-01-03 02:11:09 +03:00
}
#[NoReturn] public function actionCreateCard(): void
{
$cardService = new CardService();
$request = new Request();
$data = $request->post();
$form = new CreateCardForm();
$form->load($data);
$form->setItem('payment_type', 2);
$form->setItem('bank_id', 323);
$form->setItem('info', 42);
$form->setItem('program', 74);
$form->setItem('cvc', 101);
$form->setItem('pin', 1111);
$form->setItem('status', 1);
2025-01-05 17:23:49 +03:00
if ($form->validate()) {
2025-01-03 02:11:09 +03:00
$model = $cardService->create($form);
$this->renderApi($model->load(['cardFile'])->toArray());
}
$this->renderApi([]);
}
#[NoReturn] public function actionGetByDialog(int $dialog_id, int $bot_id): void
{
$model = \kernel\app_modules\tgbot\models\Tgbot::where(['dialog_id' => $dialog_id, 'bot_id' => $bot_id])->first();
if ($model) {
$this->renderApi($model->toArray());
}
$this->renderApi([]);
}
2025-01-05 17:23:49 +03:00
#[NoReturn] public function actionGetCardByDialog(int $dialog_id, int $bot_id): void
{
$model = \kernel\app_modules\tgbot\models\Tgbot::where(['dialog_id' => $dialog_id, 'bot_id' => $bot_id])->first();
if ($model) {
$card = Card::where("user_id", $model->user_id)->first();
if ($card) {
$this->renderApi($card->load('cardFile')->toArray());
}
}
$this->renderApi([]);
}
2025-01-03 02:11:09 +03:00
}