forked from mirzaev/deeproots_bot
read database util and file cleanup
This commit is contained in:
parent
d867d640a4
commit
ced4e3241c
|
@ -6,24 +6,24 @@ namespace mirzaev\deeproots\models;
|
|||
|
||||
// Files of the project
|
||||
use mirzaev\deeproots\models\core,
|
||||
mirzaev\deeproots\models\account\localization;
|
||||
mirzaev\deeproots\models\account\localization;
|
||||
|
||||
// Svoboda time
|
||||
use svoboda\time\statement as svoboda;
|
||||
|
||||
// Baza database
|
||||
use mirzaev\baza\database,
|
||||
mirzaev\baza\column,
|
||||
mirzaev\baza\record,
|
||||
mirzaev\baza\enumerations\encoding,
|
||||
mirzaev\baza\enumerations\type;
|
||||
mirzaev\baza\column,
|
||||
mirzaev\baza\record,
|
||||
mirzaev\baza\enumerations\encoding,
|
||||
mirzaev\baza\enumerations\type;
|
||||
|
||||
// Framework for Telegram
|
||||
use Zanzara\Telegram\Type\User as model;
|
||||
|
||||
// Built-in libraries
|
||||
use Exception as exception,
|
||||
RuntimeException as exception_runtime;
|
||||
RuntimeException as exception_runtime;
|
||||
|
||||
/**
|
||||
* Telegram account
|
||||
|
@ -35,158 +35,158 @@ use Exception as exception,
|
|||
*/
|
||||
final class telegram extends core
|
||||
{
|
||||
/**
|
||||
* File
|
||||
*
|
||||
* @var string $database Path to the database file
|
||||
*/
|
||||
protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'telegram.baza';
|
||||
/**
|
||||
* File
|
||||
*
|
||||
* @var string $database Path to the database file
|
||||
*/
|
||||
protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'telegram.baza';
|
||||
|
||||
/**
|
||||
* Database
|
||||
*
|
||||
* @var database $database The database
|
||||
*/
|
||||
public protected(set) database $database;
|
||||
/**
|
||||
* Database
|
||||
*
|
||||
* @var database $database The database
|
||||
*/
|
||||
public protected(set) database $database;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// Initializing the database
|
||||
$this->database = new database()
|
||||
->encoding(encoding::utf8)
|
||||
->columns(
|
||||
new column('identifier', type::integer_unsigned),
|
||||
new column('domain', type::string, ['length' => 32]),
|
||||
new column('name_first', type::string, ['length' => 64]),
|
||||
new column('name_second', type::string, ['length' => 64]),
|
||||
new column('language', type::string, ['length' => 2]),
|
||||
new column('robot', type::char),
|
||||
new column('updated', type::integer_unsigned),
|
||||
new column('created', type::integer_unsigned)
|
||||
)
|
||||
->connect($this->file);
|
||||
}
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// Initializing the database
|
||||
$this->database = new database()
|
||||
->encoding(encoding::utf8)
|
||||
->columns(
|
||||
new column('identifier', type::integer_unsigned),
|
||||
new column('domain', type::string, ['length' => 32]),
|
||||
new column('name_first', type::string, ['length' => 64]),
|
||||
new column('name_second', type::string, ['length' => 64]),
|
||||
new column('language', type::string, ['length' => 2]),
|
||||
new column('robot', type::char),
|
||||
new column('updated', type::integer_unsigned),
|
||||
new column('created', type::integer_unsigned)
|
||||
)
|
||||
->connect($this->file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* Searches for the telegram account record in the database, and if it does not find it, then create
|
||||
*
|
||||
* @param model $telegram The telegram account
|
||||
*
|
||||
* @throws exception_runtime if update the telegram account record in the database by the telegram account values
|
||||
* @throws exception_runtime if failed to find the created telegram account
|
||||
* @throws exception_runtime if failed to create the telegram account
|
||||
*
|
||||
* @return record The telegram account record from the database
|
||||
*/
|
||||
public function initialize(model $telegram): record
|
||||
{
|
||||
// Searching for the account in the database
|
||||
$account = $this->database->read(filter: fn(record $record) => $record->identifier === $telegram->getId(), amount: 1)[0] ?? null;
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* Searches for the telegram account record in the database, and if it does not find it, then create
|
||||
*
|
||||
* @param model $telegram The telegram account
|
||||
*
|
||||
* @throws exception_runtime if failed to update the telegram account record in the database by the telegram account values
|
||||
* @throws exception_runtime if failed to find the created telegram account
|
||||
* @throws exception_runtime if failed to create the telegram account
|
||||
*
|
||||
* @return record The telegram account record from the database
|
||||
*/
|
||||
public function initialize(model $telegram): record
|
||||
{
|
||||
// Searching for the account in the database
|
||||
$account = $this->database->read(filter: fn(record $record) => $record->identifier === $telegram->getId(), amount: 1)[0] ?? null;
|
||||
|
||||
if ($account instanceof record) {
|
||||
// Found the telegram account record
|
||||
if ($account instanceof record) {
|
||||
// Found the telegram account record
|
||||
|
||||
if (
|
||||
$account->name_first !== $telegram->getFirstName() ||
|
||||
$account->name_second !== $telegram->getLastName() ||
|
||||
$account->domain !== $telegram->getUsername()
|
||||
) {
|
||||
// The telegram account was updated
|
||||
|
||||
// Updating the account in the database
|
||||
$updated = $this->database->read(
|
||||
filter: fn(record $record) => $record->identifier === $telegram->getId(),
|
||||
update: function (record &$record) use ($telegram){
|
||||
// Writing new values into the record
|
||||
$record->name_first = $telegram->getFirstName();
|
||||
$record->name_second = $telegram->getLastName();
|
||||
$record->domain = $telegram->getUsername();
|
||||
$record->updated = svoboda::timestamp();
|
||||
},
|
||||
amount: 1
|
||||
)[0] ?? null;
|
||||
if (
|
||||
$account->name_first !== $telegram->getFirstName() ||
|
||||
$account->name_second !== $telegram->getLastName() ||
|
||||
$account->domain !== $telegram->getUsername()
|
||||
) {
|
||||
// The telegram account was updated
|
||||
|
||||
if ($updated instanceof record && $updated->values() !== $account->values()) {
|
||||
// Updated the account in the database
|
||||
// Updating the account in the database
|
||||
$updated = $this->database->read(
|
||||
filter: fn(record $record) => $record->identifier === $telegram->getId(),
|
||||
update: function (record &$record) use ($telegram){
|
||||
// Writing new values into the record
|
||||
$record->name_first = $telegram->getFirstName();
|
||||
$record->name_second = $telegram->getLastName();
|
||||
$record->domain = $telegram->getUsername();
|
||||
$record->updated = svoboda::timestamp();
|
||||
},
|
||||
amount: 1
|
||||
)[0] ?? null;
|
||||
|
||||
// Exit (success)
|
||||
return $updated;
|
||||
} else {
|
||||
// Not updated the account in the database
|
||||
if ($updated instanceof record && $updated->values() !== $account->values()) {
|
||||
// Updated the account in the database
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception_runtime('Failed to update the account record in the database by the telegram account values');
|
||||
}
|
||||
}
|
||||
// Exit (success)
|
||||
return $updated;
|
||||
} else {
|
||||
// Not updated the account in the database
|
||||
|
||||
// Exit (success)
|
||||
return $account;
|
||||
} else {
|
||||
// Not found the account record
|
||||
// Exit (fail)
|
||||
throw new exception_runtime('Failed to update the account record in the database by the telegram account values');
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->create($telegram)) {
|
||||
// Created the account
|
||||
// Exit (success)
|
||||
return $account;
|
||||
} else {
|
||||
// Not found the account record
|
||||
|
||||
// Searching for the created telegram account in the database
|
||||
$account = $this->database->read(filter: fn(record $record) => $record->identifier === $telegram->getId(), amount: 1)[0] ?? null;
|
||||
if ($this->create($telegram)) {
|
||||
// Created the account
|
||||
|
||||
if ($account instanceof record) {
|
||||
// Found the created telegram account
|
||||
// Searching for the created telegram account in the database
|
||||
$account = $this->database->read(filter: fn(record $record) => $record->identifier === $telegram->getId(), amount: 1)[0] ?? null;
|
||||
|
||||
// Exit (success)
|
||||
return $account;
|
||||
} else {
|
||||
// Not found the created telegram account
|
||||
if ($account instanceof record) {
|
||||
// Found the created telegram account
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception_runtime('Failed to find the created telegram account');
|
||||
}
|
||||
} else {
|
||||
// Not created the telegram account
|
||||
// Exit (success)
|
||||
return $account;
|
||||
} else {
|
||||
// Not found the created telegram account
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception_runtime('Failed to create the telegram account');
|
||||
}
|
||||
}
|
||||
}
|
||||
// Exit (fail)
|
||||
throw new exception_runtime('Failed to find the created telegram account');
|
||||
}
|
||||
} else {
|
||||
// Not created the telegram account
|
||||
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* Creates the account record in the database
|
||||
*
|
||||
* @param model $telegram The telegram account
|
||||
*
|
||||
* @return int|false The record identifier, if created
|
||||
*/
|
||||
public function create(model $telegram): int|false
|
||||
{
|
||||
// Initializing the identifier
|
||||
$identifier = (int) $telegram->getId();
|
||||
// Exit (fail)
|
||||
throw new exception_runtime('Failed to create the telegram account');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initializing the record
|
||||
$record = $this->database->record(
|
||||
$identifier,
|
||||
$telegram->getUsername(),
|
||||
$telegram->getFirstName(),
|
||||
$telegram->getLastName(),
|
||||
$telegram->getLanguageCode(),
|
||||
(int) $telegram->isBot(),
|
||||
svoboda::timestamp(),
|
||||
svoboda::timestamp()
|
||||
);
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* Creates the account record in the database
|
||||
*
|
||||
* @param model $telegram The telegram account
|
||||
*
|
||||
* @return int|false The record identifier, if created
|
||||
*/
|
||||
public function create(model $telegram): int|false
|
||||
{
|
||||
// Initializing the identifier
|
||||
$identifier = (int) $telegram->getId();
|
||||
|
||||
// Creating the record in the database
|
||||
$created = $this->database->write($record);
|
||||
// Initializing the record
|
||||
$record = $this->database->record(
|
||||
$identifier,
|
||||
$telegram->getUsername(),
|
||||
$telegram->getFirstName(),
|
||||
$telegram->getLastName(),
|
||||
$telegram->getLanguageCode(),
|
||||
(int) $telegram->isBot(),
|
||||
svoboda::timestamp(),
|
||||
svoboda::timestamp()
|
||||
);
|
||||
|
||||
// Exit (success)
|
||||
return $created ? $identifier : false;
|
||||
}
|
||||
// Creating the record in the database
|
||||
$created = $this->database->write($record);
|
||||
|
||||
// Exit (success)
|
||||
return $created ? $identifier : false;
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -6,30 +6,30 @@ namespace mirzaev\deeproots;
|
|||
|
||||
// Framework for PHP
|
||||
use mirzaev\minimal\core,
|
||||
mirzaev\minimal\route;
|
||||
mirzaev\minimal\route;
|
||||
|
||||
// Enabling debugging
|
||||
/* ini_set('error_reporting', E_ALL);
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1); */
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1); */
|
||||
|
||||
// Initializing path to the public directory
|
||||
// Initializing path to the public directory
|
||||
define('INDEX', __DIR__);
|
||||
|
||||
// Initializing path to the project root directory
|
||||
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
|
||||
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
|
||||
|
||||
// Initializing path to the directory of views
|
||||
define('VIEWS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views');
|
||||
// Initializing path to the directory of views
|
||||
define('VIEWS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views');
|
||||
|
||||
// Initializing path to the directory of settings
|
||||
define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings');
|
||||
// Initializing path to the directory of settings
|
||||
define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings');
|
||||
|
||||
// Initializing system settings
|
||||
// Initializing system settings
|
||||
require SETTINGS . DIRECTORY_SEPARATOR . 'system.php';
|
||||
|
||||
// Initializing path to the directory of the storage
|
||||
define('STORAGE', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'storage');
|
||||
// Initializing path to the directory of the storage
|
||||
define('STORAGE', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'storage');
|
||||
|
||||
// Initializing path to the databases directory
|
||||
define('DATABASES', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'databases');
|
||||
|
@ -42,8 +42,8 @@ $core = new core(namespace: __NAMESPACE__);
|
|||
|
||||
// Initializing routes
|
||||
$core->router
|
||||
->write('/', new route('index', 'index'), 'GET')
|
||||
;
|
||||
->write('/', new route('index', 'index'), 'GET')
|
||||
;
|
||||
|
||||
// Handling request
|
||||
$core->start();
|
||||
|
|
|
@ -6,28 +6,28 @@ namespace mirzaev\deeproots;
|
|||
|
||||
// Files of the project
|
||||
use mirzaev\deeproots\models\telegram\middlewares,
|
||||
mirzaev\deeproots\models\telegram\commands,
|
||||
mirzaev\deeproots\models\telegram\settings,
|
||||
mirzaev\deeproots\models\telegram\processes\question\search as process_question_search,
|
||||
mirzaev\deeproots\models\telegram\buttons\question\search as buttons_question_search,
|
||||
mirzaev\deeproots\models\telegram\processes\question\create as process_question_create,
|
||||
mirzaev\deeproots\models\enumerations\language;
|
||||
mirzaev\deeproots\models\telegram\commands,
|
||||
mirzaev\deeproots\models\telegram\settings,
|
||||
mirzaev\deeproots\models\telegram\processes\question\search as process_question_search,
|
||||
mirzaev\deeproots\models\telegram\buttons\question\search as buttons_question_search,
|
||||
mirzaev\deeproots\models\telegram\processes\question\create as process_question_create,
|
||||
mirzaev\deeproots\models\enumerations\language;
|
||||
|
||||
// Framework for Telegram
|
||||
use Zanzara\Zanzara as zanzara,
|
||||
Zanzara\Context as context,
|
||||
Zanzara\Config as config;
|
||||
Zanzara\Context as context,
|
||||
Zanzara\Config as config;
|
||||
|
||||
// Enabling debugging
|
||||
/* ini_set('error_reporting', E_ALL);
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1); */
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1); */
|
||||
|
||||
// Initializing path to the public directory
|
||||
// Initializing path to the public directory
|
||||
define('INDEX', __DIR__);
|
||||
|
||||
// Initializing path to the root directory
|
||||
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
|
||||
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
|
||||
|
||||
// Initializing path to the settings directory
|
||||
define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings');
|
||||
|
@ -82,10 +82,10 @@ $robot->onCommand('society', [commands::class, 'society']);
|
|||
|
||||
// Initializing the robot settings language buttons handlers
|
||||
foreach (language::cases() as $language) {
|
||||
// Iterating over languages
|
||||
// Iterating over languages
|
||||
|
||||
// Initializing language buttons
|
||||
$robot->onCbQueryData(['settings_language_' . $language->name], fn(context $context) => settings::language($context, $language));
|
||||
// Initializing language buttons
|
||||
$robot->onCbQueryData(['settings_language_' . $language->name], fn(context $context) => settings::language($context, $language));
|
||||
};
|
||||
|
||||
$robot->onCbQueryData(['system_questions_search_identifier'], [buttons_question_search::class, 'identifier'])->middleware([middlewares::class, 'system_questions']);
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
use mirzaev\baza\database,
|
||||
mirzaev\baza\column,
|
||||
mirzaev\baza\record,
|
||||
mirzaev\baza\enumerations\encoding,
|
||||
mirzaev\baza\enumerations\type;
|
||||
|
||||
// Initializing path constants
|
||||
define('INDEX', __DIR__);
|
||||
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
|
||||
define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings');
|
||||
define('DATABASES', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'databases');
|
||||
|
||||
// Initializing dependencies and system settings
|
||||
require ROOT . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
|
||||
require SETTINGS . DIRECTORY_SEPARATOR . 'system.php';
|
||||
|
||||
// Initializing a path to the Baza database binary file
|
||||
$file = DATABASES . DIRECTORY_SEPARATOR . 'telegram.baza';
|
||||
|
||||
// Instanciating new database class we want to read
|
||||
$database = new database()
|
||||
->encoding(encoding::utf8)
|
||||
->columns(
|
||||
new column('identifier', type::integer_unsigned),
|
||||
new column('domain', type::string, ['length' => 32]),
|
||||
new column('name_first', type::string, ['length' => 64]),
|
||||
new column('name_second', type::string, ['length' => 64]),
|
||||
new column('language', type::string, ['length' => 2]),
|
||||
new column('robot', type::char),
|
||||
new column('updated', type::integer_unsigned),
|
||||
new column('created', type::integer_unsigned)
|
||||
)
|
||||
->connect($file);
|
||||
|
||||
// Reading the whole database array
|
||||
$read = $database->read();
|
||||
print_r($read);
|
Loading…
Reference in New Issue