From 128cb7db344831626f2dd1b2a3e5badcf95d6cfe Mon Sep 17 00:00:00 2001 From: kiritokatklian Date: Thu, 29 Apr 2021 01:13:32 +0200 Subject: [PATCH] [Update] TV Rating Weights - Added new unique weights column to tv ratings - Added weight field to Anime resource in Nova - Updated Anime model with the weight field - Updated select-preferred-tv-rating-form to make use of the weight instead of the id - Updated tv_rating scope to make use of the weight instead of the id --- app/Models/Anime.php | 7 ++++--- app/Models/TvRating.php | 1 + app/Nova/TvRating.php | 6 ++++++ config/app.php | 2 +- database/factories/TvRatingFactory.php | 3 ++- .../2018_08_17_103103_create_tv_ratings_table.php | 1 + database/seeders/DatabaseSeeder.php | 2 +- database/seeders/TvRatingSeeder.php | 10 ++++++---- .../profile/select-preferred-tv-rating-form.blade.php | 2 +- 9 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/Models/Anime.php b/app/Models/Anime.php index ae6c36b03..4a0059687 100644 --- a/app/Models/Anime.php +++ b/app/Models/Anime.php @@ -89,10 +89,11 @@ protected static function boot() static::addGlobalScope('tv_rating', function (Builder $builder) { if (Auth::user() != null) { - $tvRating = settings('tv_rating'); + $preferredTvRating = settings('tv_rating'); + $tvRating = TvRating::firstWhere('weight', $preferredTvRating); - if ($tvRating != -1) { - $builder->where('tv_rating_id', '<=', $tvRating); + if (!empty($tvRating)) { + $builder->where('tv_rating_id', '<=', $tvRating->id); } } }); diff --git a/app/Models/TvRating.php b/app/Models/TvRating.php index 72fee858a..40fd10156 100644 --- a/app/Models/TvRating.php +++ b/app/Models/TvRating.php @@ -32,6 +32,7 @@ class TvRating extends Model protected $fillable = [ 'rating', 'description', + 'weight', ]; /** diff --git a/app/Nova/TvRating.php b/app/Nova/TvRating.php index ff5e84db5..0349a2736 100644 --- a/app/Nova/TvRating.php +++ b/app/Nova/TvRating.php @@ -5,6 +5,7 @@ use Illuminate\Http\Request; use Laravel\Nova\Fields\HasMany; use Laravel\Nova\Fields\ID; +use Laravel\Nova\Fields\Number; use Laravel\Nova\Fields\Text; class TvRating extends Resource @@ -58,6 +59,11 @@ public function fields(Request $request) ->help('A very short description of the rating. E.g. Not Rated, All Ages, Children...') ->required(), + Number::make('Weight') + ->help('The priority of the rating. E.g: if a TV rating with a weight of 5 is selected, all ratings that are less than, and equal to, 5 are accessible to the user.') + ->rules(['min:0', 'max:255']) + ->required(), + HasMany::make('Anime'), ]; } diff --git a/config/app.php b/config/app.php index 1649d024f..1b06352b3 100644 --- a/config/app.php +++ b/config/app.php @@ -38,7 +38,7 @@ | or any other location as required by the application or its packages. */ - 'version' => '1.2.0-alpha.31', + 'version' => '1.2.0-alpha.32', /* |-------------------------------------------------------------------------- diff --git a/database/factories/TvRatingFactory.php b/database/factories/TvRatingFactory.php index 8e8da4c1a..1b89c6cb1 100644 --- a/database/factories/TvRatingFactory.php +++ b/database/factories/TvRatingFactory.php @@ -23,7 +23,8 @@ public function definition() { return [ 'name' => $this->faker->randomLetter, - 'description' => $this->faker->words(3, true) + 'description' => $this->faker->words(3, true), + 'weight' => $this->faker->unique()->numberBetween(1, 10) ]; } } diff --git a/database/migrations/2018_08_17_103103_create_tv_ratings_table.php b/database/migrations/2018_08_17_103103_create_tv_ratings_table.php index 4cdcdd9ca..a4055dc5b 100644 --- a/database/migrations/2018_08_17_103103_create_tv_ratings_table.php +++ b/database/migrations/2018_08_17_103103_create_tv_ratings_table.php @@ -18,6 +18,7 @@ public function up() $table->bigIncrements('id'); $table->string('name'); $table->string('description'); + $table->unsignedTinyInteger('weight'); $table->timestamps(); }); } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 17934f8a8..27112fa50 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -17,6 +17,7 @@ public function run() RoleSeeder::class, StudioSeeder::class, TvRatingSeeder::class, + GenreSeeder::class, AnimeDummySeeder::class, AnimeRelationsSeeder::class, UserSeeder::class, @@ -24,7 +25,6 @@ public function run() BadgeSeeder::class, ForumThreadSeeder::class, ForumReplySeeder::class, - GenreSeeder::class, AppThemeSeeder::class, ActorCharacterAnimeSeeder::class, ]); diff --git a/database/seeders/TvRatingSeeder.php b/database/seeders/TvRatingSeeder.php index 020d61503..dc112119a 100644 --- a/database/seeders/TvRatingSeeder.php +++ b/database/seeders/TvRatingSeeder.php @@ -16,22 +16,27 @@ class TvRatingSeeder extends Seeder [ 'name' => 'NR', 'description' => 'Not Rated', + 'weight' => 1, ], [ 'name' => 'G', 'description' => 'All Ages', + 'weight' => 2, ], [ 'name' => 'PG-12', 'description' => 'Parental Guidance Suggested', + 'weight' => 3, ], [ 'name' => 'R15+', 'description' => 'Violence & Profanity', + 'weight' => 4, ], [ 'name' => 'R18+', 'description' => 'Adults Only', + 'weight' => 5, ] ]; @@ -43,10 +48,7 @@ class TvRatingSeeder extends Seeder public function run() { foreach ($this->tvRatings as $tvRating) { - TvRating::create([ - 'name' => $tvRating['name'], - 'description' => $tvRating['description'], - ]); + TvRating::create($tvRating); } } } diff --git a/resources/views/livewire/profile/select-preferred-tv-rating-form.blade.php b/resources/views/livewire/profile/select-preferred-tv-rating-form.blade.php index 425f2a8a9..f036283d1 100644 --- a/resources/views/livewire/profile/select-preferred-tv-rating-form.blade.php +++ b/resources/views/livewire/profile/select-preferred-tv-rating-form.blade.php @@ -17,7 +17,7 @@ @foreach (App\Models\TvRating::all()->where('id', '!=', 1) as $tvRating) - + @endforeach