Skip to content

Commit 2829003

Browse files
committed
refactor: save request last bounty timestamp separately
created_at is the wrong field to save this value in because the initial created_at date is lost. Using updated_at also doesn't work since updating the request fields will bump the request even if more bounty isn't added. So, we use a new field for this info. We use a field on the requests table and not the request_bounty table so that we can use an index to sort.
1 parent 9951eaa commit 2829003

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

app/Http/Controllers/BountyController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function store(StoreTorrentRequestBountyRequest $request, TorrentRequest
5050

5151
$torrentRequest->increment('bounty', $request->integer('seedbonus'));
5252

53-
$torrentRequest->created_at = now();
53+
$torrentRequest->last_bountied_at = now();
5454
$torrentRequest->save();
5555

5656
if ($request->boolean('anon') == 0) {

app/Http/Livewire/TorrentRequestSearch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class TorrentRequestSearch extends Component
113113
public int $perPage = 25;
114114

115115
#[Url(history: true)]
116-
public string $sortField = 'created_at';
116+
public string $sortField = 'last_bountied_at';
117117

118118
#[Url(history: true)]
119119
public string $sortDirection = 'desc';

app/Models/TorrentRequest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @property bool $anon
3939
* @property \Illuminate\Support\Carbon|null $created_at
4040
* @property \Illuminate\Support\Carbon|null $updated_at
41+
* @property \Illuminate\Support\Carbon|null $last_bountied_at
4142
* @property int|null $filled_by
4243
* @property int|null $torrent_id
4344
* @property \Illuminate\Support\Carbon|null $filled_when
@@ -86,13 +87,14 @@ class TorrentRequest extends Model
8687
protected function casts(): array
8788
{
8889
return [
89-
'filled_when' => 'datetime',
90-
'approved_when' => 'datetime',
91-
'tmdb_movie_id' => 'int',
92-
'tmdb_tv_id' => 'int',
93-
'igdb' => 'int',
94-
'bounty' => 'decimal:2',
95-
'anon' => 'bool',
90+
'filled_when' => 'datetime',
91+
'approved_when' => 'datetime',
92+
'last_bountied_at' => 'datetime',
93+
'tmdb_movie_id' => 'int',
94+
'tmdb_tv_id' => 'int',
95+
'igdb' => 'int',
96+
'bounty' => 'decimal:2',
97+
'anon' => 'bool',
9698
];
9799
}
98100

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* NOTICE OF LICENSE.
4+
*
5+
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
6+
* The details is bundled with this project in the file LICENSE.txt.
7+
*
8+
* @project UNIT3D Community Edition
9+
*
10+
* @author Roardom <[email protected]>
11+
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
12+
*/
13+
14+
use Illuminate\Database\Migrations\Migration;
15+
use Illuminate\Database\Schema\Blueprint;
16+
use Illuminate\Support\Facades\DB;
17+
use Illuminate\Support\Facades\Schema;
18+
19+
return new class () extends Migration {
20+
/**
21+
* Run the migrations.
22+
*/
23+
public function up(): void
24+
{
25+
Schema::table('requests', function (Blueprint $table): void {
26+
$table->timestamp('last_bountied_at')->after('updated_at');
27+
});
28+
29+
DB::table('requests')
30+
->join('request_bounty', 'requests_id', '=', 'requests.id')
31+
->groupBy('requests.id')
32+
->update([
33+
'requests.created_at' => DB::raw('MIN(request_bounty.created_at)'),
34+
'requests.last_bountied_at' => DB::raw('MAX(request_bounty.created_at)'),
35+
]);
36+
}
37+
};

resources/views/livewire/torrent-request-search.blade.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ class="form__button form__button--text"
336336
<i class="{{ config('other.font-awesome') }} fa-coins"></i>
337337
@include('livewire.includes._sort-icon', ['field' => 'bounty'])
338338
</th>
339-
<th wire:click="sortBy('created_at')" role="columnheader button">
339+
<th wire:click="sortBy('last_bountied_at')" role="columnheader button">
340340
{{ __('common.created_at') }}
341-
@include('livewire.includes._sort-icon', ['field' => 'created_at'])
341+
@include('livewire.includes._sort-icon', ['field' => 'last_bountied_at'])
342342
</th>
343343
<th>{{ __('common.status') }}</th>
344344
</tr>
@@ -367,10 +367,10 @@ class="form__button form__button--text"
367367
<td>{{ number_format($torrentRequest->bounty) }}</td>
368368
<td>
369369
<time
370-
datetime="{{ $torrentRequest->created_at }}"
371-
title="{{ $torrentRequest->created_at }}"
370+
datetime="{{ $torrentRequest->last_bountied_at }}"
371+
title="{{ $torrentRequest->last_bountied_at }}"
372372
>
373-
{{ $torrentRequest->created_at->diffForHumans() }}
373+
{{ $torrentRequest->last_bountied_at->diffForHumans() }}
374374
</time>
375375
</td>
376376
<td>

0 commit comments

Comments
 (0)