Completed
Pull Request — master (#96)
by Brandon
02:26
created

SiteController::locations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\DataTables\SiteDataTable;
7
use App\Site;
8
9
class SiteController extends Controller
10
{
11
    /**
12
     * Create a new controller instance.
13
     */
14
    public function __construct()
15
    {
16
        $this->middleware('auth');
17
    }
18
    
19
    /**
20
     * Display a listing of the resource.
21
     *
22
     * @param  SiteDataTable   $dataTable
23
     * @return Response
24
     */
25
    public function index(SiteDataTable $dataTable)
26
    {
27
        return $dataTable->render('site.index');
28
    }
29
    
30
    /**
31
     * Show the form for creating a new resource.
32
     *
33
     * @return \Illuminate\Http\Response
34
     */
35
    public function create()
36
    {
37
        return view('site.create');
38
    }
39
    
40
    /**
41
     * Store a newly created resource in storage.
42
     *
43
     * @param  \Illuminate\Http\Request  $request
44
     * @return \Illuminate\Http\Response
45
     */
46 View Code Duplication
    public function store(Request $request)
47
    {
48
        $request->validate([
49
            'name' => 'required|min:2|max:75|regex:#(^[a-zA-Z0-9])([\w ]*)(\w$)#|unique:sites,name',
50
        ]);
51
        
52
        $site = Site::create(['name' => $request->name]);
53
        
54
        return redirect()->route('site.show', $site->id)
55
            ->with('success', 'Site created successfully');
56
    }
57
    
58
    /**
59
     * Display the specified resource.
60
     *
61
     * @param  int  $id
62
     * @return \Illuminate\Http\Response
63
     */
64
    public function show($id)
65
    {
66
        $site = Site::findOrFail($id);
67
        $locations = $site->locations()->orderBy('name', 'ASC')->paginate(15);
68
        
69
        return view('site.show', [ 'site' => $site, 'locations' => $locations ]);
70
    }
71
    
72
    /**
73
     * Show the form for editing the specified resource.
74
     *
75
     * @param  int  $id
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function edit($id)
79
    {
80
        $site = Site::findOrFail($id);
81
        
82
        return view('site.edit', [ 'site' => $site ]);
83
    }
84
    
85
    /**
86
     * Update the specified resource in storage.
87
     *
88
     * @param  \Illuminate\Http\Request  $request
89
     * @param  Site  $site
90
     * @return \Illuminate\Http\Response
91
     */
92 View Code Duplication
    public function update(Request $request, Site $site)
93
    {
94
        $request->validate([
95
            'name' => 'required|min:2|max:75|regex:#(^[a-zA-Z0-9])([\w ]*)(\w$)#|unique:sites,name,'.$site->id,
96
        ]);
97
        
98
        $site->update(['name' => $request->name]);
99
        
100
        return redirect()->route('site.show', $site->id)
101
            ->with('success', 'Site updated successfully');
102
    }
103
    
104
    /**
105
     * Remove the specified resource from storage.
106
     *
107
     * @param  int  $id
108
     * @return \Illuminate\Http\Response
109
     */
110
    public function destroy($id)
111
    {
112
        Site::findOrFail($id)->delete();
113
        return redirect()->route('site.index')
114
            ->with('success','Site deleted successfully');
115
    }
116
    
117
    /**
118
     * Get the locations belonging to the given site
119
     *
120
     * @param  Site $site
121
     * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
122
     */
123
    public function locations(Site $site)
124
    {
125
        $locations = $site->locations;
126
        
127
        return response()->json($locations);
128
    }
129
}
130