2
2
3
3
namespace App \Http \Controllers ;
4
4
5
- use Illuminate \ Http \ Request ;
6
-
5
+ use App \ Client ;
6
+ use App \ Service ;
7
7
use App \Http \Requests ;
8
+ use Illuminate \Http \Request ;
8
9
9
10
class ServiceController extends Controller
10
11
{
@@ -15,7 +16,9 @@ class ServiceController extends Controller
15
16
*/
16
17
public function index ()
17
18
{
18
- //
19
+ $ services = Service::orderBy ('title ' )->with (['client ' ])->get ();
20
+
21
+ return view ('services.index ' )->with (compact ('services ' ));
19
22
}
20
23
21
24
/**
@@ -25,7 +28,14 @@ public function index()
25
28
*/
26
29
public function create ()
27
30
{
28
- //
31
+ $ currencies = [
32
+ 'hrk ' => 'HRK ' ,
33
+ 'usd ' => 'USD ' ,
34
+ 'eur ' => 'EUR ' ,
35
+ ];
36
+ $ clients = Client::orderBy ('name ' )->pluck ('name ' , 'id ' )->toArray ();
37
+
38
+ return view ('services.create ' )->with (compact ('currencies ' , 'clients ' ));
29
39
}
30
40
31
41
/**
@@ -36,18 +46,33 @@ public function create()
36
46
*/
37
47
public function store (Request $ request )
38
48
{
39
- //
40
- }
49
+ $ this ->validate ($ request , [
50
+ 'title ' => 'required|max:255 ' ,
51
+ 'note ' => 'string ' ,
52
+ 'month ' => 'required|integer|min:1|max:12 ' ,
53
+ 'day ' => 'required|integer|min:1|max:31 ' ,
54
+ 'cost ' => 'required|integer|min:0 ' ,
55
+ 'currency ' => 'required|in:hrk,usd,eur ' ,
56
+ 'active ' => 'boolean ' ,
57
+ 'client_id ' => 'required|exists:clients,id '
58
+ ]);
41
59
42
- /**
43
- * Display the specified resource.
44
- *
45
- * @param int $id
46
- * @return \Illuminate\Http\Response
47
- */
48
- public function show ($ id )
49
- {
50
- //
60
+ $ client = Client::find ($ request ->get ('client_id ' ));
61
+
62
+ $ service = new Service ;
63
+ $ service ->title = $ request ->get ('title ' );
64
+ $ service ->note = $ request ->get ('note ' );
65
+ $ service ->month = $ request ->get ('month ' );
66
+ $ service ->day = $ request ->get ('day ' );
67
+ $ service ->cost = $ request ->get ('cost ' );
68
+ $ service ->currency = $ request ->get ('currency ' );
69
+ $ service ->active = $ request ->get ('active ' , false );
70
+ $ service ->client ()->associate ($ client );
71
+ $ service ->save ();
72
+
73
+ flash ()->success ('Service created! ' );
74
+
75
+ return redirect ()->route ('services.index ' );
51
76
}
52
77
53
78
/**
@@ -58,7 +83,15 @@ public function show($id)
58
83
*/
59
84
public function edit ($ id )
60
85
{
61
- //
86
+ $ service = Service::findOrFail ($ id );
87
+ $ currencies = [
88
+ 'hrk ' => 'HRK ' ,
89
+ 'usd ' => 'USD ' ,
90
+ 'eur ' => 'EUR ' ,
91
+ ];
92
+ $ clients = Client::orderBy ('name ' )->pluck ('name ' , 'id ' )->toArray ();
93
+
94
+ return view ('services.edit ' )->with (compact ('service ' , 'currencies ' , 'clients ' ));
62
95
}
63
96
64
97
/**
@@ -70,7 +103,35 @@ public function edit($id)
70
103
*/
71
104
public function update (Request $ request , $ id )
72
105
{
73
- //
106
+ $ service = Service::findOrFail ($ id );
107
+
108
+ $ this ->validate ($ request , [
109
+ 'title ' => 'required|max:255 ' ,
110
+ 'note ' => 'string ' ,
111
+ 'month ' => 'required|integer|min:1|max:12 ' ,
112
+ 'day ' => 'required|integer|min:1|max:31 ' ,
113
+ 'cost ' => 'required|integer|min:0 ' ,
114
+ 'currency ' => 'required|in:hrk,usd,eur ' ,
115
+ 'active ' => 'boolean ' ,
116
+ 'client_id ' => 'required|exists:clients,id '
117
+ ]);
118
+
119
+ $ service ->update ([
120
+ 'title ' => $ request ->get ('title ' ),
121
+ 'note ' => $ request ->get ('note ' ),
122
+ 'month ' => $ request ->get ('month ' ),
123
+ 'day ' => $ request ->get ('day ' ),
124
+ 'cost ' => $ request ->get ('cost ' ),
125
+ 'currency ' => $ request ->get ('currency ' ),
126
+ 'active ' => $ request ->get ('active ' , false ),
127
+ ]);
128
+
129
+ $ client = Client::find ($ request ->get ('client_id ' ));
130
+ $ service ->client ()->associate ($ client );
131
+
132
+ flash ()->success ('Service Updated! ' );
133
+
134
+ return redirect ()->route ('services.edit ' , $ service ->id );
74
135
}
75
136
76
137
/**
@@ -81,6 +142,11 @@ public function update(Request $request, $id)
81
142
*/
82
143
public function destroy ($ id )
83
144
{
84
- //
145
+ $ service = Service::findOrFail ($ id );
146
+ $ service ->delete ();
147
+
148
+ flash ()->success ('Service Deleted! ' );
149
+
150
+ return redirect ()->route ('services.index ' );
85
151
}
86
152
}
0 commit comments