-
Notifications
You must be signed in to change notification settings - Fork 2
/
EditionController.php
130 lines (110 loc) · 3.57 KB
/
EditionController.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace App\Http\Controllers\Crew;
use App\Actions\Schedule\ResetSchedule;
use App\Http\Controllers\Controller;
use App\Models\Edition;
use App\Models\Event;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
class EditionController extends Controller
{
/**
* Returns the landing page
*
* @return View
*/
public function index(): View
{
if (Auth::user()->cannot('viewAny', Edition::class)) {
abort(403);
}
$editions = Edition::all();
return view('crew.editions.index', compact('editions'));
}
/**
* Returns the show page
*
* @param Edition $edition
* @return View
*/
public function show(Edition $edition): View
{
if (Auth::user()->cannot('view', Edition::class)) {
abort(403);
}
$events = $edition->editionEvents;
return view('crew.editions.show', compact('edition', 'events'));
}
/**
* Returns create form for the editions
*
* @return View
*/
public function create(): View
{
if (Auth::user()->cannot('create', Edition::class)) {
abort(403);
}
return view('crew.editions.create');
}
/**
* Creates new instance of Edition and stores in the database
*
* @param Request $request data about the edition
* @return RedirectResponse redirects to show page
*/
public function store(Request $request): RedirectResponse
{
if (Auth::user()->cannot('create', Edition::class)) {
abort(403);
}
$edition = Edition::create($request->validate(Edition::rules(), [
'start_at.after' => 'The start date should be at least a month from now.',
'start_at.before' => 'The start date should be two years from now at latest.',
'end_at.after' => 'The end date should be later than start date.',
'end_at.before' => 'The end date should be two years from now at latest.',
]));
// attach all the default events to a new edition
foreach (Event::all() as $event) {
$edition->addEvent($event);
}
return redirect(route('moderator.editions.show', compact('edition')))
->banner("Edition {$edition->name} was successfully created");
}
/**
* Triggers a function for a specific edition that activates it
*
* @param Edition $edition to activate
* @return RedirectResponse redirects to the index page
*/
public function activateEdition(Edition $edition): RedirectResponse
{
if (Auth::user()->cannot('activate', Edition::class)) {
abort(403);
}
// if all the dates for edition are present, it can be activated
if ($edition->configured()) {
$edition->activate();
}
return redirect(route('moderator.editions.show', $edition))
->banner("Edition {$edition->name} was successfully activated");
}
/**
* Deletes a record of a particular edition in the database
*
* @param Edition $edition
* @return RedirectResponse redirects to index page
*/
public function destroy(Edition $edition): RedirectResponse
{
if (Auth::user()->cannot('delete', Edition::class)) {
abort(403);
}
ResetSchedule::reset('full');
$edition->delete();
return redirect(route('moderator.editions.index'))
->banner("Edition {$edition->name} was successfully deleted");
}
}