Skip to content

Commit

Permalink
rework text length
Browse files Browse the repository at this point in the history
  • Loading branch information
ufaboy committed Oct 21, 2023
1 parent a1ec433 commit 2dc4ce1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
4 changes: 3 additions & 1 deletion common/models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -84,6 +85,7 @@ public function attributeLabels(): array {
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'last_read' => 'Last Read',
'text_length' => 'Text Length',
];
}

Expand Down
25 changes: 25 additions & 0 deletions migrations/m231021_152021_add_text_length_column_to_book_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use yii\db\Migration;

/**
* Handles adding columns to table `{{%book}}`.
*/
class m231021_152021_add_text_length_column_to_book_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('{{%book}}', 'text_length', $this->integer());
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('{{%book}}', 'text_length');
}
}
7 changes: 5 additions & 2 deletions modules/api/models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand All @@ -81,7 +82,8 @@ public function fields() {
// 'author',
// 'series',
'created_at',
'updated_at',];
'updated_at',
'text_length'];
}

public function extraFields() {
Expand Down Expand Up @@ -112,6 +114,7 @@ public function attributeLabels(): array {
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'last_read' => 'Last Read',
'text_length' => 'Text Length',
];
}

Expand Down
48 changes: 30 additions & 18 deletions modules/api/models/BookSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function fields(): array {
'length',
'updated_at',
'last_read',
'text_length'
];
}

Expand All @@ -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'],
];
}
Expand Down Expand Up @@ -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,
Expand All @@ -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],
Expand All @@ -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;
}
Expand Down

0 comments on commit 2dc4ce1

Please sign in to comment.