Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Fixed broadcast adapter func and random anime/manga endpoint #543

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Jikan\Jikan;
use Jikan\Request\Anime\AnimeRequest;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use MongoDB\Model\BSONDocument;

class Anime extends JikanApiSearchableModel
{
Expand Down Expand Up @@ -407,7 +408,7 @@
];
}

private function adaptBroadcastValue(array|string|null $broadcast): array
private function adaptBroadcastValue(array|string|null|BSONDocument $broadcast): array
{
$null_value = [
'day' => null,
Expand All @@ -423,6 +424,10 @@
return $broadcast;
}

if ($broadcast instanceof BSONDocument) {
return $broadcast->getArrayCopy();

Check warning on line 428 in app/Anime.php

View check run for this annotation

Codecov / codecov/patch

app/Anime.php#L427-L428

Added lines #L427 - L428 were not covered by tests
}

if (!preg_match('~(.*) at (.*) \(~', $broadcast, $matches)) {
return [
'day' => null,
Expand Down
10 changes: 6 additions & 4 deletions app/Features/QueryRandomAnimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Contracts\RequestHandler;
use App\Dto\QueryRandomAnimeCommand;
use App\Http\Resources\V4\AnimeResource;
use Spatie\LaravelData\Optional;

/**
* @implements RequestHandler<QueryRandomAnimeCommand, AnimeResource>
Expand All @@ -18,12 +19,13 @@
public function handle($request): AnimeResource
{
$queryable = Anime::query();
// apply sfw, kids and unapproved filters
/** @noinspection PhpUndefinedMethodInspection */
$queryable = $queryable->filter(collect($request->all()));

$o = Optional::create();
$sfwParam = $request->sfw === $o ? false : $request->sfw;
$unapprovedParam = $request->unapproved === $o ? false : $request->unapproved;

Check warning on line 25 in app/Features/QueryRandomAnimeHandler.php

View check run for this annotation

Codecov / codecov/patch

app/Features/QueryRandomAnimeHandler.php#L23-L25

Added lines #L23 - L25 were not covered by tests

return new AnimeResource(
$queryable->random()->first()
$queryable->random(1, $sfwParam, $unapprovedParam)->first()

Check warning on line 28 in app/Features/QueryRandomAnimeHandler.php

View check run for this annotation

Codecov / codecov/patch

app/Features/QueryRandomAnimeHandler.php#L28

Added line #L28 was not covered by tests
);
}

Expand Down
10 changes: 6 additions & 4 deletions app/Features/QueryRandomMangaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Dto\QueryRandomMangaCommand;
use App\Http\Resources\V4\MangaResource;
use App\Manga;
use Spatie\LaravelData\Optional;

/**
* @implements RequestHandler<QueryRandomMangaCommand, MangaResource>
Expand All @@ -18,12 +19,13 @@
public function handle($request)
{
$queryable = Manga::query();
// apply sfw, kids and unapproved filters
/** @noinspection PhpUndefinedMethodInspection */
$queryable = $queryable->filter(collect($request->all()));

$o = Optional::create();
$sfwParam = $request->sfw === $o ? false : $request->sfw;
$unapprovedParam = $request->unapproved === $o ? false : $request->unapproved;

Check warning on line 25 in app/Features/QueryRandomMangaHandler.php

View check run for this annotation

Codecov / codecov/patch

app/Features/QueryRandomMangaHandler.php#L23-L25

Added lines #L23 - L25 were not covered by tests

return new MangaResource(
$queryable->random()->first()
$queryable->random(1, $sfwParam, $unapprovedParam)->first()

Check warning on line 28 in app/Features/QueryRandomMangaHandler.php

View check run for this annotation

Codecov / codecov/patch

app/Features/QueryRandomMangaHandler.php#L28

Added line #L28 was not covered by tests
);
}

Expand Down
45 changes: 41 additions & 4 deletions app/JikanApiModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App;

use App\Enums\AnimeRatingEnum;
use App\Enums\MangaTypeEnum;
use App\Filters\FilterQueryString;
use Illuminate\Support\Collection;
use Jenssegers\Mongodb\Eloquent\Builder;
use Jikan\Helper\Constants;

class JikanApiModel extends \Jenssegers\Mongodb\Eloquent\Model
{
Expand All @@ -19,10 +22,44 @@
protected array $filters = [];

/** @noinspection PhpUnused */
public function scopeRandom(Builder $query, int $numberOfRandomItems = 1): Collection
public function scopeRandom(Builder $query, int $numberOfRandomItems = 1, bool $sfw = false, bool $unapproved = false): Collection

Check warning on line 25 in app/JikanApiModel.php

View check run for this annotation

Codecov / codecov/patch

app/JikanApiModel.php#L25

Added line #L25 was not covered by tests
{
return $query->raw(fn(\Jenssegers\Mongodb\Collection $collection) => $collection->aggregate([
['$sample' => ['size' => $numberOfRandomItems]]
]));
return $query->raw(function(\Jenssegers\Mongodb\Collection $collection) use ($numberOfRandomItems, $sfw, $unapproved) {
$sfwFilter = [
'demographics.mal_id' => [
'$nin' => [
Constants::GENRE_ANIME_HENTAI,
Constants::GENRE_ANIME_EROTICA,
Constants::GENRE_MANGA_HENTAI,
Constants::GENRE_MANGA_EROTICA

Check warning on line 34 in app/JikanApiModel.php

View check run for this annotation

Codecov / codecov/patch

app/JikanApiModel.php#L27-L34

Added lines #L27 - L34 were not covered by tests
]
],
'rating' => ['$ne' => AnimeRatingEnum::rx()->label],
'type' => ['$ne' => MangaTypeEnum::doujin()->label],
'genres.mal_id' => ['$nin' => [
Constants::GENRE_ANIME_HENTAI,
Constants::GENRE_MANGA_HENTAI
]]
];

Check warning on line 43 in app/JikanApiModel.php

View check run for this annotation

Codecov / codecov/patch

app/JikanApiModel.php#L36-L43

Added lines #L36 - L43 were not covered by tests

$pipelineParams = [
['$sample' => ['size' => $numberOfRandomItems]]
];

Check warning on line 47 in app/JikanApiModel.php

View check run for this annotation

Codecov / codecov/patch

app/JikanApiModel.php#L45-L47

Added lines #L45 - L47 were not covered by tests

if ($sfw && $unapproved) {
array_unshift($pipelineParams, [
'$match' => [
...$sfwFilter,
'approved' => false

Check warning on line 53 in app/JikanApiModel.php

View check run for this annotation

Codecov / codecov/patch

app/JikanApiModel.php#L49-L53

Added lines #L49 - L53 were not covered by tests
]
]);
} else if ($sfw) {
array_unshift($pipelineParams, ['$match' => $sfwFilter]);
} else if ($unapproved) {
array_unshift($pipelineParams, ['$match' => ['approved' => false]]);

Check warning on line 59 in app/JikanApiModel.php

View check run for this annotation

Codecov / codecov/patch

app/JikanApiModel.php#L55-L59

Added lines #L55 - L59 were not covered by tests
}

return $collection->aggregate($pipelineParams);
});

Check warning on line 63 in app/JikanApiModel.php

View check run for this annotation

Codecov / codecov/patch

app/JikanApiModel.php#L62-L63

Added lines #L62 - L63 were not covered by tests
}
}