From 2dc4ce1f132a440ad523b560c15e5bed6b815fed Mon Sep 17 00:00:00 2001 From: Azat Kashapov Date: Sat, 21 Oct 2023 18:26:14 +0200 Subject: [PATCH] rework text length --- common/models/Book.php | 4 +- ...1_add_text_length_column_to_book_table.php | 25 ++++++++++ modules/api/models/Book.php | 7 ++- modules/api/models/BookSearch.php | 48 ++++++++++++------- 4 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 migrations/m231021_152021_add_text_length_column_to_book_table.php diff --git a/common/models/Book.php b/common/models/Book.php index 3e33bb0..4a2f666 100644 --- a/common/models/Book.php +++ b/common/models/Book.php @@ -24,6 +24,7 @@ * @property int|null $created_at * @property int|null $updated_at * @property int|null $last_read + * @property int|null $text_length * * @property Author $author * @property BookTag[] $bookTags @@ -56,7 +57,7 @@ public function behaviors(): array { public function rules(): array { return [ [['text'], 'string'], - [['view_count', 'rating', 'bookmark', 'author_id', 'series_id', 'created_at', 'updated_at', 'last_read'], 'integer'], + [['view_count', 'rating', 'bookmark', 'author_id', 'series_id', 'created_at', 'updated_at', 'last_read', 'text_length'], 'integer'], [['tag_ids'], 'safe'], [['name', 'cover'], 'string', 'max' => 255], [['description', 'source'], 'string', 'max' => 1024], @@ -84,6 +85,7 @@ public function attributeLabels(): array { 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'last_read' => 'Last Read', + 'text_length' => 'Text Length', ]; } diff --git a/migrations/m231021_152021_add_text_length_column_to_book_table.php b/migrations/m231021_152021_add_text_length_column_to_book_table.php new file mode 100644 index 0000000..7009eec --- /dev/null +++ b/migrations/m231021_152021_add_text_length_column_to_book_table.php @@ -0,0 +1,25 @@ +addColumn('{{%book}}', 'text_length', $this->integer()); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropColumn('{{%book}}', 'text_length'); + } +} diff --git a/modules/api/models/Book.php b/modules/api/models/Book.php index e5c6b2f..5d87706 100644 --- a/modules/api/models/Book.php +++ b/modules/api/models/Book.php @@ -24,6 +24,7 @@ * @property int|null $created_at * @property int|null $updated_at * @property int|null $last_read + * @property int|null $text_length * * @property Author $author * @property BookTag[] $bookTags @@ -57,7 +58,7 @@ public function behaviors(): array { public function rules(): array { return [ [['text'], 'string'], - [['view_count', 'rating', 'bookmark', 'author_id', 'series_id', 'created_at', 'updated_at', 'last_read'], 'integer'], + [['view_count', 'rating', 'bookmark', 'author_id', 'series_id', 'created_at', 'updated_at', 'last_read', 'text_length'], 'integer'], [['tag_ids', 'bookmark'], 'safe'], [['name', 'cover'], 'string', 'max' => 255], [['description', 'source'], 'string', 'max' => 1024], @@ -81,7 +82,8 @@ public function fields() { // 'author', // 'series', 'created_at', - 'updated_at',]; + 'updated_at', + 'text_length']; } public function extraFields() { @@ -112,6 +114,7 @@ public function attributeLabels(): array { 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'last_read' => 'Last Read', + 'text_length' => 'Text Length', ]; } diff --git a/modules/api/models/BookSearch.php b/modules/api/models/BookSearch.php index aafb8bc..2a1d006 100644 --- a/modules/api/models/BookSearch.php +++ b/modules/api/models/BookSearch.php @@ -39,6 +39,7 @@ public function fields(): array { 'length', 'updated_at', 'last_read', + 'text_length' ]; } @@ -47,7 +48,7 @@ public function fields(): array { */ public function rules(): array { return [ - [['id', 'view_count', 'rating', 'bookmark', 'author_id', 'series_id', 'created_at', 'updated_at', 'last_read'], 'integer'], + [['id', 'view_count', 'rating', 'bookmark', 'author_id', 'series_id', 'created_at', 'updated_at', 'last_read', 'text_length'], 'integer'], [['name', 'description', 'text', 'source', 'cover', 'tag', 'authorName', 'seriesName', 'author.name', 'series.name', 'perPage', 'sort', 'page', 'size'], 'safe'], ]; } @@ -111,7 +112,18 @@ public function search($params) { $sizeLast = 999999999; } - $query->select(["book.*", 'count("tag"."id") AS tag_count', 'LENGTH(book.text) as length']); +// $query->select(["book.*", 'count("tag"."id") AS tag_count', 'LENGTH(book.text) as length']); + $query->select(["book.id", + 'book.name', + 'book.description', + 'book.view_count', + 'book.rating', + 'book.cover', + 'book.author_id', + 'book.series_id', + 'book.updated_at', + 'book.last_read', + 'book.text_length']); $query->andFilterWhere([ 'book.id' => $this->id, @@ -134,18 +146,18 @@ public function search($params) { ->andFilterWhere(['ilike', 'series.name', $this->seriesName]); $query->andFilterWhere([ - 'between', 'LENGTH(book.text)', $sizeStart, $sizeLast + 'between', 'book.text_length', $sizeStart, $sizeLast ]); $query->groupBy(['book.id', 'author.name', 'series.name']); - $dataProvider->sort->attributes['length'] = [ - 'asc' => ['length' => SORT_ASC, 'book.id' => SORT_ASC], - 'desc' => ['length' => SORT_DESC, 'book.id' => SORT_DESC], - ]; - $dataProvider->sort->attributes['tags'] = [ - 'asc' => ['tag_count' => SORT_ASC, 'book.id' => SORT_ASC], - 'desc' => ['tag_count' => SORT_DESC, 'book.id' => SORT_DESC], + $dataProvider->sort->attributes['text_length'] = [ + 'asc' => ['text_length' => SORT_ASC, 'book.id' => SORT_ASC], + 'desc' => ['text_length' => SORT_DESC, 'book.id' => SORT_DESC], ]; +// $dataProvider->sort->attributes['tags'] = [ +// 'asc' => ['tag_count' => SORT_ASC, 'book.id' => SORT_ASC], +// 'desc' => ['tag_count' => SORT_DESC, 'book.id' => SORT_DESC], +// ]; $dataProvider->sort->attributes['author'] = [ 'asc' => ['author.name' => SORT_ASC, 'book.id' => SORT_ASC], 'desc' => ['author.name' => SORT_DESC, 'book.id' => SORT_DESC], @@ -155,20 +167,20 @@ public function search($params) { 'desc' => ['series.name' => SORT_DESC, 'book.id' => SORT_DESC], ]; $dataProvider->sort->attributes['rating'] = [ - 'asc' => [new \yii\db\Expression('rating ASC NULLS FIRST'), 'book.id' => SORT_ASC], - 'desc' => [new \yii\db\Expression('rating DESC NULLS LAST'), 'book.id' => SORT_DESC], + 'asc' => [new Expression('rating ASC NULLS FIRST'), 'book.id' => SORT_ASC], + 'desc' => [new Expression('rating DESC NULLS LAST'), 'book.id' => SORT_DESC], ]; $dataProvider->sort->attributes['updated_at'] = [ - 'asc' => [new \yii\db\Expression('updated_at ASC NULLS FIRST'), 'book.id' => SORT_ASC], - 'desc' => [new \yii\db\Expression('updated_at DESC NULLS LAST'), 'book.id' => SORT_DESC], + 'asc' => [new Expression('updated_at ASC NULLS FIRST'), 'book.id' => SORT_ASC], + 'desc' => [new Expression('updated_at DESC NULLS LAST'), 'book.id' => SORT_DESC], ]; $dataProvider->sort->attributes['view_count'] = [ - 'asc' => [new \yii\db\Expression('view_count ASC NULLS FIRST'), 'book.id' => SORT_ASC], - 'desc' => [new \yii\db\Expression('view_count DESC NULLS LAST'), 'book.id' => SORT_DESC], + 'asc' => [new Expression('view_count ASC NULLS FIRST'), 'book.id' => SORT_ASC], + 'desc' => [new Expression('view_count DESC NULLS LAST'), 'book.id' => SORT_DESC], ]; $dataProvider->sort->attributes['last_read'] = [ - 'asc' => [new \yii\db\Expression('last_read ASC NULLS FIRST'), 'book.id' => SORT_ASC], - 'desc' => [new \yii\db\Expression('last_read DESC NULLS LAST'), 'book.id' => SORT_DESC], + 'asc' => [new Expression('last_read ASC NULLS FIRST'), 'book.id' => SORT_ASC], + 'desc' => [new Expression('last_read DESC NULLS LAST'), 'book.id' => SORT_DESC], ]; return $dataProvider; }