1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Judite\Models\Exchange; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use App\Events\ExchangeWasDeclined; |
8
|
|
|
use App\Events\ExchangeWasConfirmed; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
|
11
|
|
|
class ExchangeController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Create a new controller instance. |
15
|
|
|
*/ |
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->middleware('auth'); |
19
|
|
|
$this->middleware('can.student'); |
20
|
|
|
$this->middleware('student.verified'); |
21
|
|
|
$this->middleware('can.exchange'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Store a confirmation of an exchange in storage. |
26
|
|
|
* |
27
|
|
|
* @param int $id |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Http\RedirectResponse |
30
|
|
|
*/ |
31
|
|
|
public function confirm($id) |
32
|
|
|
{ |
33
|
|
|
$exchange = DB::transaction(function () use ($id) { |
34
|
|
|
$exchange = Auth::student()->proposedExchanges()->findOrFail($id); |
35
|
|
|
|
36
|
|
|
return $exchange->perform(); |
37
|
|
|
}); |
38
|
|
|
flash('The shift exchange request was successfully confirmed.')->success(); |
39
|
|
|
event(new ExchangeWasConfirmed($exchange)); |
|
|
|
|
40
|
|
|
|
41
|
|
|
return redirect()->back(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Store a decline of an exchange in storage. |
46
|
|
|
* |
47
|
|
|
* @param int $id |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\RedirectResponse |
50
|
|
|
*/ |
51
|
|
|
public function decline($id) |
52
|
|
|
{ |
53
|
|
|
$exchange = DB::transaction(function () use ($id) { |
54
|
|
|
$exchange = Auth::student()->proposedExchanges()->findOrFail($id); |
55
|
|
|
$exchange->delete(); |
56
|
|
|
|
57
|
|
|
return $exchange; |
58
|
|
|
}); |
59
|
|
|
flash('The shift exchange request was successfully declined.')->success(); |
60
|
|
|
event(new ExchangeWasDeclined($exchange)); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return redirect()->back(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Remove the specified resource from storage. |
67
|
|
|
* |
68
|
|
|
* @param int $id |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Http\RedirectResponse |
71
|
|
|
*/ |
72
|
|
|
public function destroy($id) |
73
|
|
|
{ |
74
|
|
|
DB::transaction(function () use ($id) { |
75
|
|
|
Auth::student()->requestedExchanges()->findOrFail($id)->delete(); |
76
|
|
|
}); |
77
|
|
|
flash('The shift exchange request was successfully deleted.')->success(); |
78
|
|
|
|
79
|
|
|
return redirect()->back(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|