Skip to content

Commit

Permalink
No more album ID within the history (#2242)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Jan 25, 2024
1 parent 067ac87 commit 7ce7efd
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 61 deletions.
20 changes: 16 additions & 4 deletions app/Jobs/ProcessImageJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,32 @@ class ProcessImageJob implements ShouldQueue
*/
public function __construct(
ProcessableJobFile $file,
string|AbstractAlbum|null $albumID,
string|AbstractAlbum|null $album,
?int $fileLastModifiedTime,
) {
$this->filePath = $file->getPath();
$this->originalBaseName = $file->getOriginalBasename();
$this->albumID = is_string($albumID) ? $albumID : $albumID?->id;

$this->albumID = null;
$album_name = __('lychee.UNSORTED');

if ($album instanceof AbstractAlbum) {
$this->albumID = $album->id;
$album_name = $album->title;
}

if (is_string($album)) {
$album_name = resolve(AlbumFactory::class)->findAbstractAlbumOrFail($this->albumID)->title;
$this->albumID = $album;
}

$this->userId = Auth::user()->id;
$this->fileLastModifiedTime = $fileLastModifiedTime;

// Set up our new history record.
$this->history = new JobHistory();
$this->history->owner_id = $this->userId;
$this->history->job = Str::limit('Process Image: ' . $this->originalBaseName, 200);
$this->history->parent_id = $this->albumID;
$this->history->job = Str::limit(sprintf('Process Image: %s added to %s.', $this->originalBaseName, $album_name), 200);
$this->history->status = JobStatus::READY;

$this->history->save();
Expand Down
33 changes: 0 additions & 33 deletions app/Models/Builders/JobHistoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace App\Models\Builders;

use App\Eloquent\FixedQueryBuilder;
use App\Exceptions\Internal\QueryBuilderException;
use App\Models\JobHistory;
use Illuminate\Support\Facades\DB;

/**
* Specialized query builder for {@link \App\Models\JobHistory}.
Expand All @@ -14,34 +11,4 @@
*/
class JobHistoryBuilder extends FixedQueryBuilder
{
/**
* Get the hydrated models without eager loading.
*
* @param array<string>|string $columns
*
* @return JobHistory[]
*
* @throws QueryBuilderException
*/
public function getModels($columns = ['*']): array
{
$baseQuery = $this->getQuery();

if ($baseQuery->columns === null || count($baseQuery->columns) === 0) {
$this->select([$baseQuery->from . '.*']);
}

if (
($columns === ['*'] || $columns === ['jobs_history.*']) &&
($baseQuery->columns === ['*'] || $baseQuery->columns === ['jobs_history.*'])
) {
$title = DB::table('base_albums', 'ba')
->selectRaw('ba.title')
->whereColumn('ba.id', '=', 'jobs_history.parent_id');

$this->addSelect(['title' => $title]);
}

return parent::getModels($columns);
}
}
29 changes: 7 additions & 22 deletions app/Models/JobHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property int $id
* @property int $owner_id
* @property User $owner
* @property string $job
* @property string|null $parent_id
* @property Album|null $parent
* @property JobStatus $status
* @property ?string $title
* @property Carbon $created_at
* @property Carbon $updated_at
* @property int $id
* @property int $owner_id
* @property User $owner
* @property string $job
* @property JobStatus $status
* @property Carbon $created_at
* @property Carbon $updated_at
*
* @method static JobHistoryBuilder|JobHistory addSelect($column)
* @method static JobHistoryBuilder|JobHistory join(string $table, string $first, string $operator = null, string $second = null, string $type = 'inner', string $where = false)
Expand All @@ -35,10 +32,8 @@
* @method static JobHistoryBuilder|JobHistory whereJob($value)
* @method static JobHistoryBuilder|JobHistory whereNotIn(string $column, string $values, string $boolean = 'and')
* @method static JobHistoryBuilder|JobHistory whereOwnerId($value)
* @method static JobHistoryBuilder|JobHistory whereParentId($value)
* @method static JobHistoryBuilder|JobHistory whereStatus($value)
* @method static JobHistoryBuilder|JobHistory whereUpdatedAt($value)
* @method static JobHistoryBuilder|JobHistory withAlbumTitleOrNull()
*
* @mixin \Eloquent
*/
Expand Down Expand Up @@ -84,14 +79,4 @@ public function owner(): BelongsTo
{
return $this->belongsTo(User::class, 'owner_id', 'id');
}

/**
* Returns the relationship between an Job and its associated album.
*
* @return BelongsTo
*/
public function parent(): BelongsTo
{
return $this->belongsTo(BaseAlbumImpl::class, 'parent_id', 'id');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public const TABLE = 'jobs_history';
public const COLUMN = 'parent_id';
public const RANDOM_ID_LENGTH = 24;

/**
* Run the migrations.
*/
public function up(): void
{
Schema::disableForeignKeyConstraints();
Schema::table(self::TABLE, function (Blueprint $table) {
$table->dropColumn(self::COLUMN);
});
Schema::enableForeignKeyConstraints();
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table(self::TABLE, function (Blueprint $table) {
$table->char(self::COLUMN, self::RANDOM_ID_LENGTH)->nullable(true); // parentId = album ID
});
}
};
2 changes: 1 addition & 1 deletion resources/views/jobs.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<body>
<pre>
@forelse($jobs as $job)
{{ $job->created_at }} -- {{ str_pad($job->status->name(), 7) }} -- {{ $job->owner->name }} -- {{ $job->job }} -- {{ $job->title ?? __('lychee.UNSORTED') }}
{{ $job->created_at }} -- {{ str_pad($job->status->name(), 7) }} -- {{ $job->owner->name }} -- {{ $job->job }}
@empty
No Jobs have been executed yet.
@endforelse
Expand Down
1 change: 0 additions & 1 deletion resources/views/livewire/pages/jobs.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<span class="mx-2 text-red-700"><pre class="inline">{{ str_pad($job->status->name(), 7) }}</pre></span>
@endif
<span class="mx-2">{{ $job->owner->name }}</span>
<span class="mx-2">{{ $job->title ?? __('lychee.UNSORTED') }}</span>
<span class="mx-2">{{ $job->job }}</span>
<br>
@empty
Expand Down

0 comments on commit 7ce7efd

Please sign in to comment.