Skip to content

Commit

Permalink
feat: add sizes to api
Browse files Browse the repository at this point in the history
  • Loading branch information
ufaboy committed Sep 29, 2023
1 parent 245b580 commit 0e660c9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
36 changes: 34 additions & 2 deletions modules/api/models/BookSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class BookSearch extends Book {
public string $tag = '';
public int|null $length = null;
public string|null $size = null;
public string $authorName = '';
public string $seriesName = '';

Expand All @@ -21,6 +23,7 @@ class BookSearch extends Book {
public function attributes(): array {
return array_merge(parent::attributes(), ['author.name', 'series.name']);
}

public function fields(): array {
return [
'id',
Expand All @@ -32,17 +35,19 @@ public function fields(): array {
'tags',
'author',
'series',
'length',
'updated_at',
'last_read',
];
}

/**
* {@inheritdoc}
*/
public function rules(): array {
return [
[['id', 'view_count', 'rating', 'bookmark', 'author_id', 'series_id', 'created_at', 'updated_at', 'last_read'], 'integer'],
[['name', 'description', 'text', 'source', 'cover', 'tag', 'authorName', 'seriesName', 'author.name', 'series.name', 'perPage', 'sort', 'page'], 'safe'],
[['name', 'description', 'text', 'source', 'cover', 'tag', 'authorName', 'seriesName', 'author.name', 'series.name', 'perPage', 'sort', 'page', 'size'], 'safe'],
];
}

Expand All @@ -61,7 +66,11 @@ public function scenarios() {
*
* @return ActiveDataProvider
*/

public function search($params) {
$sizeStart = null;
$sizeLast = null;

$query = self::find();
$query->joinWith('author');
$query->joinWith('series');
Expand All @@ -86,7 +95,22 @@ public function search($params) {
// $query->where('0=1');
return $dataProvider;
}
$query->select(["book.*", 'count("tag"."id") AS tag_count']);

if ($this->size === 'S') {
$sizeStart = 0;
$sizeLast = 49999;
} elseif ($this->size === 'M') {
$sizeStart = 50000;
$sizeLast = 299999;
} elseif ($this->size === 'L') {
$sizeStart = 300000;
$sizeLast = 499999;
} elseif ($this->size === 'XL') {
$sizeStart = 500000;
$sizeLast = 999999999;
}

$query->select(["book.*", 'count("tag"."id") AS tag_count', 'LENGTH(book.text) as length']);

$query->andFilterWhere([
'book.id' => $this->id,
Expand All @@ -108,7 +132,15 @@ public function search($params) {
->andFilterWhere(['ilike', 'author.name', $this->authorName])
->andFilterWhere(['ilike', 'series.name', $this->seriesName]);

$query->andFilterWhere([
'between', 'LENGTH(book.text)', $sizeStart, $sizeLast
]);

$query->groupBy(['book.id', 'author.name', 'series.name']);
$dataProvider->sort->attributes['length'] = [
'asc' => ['length' => SORT_ASC],
'desc' => ['length' => SORT_DESC],
];
$dataProvider->sort->attributes['tags'] = [
'asc' => ['tag_count' => SORT_ASC],
'desc' => ['tag_count' => SORT_DESC],
Expand Down
7 changes: 6 additions & 1 deletion modules/api/models/ImageSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public function search($params): ActiveDataProvider {
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->joinWith('book');
$query->joinWith([
'book' => function ($query) {
$query->select(['id', 'name']); // Укажите нужные вам столбцы
},
]);
$this->load($params, '');

if (!$this->validate()) {
Expand All @@ -80,6 +84,7 @@ public function search($params): ActiveDataProvider {
->andFilterWhere(['ilike', 'book.name', $this->getAttribute('book.name')]);

$query->groupBy('image.id');

$dataProvider->sort->attributes['book.name'] = [
'asc' => ['book.name' => SORT_ASC],
'desc' => ['book.name' => SORT_DESC],
Expand Down

0 comments on commit 0e660c9

Please sign in to comment.