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

Commit 45d6356

Browse files
committed
Some request validation.
1 parent e079d33 commit 45d6356

2 files changed

Lines changed: 103 additions & 3 deletions

File tree

app/Http/Controllers/API/MappingsController.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use MappingsCache;
2323
use App\Event;
2424
use App\MappingMovie;
25+
use App\Http\Requests;
26+
use App\Http\Requests\MappingAddRequest;
2527

2628

2729
class MappingsController extends JSONController
@@ -37,13 +39,24 @@ public function get(Request $request) {
3739

3840
$mapping = Mapping::find($id);
3941

42+
if ($mapping == null)
43+
{
44+
abort(404, "Mapping with id $id was not found. Maybe it was removed?");
45+
}
46+
4047
return response()->json($mapping)->header("Access-Control-Allow-Origin", "*");
4148
}
4249

4350
public function find(Request $request)
4451
{
4552
$tmdbid = $request->query("tmdbid");
4653
$movie = MappingMovie::find($tmdbid);
54+
55+
if ($movie == null)
56+
{
57+
abort(404, "Movie with tmdbid $tmdbid was not found. Either it does not exist or no mappings have been added yet");
58+
}
59+
4760
$type = $request->query("type");
4861

4962
$titles = [];
@@ -64,14 +77,19 @@ public function find(Request $request)
6477
return response()->json($movie)->header("Access-Control-Allow-Origin", "*");
6578
}
6679

67-
public function add(Request $request) {
80+
public function add(MappingAddRequest $request) {
6881
$tmdbid = $request->query("tmdbid");
6982
$type = $request->query("type");
7083

7184
//Ensure that the movie is in our mapping database!
7285
if (!MappingMovie::find($tmdbid))
7386
{
74-
Movie::find($tmdbid)->createMappingMovie()->save();
87+
$movie = Movie::find($tmdbid);
88+
if ($movie == null)
89+
{
90+
abort(422, "The movie with the given tmdbid could not be found!");
91+
}
92+
$movie->createMappingMovie()->save();
7593
}
7694

7795
$existing = false;
@@ -121,11 +139,22 @@ public function add(Request $request) {
121139
public function vote(Request $request) {
122140
$id = $request->query("id");
123141
$direction = $request->query("direction");
124-
if (!isset($direction))
142+
if (!isset($direction) || $direction > 1)
125143
{
126144
$direction = 1;
127145
}
146+
147+
if ($direction < 1)
148+
{
149+
$direction = -1;
150+
}
128151
$mapping = Mapping::find($id);
152+
153+
if ($mapping == null)
154+
{
155+
abort(404,"Mapping with id $id was not found. Maybe it was removed?");
156+
}
157+
129158
$mapping->vote($direction);
130159

131160
return response()->json($mapping)->header("Access-Control-Allow-Origin", "*");
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Http\JsonResponse;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Illuminate\Validation\Rule;
9+
10+
class MappingAddRequest extends JSONRequest
11+
{
12+
/**
13+
* Determine if the user is authorized to make this request.
14+
*
15+
* @return bool
16+
*/
17+
public function authorize()
18+
{
19+
return true;
20+
}
21+
22+
/**
23+
* Get the validation rules that apply to the request.
24+
*
25+
* @return array
26+
*/
27+
public function rules()
28+
{
29+
$arr = [
30+
'tmdbid' => array(
31+
"required",
32+
"regex:/^\d+$/"
33+
),
34+
'type' => array(
35+
"required",
36+
Rule::in(['title', 'year']),
37+
)
38+
];
39+
40+
if ($this->input("type") == "title")
41+
{
42+
$arr["aka_title"] = ["required", "regex:/^.{2}.+/"];
43+
}
44+
else if ($this->input("type") == "year")
45+
{
46+
$arr["aka_year"] = ["required", "regex:/^(19|20)\d{2}$/"];
47+
}
48+
49+
return $arr;
50+
}
51+
52+
/**
53+
* Get the error messages for the defined validation rules.
54+
*
55+
* @return array
56+
*/
57+
public function messages()
58+
{
59+
return [
60+
'tmdbid.required' => 'A tmdbid for the movie is required',
61+
'tmdbid.regex' => 'The format of the tmdbid given is invalid!',
62+
"type.required" => "The type of mapping to add is required",
63+
"type.in" => "The type of mapping has to either be 'title' or 'year'.",
64+
"aka_year.required" => "The alternative year is required with type 'year'.",
65+
"aka_year.regex" => "The alternative year has to be a valid movie year.",
66+
"aka_title.required" => "The alternative title is required with type 'title'",
67+
"aka_title.regex" => "The alternative title must be at least 3 letters long"
68+
];
69+
}
70+
71+
}

0 commit comments

Comments
 (0)