Skip to content
This repository was archived by the owner on Jan 1, 2021. It is now read-only.

Commit df9f856

Browse files
committed
More request validation.
1 parent 1da2572 commit df9f856

4 files changed

Lines changed: 125 additions & 32 deletions

File tree

app/Http/Controllers/API/MappingsController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use App\MappingMovie;
2525
use App\Http\Requests;
2626
use App\Http\Requests\MappingAddRequest;
27+
use App\Http\Requests\MappingFindRequest;
28+
use App\Http\Requests\MappingGetRequest;
2729

2830

2931
class MappingsController extends JSONController
@@ -34,7 +36,7 @@ class MappingsController extends JSONController
3436
* @param int $id
3537
* @return Response
3638
*/
37-
public function get(Request $request) {
39+
public function get(MappingGetRequest $request) {
3840
$id = $request->query("id");
3941

4042
$mapping = Mapping::find($id);
@@ -47,14 +49,14 @@ public function get(Request $request) {
4749
return response()->json($mapping)->header("Access-Control-Allow-Origin", "*");
4850
}
4951

50-
public function find(Request $request)
52+
public function find(MappingFindRequest $request)
5153
{
5254
$tmdbid = $request->query("tmdbid");
5355
$movie = MappingMovie::find($tmdbid);
5456

5557
if ($movie == null)
5658
{
57-
abort(404, "Movie with tmdbid $tmdbid was not found. Either it does not exist or no mappings have been added yet");
59+
abort(404, "Movie with tmdbid $tmdbid was not found. Either it does not exist or no mappings have been added yet.");
5860
}
5961

6062
$type = $request->query("type");
@@ -136,7 +138,7 @@ public function add(MappingAddRequest $request) {
136138
return response()->json($mapping)->header("Access-Control-Allow-Origin", "*");
137139
}
138140

139-
public function vote(Request $request) {
141+
public function vote(MappingGetRequest $request) {
140142
$id = $request->query("id");
141143
$direction = $request->query("direction");
142144
if (!isset($direction) || $direction > 1)

app/Http/Requests/MappingRequests.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,98 @@ public function messages()
6969
}
7070

7171
}
72+
73+
class MappingFindRequest extends JSONRequest
74+
{
75+
/**
76+
* Determine if the user is authorized to make this request.
77+
*
78+
* @return bool
79+
*/
80+
public function authorize()
81+
{
82+
return true;
83+
}
84+
85+
/**
86+
* Get the validation rules that apply to the request.
87+
*
88+
* @return array
89+
*/
90+
public function rules()
91+
{
92+
$arr = [
93+
'tmdbid' => array(
94+
"required",
95+
"regex:/^\d+$/"
96+
),
97+
'type' => array(
98+
"sometimes",
99+
"required",
100+
Rule::in(['title', 'year', 'all']),
101+
)
102+
];
103+
104+
return $arr;
105+
}
106+
107+
/**
108+
* Get the error messages for the defined validation rules.
109+
*
110+
* @return array
111+
*/
112+
public function messages()
113+
{
114+
return [
115+
'tmdbid.required' => 'A tmdbid to find mappings by is required.',
116+
'tmdbid.regex' => 'The format of the tmdbid given is invalid!',
117+
"type.required" => "The type of mappings to return is required.",
118+
"type.in" => "The type of mapping has to be on of 'title', 'year' or 'all'.",
119+
];
120+
}
121+
122+
}
123+
124+
class MappingGetRequest extends JSONRequest
125+
{
126+
/**
127+
* Determine if the user is authorized to make this request.
128+
*
129+
* @return bool
130+
*/
131+
public function authorize()
132+
{
133+
return true;
134+
}
135+
136+
/**
137+
* Get the validation rules that apply to the request.
138+
*
139+
* @return array
140+
*/
141+
public function rules()
142+
{
143+
$arr = [
144+
'id' => array(
145+
"required",
146+
"regex:/^\d+$/"
147+
)
148+
];
149+
150+
return $arr;
151+
}
152+
153+
/**
154+
* Get the error messages for the defined validation rules.
155+
*
156+
* @return array
157+
*/
158+
public function messages()
159+
{
160+
return [
161+
'id.required' => 'The id of the mapping is required.',
162+
'id.regex' => 'The format of the id given is invalid!'
163+
];
164+
}
165+
166+
}

composer.lock

Lines changed: 19 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/views/welcome.blade.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77

8-
<title>Laravel</title>
8+
<title>Radarr API</title>
99

1010
<!-- Fonts -->
1111
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
@@ -66,7 +66,7 @@
6666
</head>
6767
<body>
6868
<div class="flex-center position-ref full-height">
69-
@if (Route::has('login'))
69+
<!-- @if (Route::has('login'))
7070
<div class="top-right links">
7171
@if (Auth::check())
7272
<a href="{{ url('/home') }}">Home</a>
@@ -75,25 +75,18 @@
7575
<a href="{{ url('/register') }}">Register</a>
7676
@endif
7777
</div>
78-
@endif
78+
@endif-->
7979

8080

8181

8282
<div class="content">
8383
<div class="title m-b-md">
84-
Laravel
84+
Radarr API
8585
</div>
8686

8787

8888
<div class="links">
89-
<a href="https://laravel.com/docs">Documentation</a>
90-
<a href="https://laracasts.com">Laracasts</a>
91-
<a href="https://laravel-news.com">News</a>
92-
<a href="https://forge.laravel.com">Forge</a>
93-
<a href="https://github.com/laravel/laravel">GitHub</a>
94-
@foreach ($movies as $movie)
95-
<li>{{ $movie->title }}</li>
96-
@endforeach
89+
In use by <a href="https://radarr.video">Radarr</a> and <a href="https://mappings.radarr.video">Radarr Mappings</a>.
9790
</div>
9891
</div>
9992
</div>

0 commit comments

Comments
 (0)