server test version
This commit is contained in:
91
kernel/app_modules/tgbot/controllers/TgBotRestController.php
Normal file
91
kernel/app_modules/tgbot/controllers/TgBotRestController.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tgbot\controllers;
|
||||
|
||||
use app\modules\tgbot\models\Tgbot;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
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();
|
||||
|
||||
if (!$tgBot){
|
||||
foreach ($this->model->getFillable() as $item){
|
||||
$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();
|
||||
|
||||
$this->renderApi($this->model->toArray());
|
||||
}
|
||||
|
||||
$this->renderApi($tgBot->toArray());
|
||||
}
|
||||
|
||||
#[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);
|
||||
|
||||
if ($form->validate()){
|
||||
$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([]);
|
||||
}
|
||||
|
||||
}
|
111
kernel/app_modules/tgbot/controllers/TgbotController.php
Normal file
111
kernel/app_modules/tgbot/controllers/TgbotController.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tgbot\controllers;
|
||||
|
||||
use app\modules\tgbot\models\forms\CreateTgBotForm;
|
||||
use app\modules\tgbot\models\Tgbot;
|
||||
use app\modules\tgbot\services\TgBotService;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\user\models\forms\CreateUserForm;
|
||||
use kernel\modules\user\service\UserService;
|
||||
use kernel\services\TokenService;
|
||||
|
||||
class TgbotController extends AdminController
|
||||
{
|
||||
protected TgBotService $botService;
|
||||
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tgbot/views/tgbot/";
|
||||
$this->botService = new TgBotService();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$tgForm = new CreateTgBotForm();
|
||||
$tgForm->load($_REQUEST);
|
||||
if ($tgForm->validate()) {
|
||||
$tg = $this->botService->create($tgForm);
|
||||
if ($tg) {
|
||||
$this->redirect("/admin/tg-bot/view/" . $tg->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/tg-bot/create");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionUpdate($id): void
|
||||
{
|
||||
$model = Tgbot::find($id);
|
||||
if (!$model) {
|
||||
throw new Exception(message: "The dialog not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEdit($id): void
|
||||
{
|
||||
$tg = Tgbot::find($id);
|
||||
if (!$tg) {
|
||||
throw new Exception(message: "The tag not found");
|
||||
}
|
||||
$tgForm = new CreateTgBotForm();
|
||||
$tgService = new TgBotService();
|
||||
$tgForm->load($_REQUEST);
|
||||
if ($tgForm->validate()) {
|
||||
$tg = $tgService->update($tgForm, $tg);
|
||||
if ($tg) {
|
||||
$this->redirect("/admin/tg-bot/view/" . $tg->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/tg-bot/update/" . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$tg = Tgbot::find($id);
|
||||
|
||||
if (!$tg) {
|
||||
throw new Exception(message: "The dialog not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['tg' => $tg]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
#[NoReturn] public function actionDelete(int $id): void
|
||||
{
|
||||
$post = Tgbot::find($id)->first();
|
||||
if (!$post){
|
||||
throw new Exception(message: "The tg client not found");
|
||||
}
|
||||
|
||||
$post->delete();
|
||||
$this->redirect("/admin/tg-bot/");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user