Repository navigation
Expand file tree
/
Copy pathScheduledCommitController.php
More file actions
64 lines (53 loc) · 2.68 KB
/
Copy pathScheduledCommitController.php
File metadata and controls
64 lines (53 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Actions\CreateScheduledCommit;
use App\Actions\DeleteScheduledCommit;
use App\Enums\UserUpdate;
use App\Exceptions\GuildPaused;
use App\Http\Requests\CreateScheduledCommitRequest;
use App\Http\Requests\DeleteScheduledCommitRequest;
use App\Http\Resources\ScheduledCommitResource;
use App\Models\ScheduledCommit;
use App\Models\User;
use App\Queries\ScheduledCommitsQuery;
use Carbon\CarbonImmutable;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
final readonly class ScheduledCommitController
{
public function index(Request $request, #[CurrentUser] User $user, ScheduledCommitsQuery $scheduled): Response
{
return Inertia::render('scheduled-commits/index', [
'scheduledCommits' => Inertia::live(function () use ($scheduled, $user, $request): LengthAwarePaginator {
$page = $scheduled->builder($user)->paginate(20);
$page->setCollection(collect(ScheduledCommitResource::collection($page->getCollection())->resolve($request)));
return $page;
}, on: UserUpdate::ScheduledCommits->value, channel: new PrivateChannel($user)),
]);
}
public function store(CreateScheduledCommitRequest $request, #[CurrentUser] User $user, CreateScheduledCommit $action): RedirectResponse
{
try {
$action->handle($user, $request->string('body')->value(), CarbonImmutable::parse($request->string('scheduled_at')->value())->utc(), $request->images(), $request->imageDescriptions(), $request->poll(), $request->linkPreview(), $request->organization(), guild: $request->guild());
} catch (GuildPaused) {
return back()->withErrors(['guild_handle' => 'This guild is paused until it has a moderator.']);
}
Inertia::flash('scheduled', true);
Inertia::flash('toast', ['type' => 'success', 'message' => 'Commit scheduled. Only you can see it until it is submitted.']);
return back();
}
public function destroy(DeleteScheduledCommitRequest $request, #[CurrentUser] User $user, ScheduledCommit $scheduledCommit, DeleteScheduledCommit $action): RedirectResponse
{
$canceled = $action->handle($user, $scheduledCommit);
Inertia::flash('toast', ['type' => $canceled ? 'success' : 'info', 'message' => $canceled
? 'Scheduled commit canceled.'
: 'This commit was already submitted or canceled. Refresh to see its status.']);
return back();
}
}