2222use MappingsCache ;
2323use App \Event ;
2424use App \MappingMovie ;
25+ use App \Http \Requests ;
26+ use App \Http \Requests \MappingAddRequest ;
2527
2628
2729class 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 " , "* " );
0 commit comments