1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use App\Judite\Models\Exchange; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use App\Events\ExchangeWasDeclined; |
9
|
|
|
use App\Events\ExchangeWasConfirmed; |
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\Response |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function confirm($id) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$exchange = DB::transaction(function () use ($id) { |
34
|
|
|
$exchange = student()->proposedExchanges()->findOrFail($id); |
35
|
|
|
|
36
|
|
|
return $exchange->perform(); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
event(new ExchangeWasConfirmed($exchange)); |
40
|
|
|
flash('The shift exchange request was successfully confirmed.')->success(); |
41
|
|
|
|
42
|
|
|
return redirect()->back(); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Store a decline of an exchange in storage. |
47
|
|
|
* |
48
|
|
|
* @param int $id |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Http\Response |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function decline($id) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$exchange = DB::transaction(function () use ($id) { |
55
|
|
|
$exchange = student()->proposedExchanges()->findOrFail($id); |
56
|
|
|
$exchange->delete(); |
57
|
|
|
|
58
|
|
|
return $exchange; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
event(new ExchangeWasDeclined($exchange)); |
62
|
|
|
flash('The shift exchange request was successfully declined.')->success(); |
63
|
|
|
|
64
|
|
|
return redirect()->back(); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Remove the specified resource from storage. |
69
|
|
|
* |
70
|
|
|
* @param int $id |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Http\Response |
73
|
|
|
*/ |
74
|
|
|
public function destroy($id) |
75
|
|
|
{ |
76
|
|
|
DB::transaction(function () use ($id) { |
77
|
|
|
student()->requestedExchanges()->findOrFail($id)->delete(); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
flash('The shift exchange request was successfully deleted.')->success(); |
81
|
|
|
|
82
|
|
|
return redirect()->back(); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.