refactor: graveyard resurrections

Use route model binding. Move methods in controller available to all users to user-specific controller. Clean up controller code.
This commit is contained in:
Roardom
2023-07-01 06:00:06 +00:00
parent e0e0ba0dae
commit f4cc89095f
7 changed files with 68 additions and 117 deletions
@@ -14,6 +14,9 @@
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Models\Graveyard;
use App\Models\History;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Http\Request;
@@ -28,4 +31,60 @@ class ResurrectionController extends Controller
return view('user.resurrection.index', ['user' => $user]);
}
/**
* Resurrect A Torrent.
*/
public function store(Request $request, User $user): \Illuminate\Http\RedirectResponse
{
abort_unless($request->user()->is($user), 403);
$torrent = Torrent::findOrFail($request->integer('torrent_id'));
if ($user->id === $torrent->user_id) {
return to_route('torrents.show', ['id' => $torrent->id])
->withErrors(trans('graveyard.resurrect-failed-own'));
}
if ($torrent->seeders !== 0) {
return to_route('torrents.show', ['id' => $torrent->id])
->withErrors('This torrent is not dead.');
}
if ($torrent->created_at->gt(now()->subDays(30))) {
return to_route('torrents.show', ['id' => $torrent->id])
->withErrors('This torrent is not older than 30 days.');
}
$isPending = Graveyard::whereBelongsTo($torrent)->where('rewarded', '=', 0)->exists();
if ($isPending) {
return to_route('torrents.show', ['id' => $torrent->id])
->withErrors(trans('graveyard.resurrect-failed-pending'));
}
$history = History::whereBelongsTo($torrent)->whereBelongsTo($user)->sole();
Graveyard::create([
'user_id' => $user->id,
'torrent_id' => $torrent->id,
'seedtime' => config('graveyard.time') + $history?->seedtime ?? 0,
]);
return to_route('torrents.show', ['id' => $torrent->id])
->withSuccess(trans('graveyard.resurrect-complete'));
}
/**
* Cancel A Ressurection.
*/
public function destroy(Request $request, User $user, Graveyard $resurrection): \Illuminate\Http\RedirectResponse
{
abort_unless($request->user()->group->is_modo || $request->user()->is($user), 403);
$resurrection->delete();
return to_route('users.resurrections.index', ['user' => $user])
->withSuccess(trans('graveyard.resurrect-canceled'));
}
}