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

Single media page #33

Merged
merged 17 commits into from
Dec 30, 2024
22 changes: 0 additions & 22 deletions app/Livewire/Media/Backlog.php

This file was deleted.

56 changes: 0 additions & 56 deletions app/Livewire/Media/Logbook.php

This file was deleted.

61 changes: 61 additions & 0 deletions app/Livewire/Media/MediaPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Livewire\Media;

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

class MediaPage extends Component
{
#[Url(except: 'finished')]
public string $list = 'finished';

#[Url(except: '')]
public string $year = '';

#[Url(except: '')]
public string $type = '';

private function mediaTypes(): array
{
return MediaTypeName::cases();
}

private function years(): array
{
return (new LogbookQuery)->years();
}

private function getYear(): ?int
{
return $this->year ? (int) $this->year : null;
}

private function getType(): ?MediaTypeName
{
return $this->type ? MediaTypeName::from($this->type) : null;
}

private function query(): Collection
{
return match ($this->list) {
'backlog' => (new BacklogQuery($this->getYear(), $this->getType()))->execute(),
'in-progress' => (new InProgressQuery)->execute(),
default => (new LogbookQuery($this->getYear(), $this->getType()))->execute(),
};
}

public function render()
{
return view('livewire.media.media-page', [
'items' => $this->query(),
'years' => $this->years(),
'mediaTypes' => $this->mediaTypes(),
]);
}
}
5 changes: 1 addition & 4 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -21,8 +20,6 @@ class AuthServiceProvider extends ServiceProvider
*/
public function boot(): void
{
Gate::define('view-backlog', function ($user) {
return $user->is_admin;
});
//
}
}
29 changes: 18 additions & 11 deletions app/Queries/Media/BacklogQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

namespace App\Queries\Media;

use App\Enum\MediaTypeName;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

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

public function execute(): Collection
{
return DB::table('media')
$query = DB::table('media')
->join('media_types', 'media.media_type_id', '=', 'media_types.id')
->leftJoin('creators', 'media.creator_id', '=', 'creators.id')
->leftJoin('media_events', 'media_events.media_id', '=', 'media.id')
Expand All @@ -26,13 +24,22 @@ public function execute(): Collection
'media.title as title',
'creators.name as creator',
'media_types.name as type',
'media.created_at as added_at',
'media.created_at as occurred_at',
'media.note as note'
)

->whereNull('media_events.id')

->orderBy('media.created_at', 'desc')
->get();
->orderBy('media.created_at', 'desc');

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

if ($this->type !== null) {
$query->where('media_types.name', $this->type);
}

return $query->get();
}
}
42 changes: 42 additions & 0 deletions app/Queries/Media/InProgressQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Queries\Media;

use App\Enum\MediaEventTypeName;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class InProgressQuery
{
public function __construct() {}

public function execute(): Collection
{

$query = DB::table('media')
->join('media_types', 'media.media_type_id', '=', 'media_types.id')
->join('media_events as started_events', 'media.id', '=', 'started_events.media_id')
->join('media_event_types', 'started_events.media_event_type_id', '=', 'media_event_types.id')
->join('creators', 'creators.id', '=', 'media.creator_id')
->where('media_event_types.name', MediaEventTypeName::STARTED);

$query->whereNotExists(function ($subQuery) {
$subQuery->select(DB::raw(1))
->from('media_events as done_events')
->join('media_event_types as second_event_types', 'done_events.media_event_type_id', '=', 'second_event_types.id')
->whereRaw('done_events.media_id = media.id')
->whereColumn('done_events.media_id', 'media.id')
->whereIn('second_event_types.name', [MediaEventTypeName::FINISHED, MediaEventTypeName::ABANDONED]);
});

$query->select(
'media.title as title',
'creators.name as creator',
'media_types.name as type',
'started_events.occurred_at as occurred_at',
'media.note as note'
);

return $query->get();
}
}
6 changes: 3 additions & 3 deletions app/Queries/Media/LogbookQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace App\Queries\Media;

use App\Enum\MediaEventTypeName;
use App\Enum\MediaTypeName;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class LogbookQuery
{
public function __construct(
public ?int $year = null,
public ?MediaEventTypeName $type = null
public ?MediaTypeName $type = null
) {}

/**
Expand All @@ -28,8 +29,7 @@ public function execute(): Collection
'media.title as title',
'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_events.occurred_at as occurred_at',
'media.note as note'
)

Expand Down
13 changes: 13 additions & 0 deletions app/Queries/Media/YearsQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Queries\Media;

class YearsQuery
{
public function __construct() {}

public function execute()
{
// TODO: descending order, disctinct years for all events and media created_at
}
}
39 changes: 0 additions & 39 deletions app/View/Components/Media/BacklogItem.php

This file was deleted.

8 changes: 7 additions & 1 deletion app/View/Components/Media/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\View\Components\Media;

use App\Enum\MediaTypeName;
use Carbon\Carbon;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
Expand All @@ -14,7 +15,7 @@ public function __construct(public readonly stdClass $item) {}

public function icon(): string
{
return match ($this->item->type) {
return match (MediaTypeName::from($this->item->type)) {
MediaTypeName::Book => '📕',
MediaTypeName::Movie => '🍿',
MediaTypeName::Album => '📀',
Expand All @@ -24,6 +25,11 @@ public function icon(): string
};
}

public function getDate(): string
{
return Carbon::parse($this->item->occurred_at)->format('Y F d');
}

/**
* Get the view / contents that represent the component.
*/
Expand Down
Loading
Loading