Skip to content

Commit

Permalink
Merge branch 'via-yiisoft-active-record'
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmuminov committed Oct 2, 2022
2 parents a0ab9dd + af05f75 commit 084b239
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 344 deletions.
6 changes: 3 additions & 3 deletions App.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class App
{
public static DbConfig $database;
public static bool $debug = false;
public static bool $development = false;
public static BotConfig $bot;
public static I18n $i18n;
public static array $params = [];
Expand All @@ -17,12 +17,12 @@ public function __construct(
BotConfig $bot,
I18n $i18n,
array $params = [],
bool $debug = false,
bool $development = false,
)
{
self::$database = $database;
self::$bot = $bot;
self::$debug = $debug;
self::$development = $development;
self::$i18n = $i18n;
self::$params = $params;
}
Expand Down
16 changes: 12 additions & 4 deletions Commands/HelloCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
namespace Commands;

use App;
use Throwable;
use Services\UserService;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Yiisoft\Db\Exception\Exception;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Yiisoft\Db\Exception\InvalidConfigException;
use Longman\TelegramBot\Exception\TelegramException;

class HelloCommand extends UserCommand
{
Expand All @@ -25,9 +29,14 @@ public function __construct(Telegram $telegram, ?Update $update = null)
parent::__construct($telegram, $update);
}

/**
* @throws InvalidConfigException
* @throws Throwable
* @throws Exception
* @throws TelegramException
*/
public function execute(): ServerResponse
{

$chat_id = $this->getMessage()->getChat()->getId();
$user = $this->userService->getByChatId($chat_id);
if ($user === null) {
Expand All @@ -36,9 +45,8 @@ public function execute(): ServerResponse
'text' => App::$i18n->get("Please, send /start command!"),
]);
}
$user->step = 'hello';
$this->userService->update($user);

$this->userService->changeStep($user, 'hello');
$this->userService->save($user);
return Request::sendMessage([
'chat_id' => $chat_id,
'text' => App::$i18n->get("Hello"),
Expand Down
35 changes: 28 additions & 7 deletions Commands/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace Commands;

use App;
use Throwable;
use Services\UserService;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Yiisoft\Db\Exception\Exception;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Commands\UserCommand;
use Yiisoft\Db\Exception\StaleObjectException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;

class StartCommand extends UserCommand
{
Expand All @@ -25,24 +30,40 @@ public function __construct(Telegram $telegram, ?Update $update = null)
parent::__construct(telegram: $telegram, update: $update);
}

/**
* @throws InvalidConfigException
* @throws StaleObjectException
* @throws Exception
* @throws Throwable
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$chat_id = $this->getMessage()->getChat()->getId();
$user = $this->userService->getByChatId($chat_id);
$chat = $this->getMessage()->getChat();
$from = $this->getMessage()->getFrom();
$user = $this->userService->getByChatId($chat->getId());
if ($user === null) {
$this->userService->create(
chat_id: $chat_id,
chat_id: $chat->getId(),
step: 'start',
username: $chat->getUsername(),
language: $from->getLanguageCode(),
);
return Request::sendMessage([
'chat_id' => $chat_id,
'chat_id' => $chat->getId(),
'text' => App::$i18n->get("Hello"),
]);
}
$user->step = 'start';
$this->userService->update($user);
if ($user->language !== $from->getLanguageCode()) {
$user->language = $from->getLanguageCode();
}
if ($user->username !== $chat->getUsername()) {
$user->username = $chat->getUsername();
}
$this->userService->changeStep($user, 'start');
$this->userService->save($user);
return Request::sendMessage([
'chat_id' => $chat_id,
'chat_id' => $chat->getId(),
'text' => App::$i18n->get("Home"),
]);
}
Expand Down
118 changes: 9 additions & 109 deletions Config/Database/pgsql.sql
Original file line number Diff line number Diff line change
@@ -1,113 +1,13 @@
------------------------------
-- user table ----------------
CREATE TABLE IF NOT EXISTS public."user"
CREATE TABLE "user"
(
id BIGSERIAL NOT NULL
CONSTRAINT user_pk PRIMARY KEY,
chat_id BIGINT NOT NULL,
step VARCHAR,
language VARCHAR(10),
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
created_at INTEGER NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS user_chat_id_uindex
ON public."user" (chat_id);

CREATE INDEX IF NOT EXISTS user_status_index
ON public."user" (status);


------------------------------
-- user_username table ----------------
CREATE TABLE IF NOT EXISTS public."user_username"
(
id BIGSERIAL NOT NULL
CONSTRAINT user_username_pk PRIMARY KEY,
chat_id BIGINT NOT NULL,
username VARCHAR(32),
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
created_at INTEGER NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS user_username_chat_id_uindex
ON public."user_username" (chat_id);

CREATE INDEX IF NOT EXISTS user_username_status_index
ON public."user_username" (status);


------------------------------
-- user_first_name table ----------------
CREATE TABLE IF NOT EXISTS public."user_first_name"
(
id BIGSERIAL NOT NULL
CONSTRAINT user_first_name_pk PRIMARY KEY,
chat_id BIGINT NOT NULL,
first_name VARCHAR(64),
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
created_at INTEGER NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS user_first_name_chat_id_uindex
ON public."user_first_name" (chat_id);

CREATE INDEX IF NOT EXISTS user_first_name_status_index
ON public."user_first_name" (status);


------------------------------
-- user_last_name table ----------------
CREATE TABLE IF NOT EXISTS public."user_last_name"
(
id BIGSERIAL NOT NULL
CONSTRAINT user_last_name_pk PRIMARY KEY,
chat_id BIGINT NOT NULL,
last_name VARCHAR(64),
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
created_at INTEGER NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS user_last_name_chat_id_uindex
ON public."user_last_name" (chat_id);

CREATE INDEX IF NOT EXISTS user_last_name_status_index
ON public."user_last_name" (status);


------------------------------
-- user_bio table ----------------
CREATE TABLE IF NOT EXISTS public."user_bio"
(
id BIGSERIAL NOT NULL
CONSTRAINT user_bio_pk PRIMARY KEY,
chat_id BIGINT NOT NULL,
bio VARCHAR(200),
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
created_at INTEGER NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS user_bio_chat_id_uindex
ON public."user_bio" (chat_id);

CREATE INDEX IF NOT EXISTS user_bio_status_index
ON public."user_bio" (status);


------------------------------
-- user_phone table ----------------
CREATE TABLE IF NOT EXISTS public."user_phone"
(
id BIGSERIAL NOT NULL
CONSTRAINT user_phone_pk PRIMARY KEY,
chat_id BIGINT NOT NULL,
id SERIAL PRIMARY KEY,
chat_id BIGINT,
step VARCHAR(200),
phone VARCHAR(20),
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
created_at INTEGER NOT NULL
username VARCHAR(64),
language VARCHAR(10),
created_at integer,
status VARCHAR(30)
);
CREATE INDEX index_foreign_key_user_chat ON "user" (chat_id);

CREATE UNIQUE INDEX IF NOT EXISTS user_phone_chat_id_uindex
ON public."user_phone" (chat_id);

CREATE INDEX IF NOT EXISTS user_phone_status_index
ON public."user_phone" (status);
36 changes: 29 additions & 7 deletions Config/DbConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,52 @@

namespace Config;

use PDO;
use Yiisoft\Cache\Cache;
use Yiisoft\Cache\ArrayCache;
use Yiisoft\Db\Pgsql\PDODriver;
use Yiisoft\Db\Cache\QueryCache;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Pgsql\ConnectionPDO;

class DbConfig
{
public static PDO $pdo;
public static ConnectionPDO $connection;
public static PDODriver $driver;

public function __construct(
private readonly string $schema = 'pgsql',
private readonly string $host = 'localhost',
private readonly int $port = 5432,
private readonly string $username = 'postgres',
private readonly string $password = '',
private readonly string $dbname = '',
private readonly array $options = [],
private readonly array $attributes = [],
private ?QueryCache $queryCache = null,
private ?SchemaCache $schemaCache = null,
)
{
self::$pdo = new PDO(
dsn: $this->schema . ':' .
self::$driver = new PDODriver(
dsn: 'pgsql:' .
'host=' . $this->host . ';' .
'port=' . $this->port . ';' .
'dbname=' . $this->dbname . ';',
username: $this->username,
password: $this->password,
options: $options
attributes: $this->attributes);
$handler = new ArrayCache();
if (is_null($this->queryCache)) {
$this->queryCache = new QueryCache(
cache: new Cache($handler, 3600)
);
}
if (is_null($this->schemaCache)) {
$this->schemaCache = new SchemaCache(
cache: new Cache($handler, 3600)
);
}
self::$connection = new ConnectionPDO(
driver: self::$driver,
queryCache: $this->queryCache,
schemaCache: $this->schemaCache,
);
}
}
4 changes: 3 additions & 1 deletion Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Models;

abstract class BaseModel implements ModelInterface
use Yiisoft\ActiveRecord\ActiveRecord;

abstract class BaseModel extends ActiveRecord
{
}
10 changes: 0 additions & 10 deletions Models/ModelInterface.php

This file was deleted.

Loading

0 comments on commit 084b239

Please sign in to comment.