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

Add year filter #31

Merged
merged 2 commits into from
Dec 23, 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
34 changes: 34 additions & 0 deletions app/Enum/MediaTypeName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Enum;

enum MediaTypeName: string
{
case Album = 'album';
case Book = 'book';
case Movie = 'movie';
case TvShow = 'tv show';
case VideoGame = 'video game';

/**
* Get the display value for the enum case.
*/
public function displayName(): string
{
return match ($this) {
self::Album => 'Album',
self::Book => 'Book',
self::Movie => 'Movie',
self::TvShow => 'TV Show',
self::VideoGame => 'Video Game',
};
}

/**
* @return string[]
*/
public static function displayNames(): array
{
return array_map(fn ($case) => $case->displayName(), self::cases());
}
}
40 changes: 37 additions & 3 deletions app/Livewire/Media/Logbook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,53 @@

namespace App\Livewire\Media;

use App\Enum\MediaTypeName;
use App\Queries\Media\LogbookQuery;
use Illuminate\Support\Collection;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
use Livewire\Component;

class Logbook extends Component
{
public Collection $items;
#[Url(except: '')]
public string $year = '';

public function mount()
/**
* @return int[]
*/
#[Computed]
public function years(): array
{
$this->items = (new LogbookQuery)->execute();
return (new LogbookQuery)->years();
}

/**
* @returns {
* 'year': ?int,
* 'type': ?MediaTypeName
* }
*/
private function getQueryFilters(): array
{

return [
'year' => empty($this->year) ? null : (int) $this->year,
];
}

#[Computed]
public function items(): Collection
{
$filters = $this->getQueryFilters();

return (new LogbookQuery(
$filters['year'],
))->execute();
}

public function mount() {}

public function render()
{
return view('livewire.media.logbook');
Expand Down
5 changes: 5 additions & 0 deletions app/Models/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Enum\MediaTypeName;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand All @@ -12,6 +13,10 @@ class MediaType extends Model

protected $fillable = ['name'];

protected $casts = [
'name' => MediaTypeName::class,
];

public function media(): HasMany
{
return $this->hasMany(Media::class);
Expand Down
41 changes: 32 additions & 9 deletions app/Queries/Media/LogbookQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@

class LogbookQuery
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
public function __construct(
public ?int $year = null,
public ?MediaEventTypeName $type = null
) {}

/**
* Get the logbook items.
*/
public function execute(): Collection
{
return DB::table('media_events')
$query = DB::table('media_events')
->join('media_event_types', 'media_events.media_event_type_id', '=', 'media_event_types.id')
->join('media', 'media_events.media_id', '=', 'media.id')
->join('media_types', 'media.media_type_id', '=', 'media_types.id')
Expand All @@ -32,11 +29,37 @@ public function execute(): Collection
'creators.name as creator',
'media_types.name as type',
'media_events.occurred_at as finished_at',
DB::raw('extract(year from media_events.occurred_at) as finished_at_year'),
'media.note as note'
)

->orderBy('media_events.occurred_at', 'desc')
->where('media_event_types.name', MediaEventTypeName::FINISHED);

if ($this->year !== null) {
$query->whereYear('media_events.occurred_at', $this->year);
}

if ($this->type !== null) {
// TODO: MediaType filter currently untested
$query->where('media_types.name', $this->type);
}

return $query->get();
}

/**
* @returns int[]
*/
public function years(): array
{
return DB::table('media_event_types')
->join('media_events', 'media_events.media_event_type_id', 'media_event_types.id')
->where('media_event_types.name', MediaEventTypeName::FINISHED)
->get();
->selectRaw('distinct extract(year from media_events.occurred_at) as year')
->orderBy('year', 'desc')
->get()
->pluck('year')
->toArray();
}
}
11 changes: 6 additions & 5 deletions app/View/Components/Media/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\View\Components\Media;

use App\Enum\MediaTypeName;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
Expand All @@ -14,11 +15,11 @@ public function __construct(public readonly stdClass $item) {}
public function icon(): string
{
return match ($this->item->type) {
'book' => '📕',
'movie' => '🍿',
'album' => '📀',
'tv show' => '📺',
'video game' => '🎮',
MediaTypeName::Book => '📕',
MediaTypeName::Movie => '🍿',
MediaTypeName::Album => '📀',
MediaTypeName::TvShow => '📺',
MediaTypeName::VideoGame => '🎮',
default => '',
};
}
Expand Down
9 changes: 5 additions & 4 deletions database/factories/MediaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Enum\MediaTypeName;
use App\Models\Creator;
use App\Models\MediaType;
use Illuminate\Database\Eloquent\Factories\Factory;
Expand Down Expand Up @@ -37,28 +38,28 @@ public function definition(): array
public function book(): MediaFactory
{
return $this->state([
'media_type_id' => MediaType::where('name', 'book')->first()->id,
'media_type_id' => MediaType::where('name', MediaTypeName::Book)->first()->id,
]);
}

public function album(): MediaFactory
{
return $this->state([
'media_type_id' => MediaType::where('name', 'album')->first()->id,
'media_type_id' => MediaType::where('name', MediaTypeName::Album)->first()->id,
]);
}

public function game(): MediaFactory
{
return $this->state([
'media_type_id' => MediaType::where('name', 'video game')->first()->id,
'media_type_id' => MediaType::where('name', MediaTypeName::VideoGame)->first()->id,
]);
}

public function movie(): MediaFactory
{
return $this->state([
'media_type_id' => MediaType::where('name', 'movie')->first()->id,
'media_type_id' => MediaType::where('name', MediaTypeName::Movie)->first()->id,
]);
}
}
3 changes: 2 additions & 1 deletion database/factories/MediaTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Enum\MediaTypeName;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
Expand All @@ -17,7 +18,7 @@ class MediaTypeFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->word,
'name' => $this->faker->randomElement(MediaTypeName::cases()),
];
}
}
14 changes: 14 additions & 0 deletions resources/views/components/media/shell.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<div>
<x-type.page-title>{{ $title }}</x-type.page-title>

@if ($showFilters)
<form class="form-control flex flex-row space-x-2 m-2">
<select
wire:model.live="year"
class="select select-sm select-ghost"
>
<option value="">All Years</option>
@foreach ($this->years as $year)
<option value="{{ $year }}">{{ $year }}</option>
@endforeach
</select>
</form>
@endif

@can("view-backlog")
<ul class="tabs tabs-boxed mt-2 max-w-96" role="tablist">
<a
Expand Down
1 change: 1 addition & 0 deletions resources/views/livewire/media/backlog.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
title="Media Backlog"
:items="$items"
:itemComponent="'media.backlog-item'"
:showFilters="false"
/>
3 changes: 2 additions & 1 deletion resources/views/livewire/media/logbook.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<x-media.shell
title="Media Log"
:items="$items"
:items="$this->items"
:itemComponent="'media.logbook-item'"
:showFilters="true"
/>
14 changes: 14 additions & 0 deletions tests/Feature/Enum/MediaTypeNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use App\Enum\MediaTypeName;

test('displayNames', function () {
/** @var TestCase $this */
expect(MediaTypeName::displayNames())->toEqual([
'Album',
'Book',
'Movie',
'TV Show',
'Video Game',
]);
});
12 changes: 10 additions & 2 deletions tests/Feature/Livewire/Media/LogbookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function empty_state()
{
Livewire::test(Logbook::class)
->assertStatus(200)
->assertSeeText('No items');
->assertSeeText('No items')
->assertSet('years', []);
}

/** @test */
Expand All @@ -42,6 +43,7 @@ public function displays_logbook()
$this->createBook('The Hobbit', 'J.R.R. Tolkien', '2023-02-07');
$this->createBook('The Da Vinci Code', 'Dan Brown', '2022-06-17');
$this->createBook('The Alchemist', 'Paulo Coelho', '2021-12-25');

Livewire::test(Logbook::class)
->assertStatus(200)
->assertSeeHtmlInOrder([
Expand All @@ -58,7 +60,13 @@ public function displays_logbook()
'2021 December 25',
'The Alchemist',
'Paulo Coelho',
]);
])
->assertSet('years', [2023, 2022, 2021]);

Livewire::withQueryParams(['year' => 2022])
->test(Logbook::class)
->assertSee('The Da Vinci Code')
->assertDontSee(['The Hobbit', 'The Alchemist']);
}

/** @test */
Expand Down
12 changes: 5 additions & 7 deletions tests/Feature/Models/MediaTypeTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?php

use App\Enum\MediaTypeName;
use App\Models\MediaType;
use Tests\TestCase;

test('factory works', function () {
/** @var TestCase $this */
$mediaType = MediaType::factory()->create();
$mediaType = MediaType::factory()->make();
$this->assertNotNull($mediaType);
$this->assertNotEmpty($mediaType->name);
});

test('media type seeded with data', function () {
/** @var TestCase $this */
$mediaTypes = MediaType::all('name')->toArray();
$this->assertCount(5, $mediaTypes);
$mediaTypeNames = MediaType::orderBy('name', 'asc')->pluck('name')->toArray();
$this->assertCount(5, $mediaTypeNames);

$mediaTypeNames = array_column($mediaTypes, 'name');

$expectedNames = ['tv show', 'book', 'movie', 'album', 'video game'];
foreach ($expectedNames as $expectedName) {
foreach (MediaTypeName::cases() as $expectedName) {
expect(in_array($expectedName, $mediaTypeNames))->toBeTrue();
}
});
Loading