Skip to content

Commit

Permalink
Merge pull request #119 from Kurozora/tv-rating-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kiritokatklian authored Apr 23, 2021
2 parents bf9ab7e + 72753a5 commit dd94128
Show file tree
Hide file tree
Showing 36 changed files with 772 additions and 326 deletions.
36 changes: 36 additions & 0 deletions app/Actions/Web/Profile/UpdateUserPreferredTvRating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Actions\Web\Profile;

use App\Contracts\Web\Profile\UpdatesUserPreferredTvRating;
use App\Models\TvRating;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;

class UpdateUserPreferredTvRating implements UpdatesUserPreferredTvRating
{
/**
* Validate and update the given user's profile information.
*
* @param User $user
* @param array $input
*
* @return void
* @throws ValidationException
*/
public function update(User $user, array $input)
{
$rules = ['required', 'integer', 'not_in:0,1'];

if ($input['tv_rating'] != -1) {
array_push($rules, 'exists:' . TvRating::TABLE_NAME . ',id');
}

Validator::make($input, [
'tv_rating' => $rules,
])->validateWithBag('updatePreferredTvRating');

settings('tv_rating', (integer) $input['tv_rating']);
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/FetchAnimeDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function handle()
//
// // Watch rating
// $this->info('[Retrieving watch rating]');
// $anime->watch_rating = $details->watchRating;
// $anime->tv_rating = $details->watchRating;
// $this->info('[Watch rating retrieved]');
// $this->info('');
//
Expand Down
17 changes: 17 additions & 0 deletions app/Contracts/Web/Profile/UpdatesUserPreferredTvRating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Contracts\Web\Profile;

use App\Models\User;

interface UpdatesUserPreferredTvRating
{
/**
* Validate and update the given user's preferred TV rating.
*
* @param User $user
* @param array $input
* @return void
*/
public function update(User $user, array $input);
}
54 changes: 0 additions & 54 deletions app/Enums/WatchRating.php

This file was deleted.

56 changes: 56 additions & 0 deletions app/Http/Livewire/Profile/SelectPreferredTvRatingForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Http\Livewire\Profile;

use App\Contracts\Web\Profile\UpdatesUserPreferredTvRating;
use Auth;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\View\View;
use Livewire\Component;

class SelectPreferredTvRatingForm extends Component
{
/**
* The component's state.
*
* @var array
*/
public array $state = [];

/**
* Prepare the component.
*
* @return void
*/
public function mount()
{
$this->state = [
'tv_rating' => settings('tv_rating')
];
}

/**
* Update the user's preferred TV rating.
*
* @param UpdatesUserPreferredTvRating $updater
*/
public function updatePreferredTvRating(UpdatesUserPreferredTvRating $updater)
{
$this->resetErrorBag();

$updater->update(Auth::user(), $this->state);

$this->emit('saved');
}

/**
* Render the component.
*
* @return Application|Factory|View
*/
public function render(): Application|Factory|View
{
return view('livewire.profile.select-preferred-tv-rating-form');
}
}
1 change: 0 additions & 1 deletion app/Http/Livewire/Profile/UpdateProfileInformationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Livewire\Profile;

use App\Contracts\UpdatesUserProfileInformation;
use App\Models\User;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Resources/AnimeResourceBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\Enums\AnimeType;
use App\Enums\DayOfWeek;
use App\Enums\UserLibraryStatus;
use App\Enums\WatchRating;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
Expand Down Expand Up @@ -49,7 +48,10 @@ public function toArray($request): array
'tagline' => $anime->tagline,
'synopsis' => $anime->synopsis,
'type' => AnimeType::getDescription($anime->type),
'watchRating' => WatchRating::getDescription($anime->watch_rating),
'tvRating' => [
'name' => $anime->tv_rating->name,
'description' => $anime->tv_rating->description,
],
'adaptationSource' => AnimeSource::getDescription($anime->adaptation_source),
'network' => $anime->network,
'producer' => $anime->producer,
Expand Down
32 changes: 32 additions & 0 deletions app/Models/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use App\Enums\AnimeImageType;
use App\Traits\KuroSearchTrait;
use Auth;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
Expand Down Expand Up @@ -69,6 +71,26 @@ class Anime extends KModel
const TABLE_NAME = 'animes';
protected $table = self::TABLE_NAME;

/**
* Bootstrap the model and its traits.
*
* @return void
*/
protected static function boot()
{
parent::boot();

static::addGlobalScope('tv_rating', function (Builder $builder) {
if (Auth::user() != null) {
$tvRating = settings('tv_rating');

if ($tvRating != -1) {
$builder->where('tv_rating_id', '<=', $tvRating);
}
}
});
}

/**
* Get the options for generating the slug.
*/
Expand Down Expand Up @@ -412,4 +434,14 @@ public function scopeMostPopular(Builder $query, int $limit = 10)

return $query->whereIn(self::TABLE_NAME . '.id', $mostAddedIDs);
}

/**
* The anime's TV rating.
*
* @return BelongsTo
*/
public function tv_rating(): BelongsTo
{
return $this->belongsTo(TvRating::class);
}
}
65 changes: 65 additions & 0 deletions app/Models/TvRating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class TvRating extends Model
{
use HasFactory;

/**
* The model's table name.
*
* @var string
*/
const TABLE_NAME = 'tv_ratings';

/**
* The model's table name.
*
* @var string
*/
protected $table = self::TABLE_NAME;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'rating',
'description',
];

/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'full_name'
];

/**
* The full name of the rating which combines the name and description attributes.
*
* @return string
*/
public function getFullNameAttribute(): string
{
return $this->name . ' - ' . $this->description;
}

/**
* The anime belonging to the TV rating.
*
* @return HasMany
*/
public function anime(): HasMany
{
return $this->hasMany(Anime::class);
}
}
24 changes: 17 additions & 7 deletions app/Nova/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Enums\AnimeStatus;
use App\Enums\AnimeType;
use App\Enums\DayOfWeek;
use App\Enums\WatchRating;
use App\Nova\Actions\FetchAnimeActors;
use App\Nova\Actions\FetchAnimeDetails;
use App\Nova\Actions\FetchAnimeImages;
Expand All @@ -15,6 +14,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laraning\NovaTimeField\TimeField as Time;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\Date;
Expand All @@ -25,6 +25,7 @@
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;

class Anime extends Resource
{
Expand All @@ -40,7 +41,7 @@ class Anime extends Resource
*
* @var string
*/
public static $title = 'id';
public static $title = 'title';

/**
* The columns that should be searched.
Expand Down Expand Up @@ -122,12 +123,9 @@ public function fields(Request $request)
->required()
->help('The general type of the anime, such as TV, Movie, or Music.'),

Select::make('Watch rating')
->options(WatchRating::asSelectArray())
->displayUsingLabels()
BelongsTo::make('TV rating', 'tv_rating')
->sortable()
->required()
->help('Use `TV-Y7 (FV)` if the show exhibits more \'fantasy violence\', and/or is generally more intense or combative than other shows.'),
->required(),

Select::make('Adaptation Source')
->options(AnimeSource::asSelectArray())
Expand Down Expand Up @@ -342,6 +340,18 @@ private function displayModIndicatorForIndex()
return '<span class="py-1 px-2 mr-1 inline-block rounded align-middle" style="background-color: #465161; color: #fff;">' . $modCount . ' ' . Str::plural('mod', $modCount) . '</span>';
}

/**
* Build an "index" query for the given resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function indexQuery(NovaRequest $request, $query)
{
return parent::indexQuery($request, $query)->withoutGlobalScope('tv_rating');
}

/**
* The icon of the resource.
*
Expand Down
Loading

0 comments on commit dd94128

Please sign in to comment.