diff --git a/core/lib/Core/Api/ApiController.class.php b/core/lib/Core/Api/ApiController.class.php index b12faa8..33e2afe 100644 --- a/core/lib/Core/Api/ApiController.class.php +++ b/core/lib/Core/Api/ApiController.class.php @@ -38,6 +38,15 @@ /** @var Request $request Объект запроса */ private Request $request; + /** @var int $from Выборка ОТ */ + private int $from; + + /** @var int|null $to Выборка ДО */ + private ?int $to; + + /** @var int|null $count Общее количество записей */ + private ?int $count; + /** * Конструктор * @@ -47,6 +56,36 @@ { $this->userObject = $userObject; $this->request = new Request(); + + // Задаем параметры пагинации при наличии + $from = $this->request->getProperty('from'); + $to = $this->request->getProperty('to'); + if ($from !== null) { + $this->from = (int)$from; + } else { + $this->from = 0; + } + + if ($to !== null) { + $this->to = (int)$to; + } else { + $this->to = PAGINATION_LIMIT; + } + } + + /** + * Получение мета информации + * + * @return array + */ + private function getMetaData(): array + { + return [ + 'count' => $this->count, + 'from' => $this->from, + 'to' => $this->to, + ]; + } /** @@ -197,9 +236,25 @@ ); } + /** + * Тестовый метод 1 (не требующий авторизации) + * + * @return void + * @throws \JsonException + */ + public static function test(): void + { + ApiView::output( + [ + 'message' => 'test completed', + 'randomInt' => random_int(1000, 9999), + 'hash' => md5(time()), + ] + ); + } /** - * Тестовый метод (не требующий авторизации) + * Тестовый метод 2 (не требующий авторизации) * * @return void * @throws \JsonException @@ -248,5 +303,56 @@ ); } + /** + * Получение списка диалогов + * + * @return void + * @throws CoreException + * @throws \JsonException + */ + public function getDialogs(): void + { + ApiView::output($this->userObject->getDialogs()); + } + + /** + * Получение сообщений диалога + * + * @return void + * @throws CoreException + * @throws \JsonException + */ + public function getDialog(): void + { + $dialogId = (int)trim($this->request->getProperty('dialogId')); + if ($dialogId === 0) { + throw new ApiException('Идентификатор диалога некорректен', ApiException::ERROR_INPUT_DATA); + } + $this->count = $this->userObject->getDialogObject()->getDialogMessagesCount($dialogId); + ApiView::output( + $this->userObject->getDialogObject()->getMessages($dialogId, true, $this->getMetaData()), + $this->getMetaData() + ); + } + + /** + * Получение сообщений диалога + * + * @return void + * @throws CoreException + * @throws \JsonException + */ + public function sendMessage(): void + { + $to = (int)trim($this->request->getProperty('toUserId')); + if ($to === 0) { + throw new ApiException('Идентификатор диалога некорректен', ApiException::ERROR_INPUT_DATA); + } + $message = trim($this->request->getProperty('message')); + if (empty($message)) { + throw new ApiException('Сообщение не может быть пустым', ApiException::ERROR_INPUT_DATA); + } + ApiView::output($this->userObject->getDialogObject()->sendMessage($to, $message)); + } } diff --git a/core/lib/Core/Api/ApiView.class.php b/core/lib/Core/Api/ApiView.class.php index 78a33cf..6bd9eba 100644 --- a/core/lib/Core/Api/ApiView.class.php +++ b/core/lib/Core/Api/ApiView.class.php @@ -56,20 +56,23 @@ * @return void * @throws \JsonException */ - public static function output($data): void + public static function output($data, $meta = []): void { header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Methods: *'); header('Access-Control-Allow-Headers: *'); - echo json_encode( - [ - 'success' => true, - 'time' => time(), - 'data' => self::prepare($data), - ], - JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES + + $result = [ + 'success' => true, + 'time' => time(), + ]; + if (!empty($meta)) { + $result['meta'] = $meta; + } + $result['data'] = self::prepare($data); + echo json_encode($result, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); } diff --git a/core/lib/Core/Models/Dialog.class.php b/core/lib/Core/Models/Dialog.class.php index b6be406..5cb5f83 100644 --- a/core/lib/Core/Models/Dialog.class.php +++ b/core/lib/Core/Models/Dialog.class.php @@ -133,11 +133,15 @@ * @return array * @throws CoreException */ - public function getMessages(int $dialogId, bool $markDialogViewed = false): array + public function getMessages(int $dialogId, bool $markDialogViewed = false, ?array $paginationData = null): array { /** @var $DB DB Объект базы данных */ $DB = DB::getInstance(); - $messages = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '" ORDER BY id ASC'); + $paginationSqlString = ''; + if ($paginationData !== null) { + $paginationSqlString = ' LIMIT ' . $paginationData['from'] . ', ' . $paginationData['to']; + } + $messages = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '" ORDER BY id ASC' . $paginationSqlString); if ($markDialogViewed) { $this->markDialogViewed($dialogId); } @@ -224,5 +228,19 @@ return $DB->getCountItems(self::TABLE_MESSAGES, ['viewed' => CODE_VALUE_N, 'user_to' => $this->user->getId()]); } + /** + * Получение количества сообщений в диалоге + * + * @param int $dialogId Идентификатор диалога + * + * @return int Количество + */ + public function getDialogMessagesCount(int $dialogId): int + { + /** @var $DB DB Объект базы данных */ + $DB = DB::getInstance(); + return $DB->getCountItems(self::TABLE_MESSAGES, ['dialog_id' => $dialogId]); + } + }