Skip to content

Commit 084b239

Browse files
committedOct 2, 2022
Merge branch 'via-yiisoft-active-record'
2 parents a0ab9dd + af05f75 commit 084b239

15 files changed

+238
-344
lines changed
 

‎App.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class App
88
{
99
public static DbConfig $database;
10-
public static bool $debug = false;
10+
public static bool $development = false;
1111
public static BotConfig $bot;
1212
public static I18n $i18n;
1313
public static array $params = [];
@@ -17,12 +17,12 @@ public function __construct(
1717
BotConfig $bot,
1818
I18n $i18n,
1919
array $params = [],
20-
bool $debug = false,
20+
bool $development = false,
2121
)
2222
{
2323
self::$database = $database;
2424
self::$bot = $bot;
25-
self::$debug = $debug;
25+
self::$development = $development;
2626
self::$i18n = $i18n;
2727
self::$params = $params;
2828
}

‎Commands/HelloCommand.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
namespace Commands;
44

55
use App;
6+
use Throwable;
67
use Services\UserService;
78
use Longman\TelegramBot\Request;
89
use Longman\TelegramBot\Telegram;
10+
use Yiisoft\Db\Exception\Exception;
911
use Longman\TelegramBot\Entities\Update;
1012
use Longman\TelegramBot\Commands\UserCommand;
1113
use Longman\TelegramBot\Entities\ServerResponse;
14+
use Yiisoft\Db\Exception\InvalidConfigException;
15+
use Longman\TelegramBot\Exception\TelegramException;
1216

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

32+
/**
33+
* @throws InvalidConfigException
34+
* @throws Throwable
35+
* @throws Exception
36+
* @throws TelegramException
37+
*/
2838
public function execute(): ServerResponse
2939
{
30-
3140
$chat_id = $this->getMessage()->getChat()->getId();
3241
$user = $this->userService->getByChatId($chat_id);
3342
if ($user === null) {
@@ -36,9 +45,8 @@ public function execute(): ServerResponse
3645
'text' => App::$i18n->get("Please, send /start command!"),
3746
]);
3847
}
39-
$user->step = 'hello';
40-
$this->userService->update($user);
41-
48+
$this->userService->changeStep($user, 'hello');
49+
$this->userService->save($user);
4250
return Request::sendMessage([
4351
'chat_id' => $chat_id,
4452
'text' => App::$i18n->get("Hello"),

‎Commands/StartCommand.php

+28-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
namespace Commands;
44

55
use App;
6+
use Throwable;
67
use Services\UserService;
78
use Longman\TelegramBot\Request;
89
use Longman\TelegramBot\Telegram;
10+
use Yiisoft\Db\Exception\Exception;
911
use Longman\TelegramBot\Entities\Update;
1012
use Longman\TelegramBot\Commands\UserCommand;
13+
use Yiisoft\Db\Exception\StaleObjectException;
14+
use Yiisoft\Db\Exception\InvalidConfigException;
1115
use Longman\TelegramBot\Entities\ServerResponse;
16+
use Longman\TelegramBot\Exception\TelegramException;
1217

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

33+
/**
34+
* @throws InvalidConfigException
35+
* @throws StaleObjectException
36+
* @throws Exception
37+
* @throws Throwable
38+
* @throws TelegramException
39+
*/
2840
public function execute(): ServerResponse
2941
{
30-
$chat_id = $this->getMessage()->getChat()->getId();
31-
$user = $this->userService->getByChatId($chat_id);
42+
$chat = $this->getMessage()->getChat();
43+
$from = $this->getMessage()->getFrom();
44+
$user = $this->userService->getByChatId($chat->getId());
3245
if ($user === null) {
3346
$this->userService->create(
34-
chat_id: $chat_id,
47+
chat_id: $chat->getId(),
3548
step: 'start',
49+
username: $chat->getUsername(),
50+
language: $from->getLanguageCode(),
3651
);
3752
return Request::sendMessage([
38-
'chat_id' => $chat_id,
53+
'chat_id' => $chat->getId(),
3954
'text' => App::$i18n->get("Hello"),
4055
]);
4156
}
42-
$user->step = 'start';
43-
$this->userService->update($user);
57+
if ($user->language !== $from->getLanguageCode()) {
58+
$user->language = $from->getLanguageCode();
59+
}
60+
if ($user->username !== $chat->getUsername()) {
61+
$user->username = $chat->getUsername();
62+
}
63+
$this->userService->changeStep($user, 'start');
64+
$this->userService->save($user);
4465
return Request::sendMessage([
45-
'chat_id' => $chat_id,
66+
'chat_id' => $chat->getId(),
4667
'text' => App::$i18n->get("Home"),
4768
]);
4869
}

‎Config/Database/pgsql.sql

+9-109
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,13 @@
1-
------------------------------
2-
-- user table ----------------
3-
CREATE TABLE IF NOT EXISTS public."user"
1+
CREATE TABLE "user"
42
(
5-
id BIGSERIAL NOT NULL
6-
CONSTRAINT user_pk PRIMARY KEY,
7-
chat_id BIGINT NOT NULL,
8-
step VARCHAR,
9-
language VARCHAR(10),
10-
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
11-
created_at INTEGER NOT NULL
12-
);
13-
14-
CREATE UNIQUE INDEX IF NOT EXISTS user_chat_id_uindex
15-
ON public."user" (chat_id);
16-
17-
CREATE INDEX IF NOT EXISTS user_status_index
18-
ON public."user" (status);
19-
20-
21-
------------------------------
22-
-- user_username table ----------------
23-
CREATE TABLE IF NOT EXISTS public."user_username"
24-
(
25-
id BIGSERIAL NOT NULL
26-
CONSTRAINT user_username_pk PRIMARY KEY,
27-
chat_id BIGINT NOT NULL,
28-
username VARCHAR(32),
29-
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
30-
created_at INTEGER NOT NULL
31-
);
32-
33-
CREATE UNIQUE INDEX IF NOT EXISTS user_username_chat_id_uindex
34-
ON public."user_username" (chat_id);
35-
36-
CREATE INDEX IF NOT EXISTS user_username_status_index
37-
ON public."user_username" (status);
38-
39-
40-
------------------------------
41-
-- user_first_name table ----------------
42-
CREATE TABLE IF NOT EXISTS public."user_first_name"
43-
(
44-
id BIGSERIAL NOT NULL
45-
CONSTRAINT user_first_name_pk PRIMARY KEY,
46-
chat_id BIGINT NOT NULL,
47-
first_name VARCHAR(64),
48-
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
49-
created_at INTEGER NOT NULL
50-
);
51-
52-
CREATE UNIQUE INDEX IF NOT EXISTS user_first_name_chat_id_uindex
53-
ON public."user_first_name" (chat_id);
54-
55-
CREATE INDEX IF NOT EXISTS user_first_name_status_index
56-
ON public."user_first_name" (status);
57-
58-
59-
------------------------------
60-
-- user_last_name table ----------------
61-
CREATE TABLE IF NOT EXISTS public."user_last_name"
62-
(
63-
id BIGSERIAL NOT NULL
64-
CONSTRAINT user_last_name_pk PRIMARY KEY,
65-
chat_id BIGINT NOT NULL,
66-
last_name VARCHAR(64),
67-
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
68-
created_at INTEGER NOT NULL
69-
);
70-
71-
CREATE UNIQUE INDEX IF NOT EXISTS user_last_name_chat_id_uindex
72-
ON public."user_last_name" (chat_id);
73-
74-
CREATE INDEX IF NOT EXISTS user_last_name_status_index
75-
ON public."user_last_name" (status);
76-
77-
78-
------------------------------
79-
-- user_bio table ----------------
80-
CREATE TABLE IF NOT EXISTS public."user_bio"
81-
(
82-
id BIGSERIAL NOT NULL
83-
CONSTRAINT user_bio_pk PRIMARY KEY,
84-
chat_id BIGINT NOT NULL,
85-
bio VARCHAR(200),
86-
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
87-
created_at INTEGER NOT NULL
88-
);
89-
90-
CREATE UNIQUE INDEX IF NOT EXISTS user_bio_chat_id_uindex
91-
ON public."user_bio" (chat_id);
92-
93-
CREATE INDEX IF NOT EXISTS user_bio_status_index
94-
ON public."user_bio" (status);
95-
96-
97-
------------------------------
98-
-- user_phone table ----------------
99-
CREATE TABLE IF NOT EXISTS public."user_phone"
100-
(
101-
id BIGSERIAL NOT NULL
102-
CONSTRAINT user_phone_pk PRIMARY KEY,
103-
chat_id BIGINT NOT NULL,
3+
id SERIAL PRIMARY KEY,
4+
chat_id BIGINT,
5+
step VARCHAR(200),
1046
phone VARCHAR(20),
105-
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
106-
created_at INTEGER NOT NULL
7+
username VARCHAR(64),
8+
language VARCHAR(10),
9+
created_at integer,
10+
status VARCHAR(30)
10711
);
12+
CREATE INDEX index_foreign_key_user_chat ON "user" (chat_id);
10813

109-
CREATE UNIQUE INDEX IF NOT EXISTS user_phone_chat_id_uindex
110-
ON public."user_phone" (chat_id);
111-
112-
CREATE INDEX IF NOT EXISTS user_phone_status_index
113-
ON public."user_phone" (status);

‎Config/DbConfig.php

+29-7
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,52 @@
22

33
namespace Config;
44

5-
use PDO;
5+
use Yiisoft\Cache\Cache;
6+
use Yiisoft\Cache\ArrayCache;
7+
use Yiisoft\Db\Pgsql\PDODriver;
8+
use Yiisoft\Db\Cache\QueryCache;
9+
use Yiisoft\Db\Cache\SchemaCache;
10+
use Yiisoft\Db\Pgsql\ConnectionPDO;
611

712
class DbConfig
813
{
9-
public static PDO $pdo;
14+
public static ConnectionPDO $connection;
15+
public static PDODriver $driver;
1016

1117
public function __construct(
12-
private readonly string $schema = 'pgsql',
1318
private readonly string $host = 'localhost',
1419
private readonly int $port = 5432,
1520
private readonly string $username = 'postgres',
1621
private readonly string $password = '',
1722
private readonly string $dbname = '',
18-
private readonly array $options = [],
23+
private readonly array $attributes = [],
24+
private ?QueryCache $queryCache = null,
25+
private ?SchemaCache $schemaCache = null,
1926
)
2027
{
21-
self::$pdo = new PDO(
22-
dsn: $this->schema . ':' .
28+
self::$driver = new PDODriver(
29+
dsn: 'pgsql:' .
2330
'host=' . $this->host . ';' .
2431
'port=' . $this->port . ';' .
2532
'dbname=' . $this->dbname . ';',
2633
username: $this->username,
2734
password: $this->password,
28-
options: $options
35+
attributes: $this->attributes);
36+
$handler = new ArrayCache();
37+
if (is_null($this->queryCache)) {
38+
$this->queryCache = new QueryCache(
39+
cache: new Cache($handler, 3600)
40+
);
41+
}
42+
if (is_null($this->schemaCache)) {
43+
$this->schemaCache = new SchemaCache(
44+
cache: new Cache($handler, 3600)
45+
);
46+
}
47+
self::$connection = new ConnectionPDO(
48+
driver: self::$driver,
49+
queryCache: $this->queryCache,
50+
schemaCache: $this->schemaCache,
2951
);
3052
}
3153
}

‎Models/BaseModel.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Models;
44

5-
abstract class BaseModel implements ModelInterface
5+
use Yiisoft\ActiveRecord\ActiveRecord;
6+
7+
abstract class BaseModel extends ActiveRecord
68
{
79
}

‎Models/ModelInterface.php

-10
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.