22
33namespace App \Http \Controllers \API ;
44
5+ use App \EventType ;
6+ use App \TitleInfo ;
7+ use App \YearInfo ;
58use Carbon \Carbon ;
69
710use App \User ;
1720use Illuminate \Support \Facades \Cache ;
1821use Helper ;
1922use MappingsCache ;
23+ use App \Event ;
24+ use App \MappingMovie ;
25+
2026
2127class MappingsController extends JSONController
2228{
@@ -28,74 +34,88 @@ class MappingsController extends JSONController
2834 */
2935 public function get (Request $ request ) {
3036 $ id = $ request ->query ("id " );
31- $ tmdbid = $ request ->query ("tmdbid " );
32- $ imdbid = $ request ->query ("imdbid " );
3337
38+ $ mapping = Mapping::find ($ id );
39+
40+ return response ()->json ($ mapping )->header ("Access-Control-Allow-Origin " , "* " );
41+ }
42+
43+ public function find (Request $ request )
44+ {
45+ $ tmdbid = $ request ->query ("tmdbid " );
46+ $ movie = MappingMovie::find ($ tmdbid );
3447 $ type = $ request ->query ("type " );
3548
36- $ query = array ("id " => $ id );
49+ $ titles = [];
50+ $ years = [];
3751
38- if (isset ($ tmdbid )) {
39- $ query = array ("tmdbid " => $ tmdbid );
40- } else if (isset ($ imdbid )) {
41- $ query = array ("imdbid " => $ imdbid );
52+ if ($ type == "title " || $ type == "all " || $ type == null )
53+ {
54+ $ titles = Mapping::where ("tmdbid " , "= " , $ tmdbid )->where ("info_type " , "= " , "title " )->get ()->toArray ();
4255 }
4356
44- if (isset ( $ type) )
57+ if ($ type == " year " || $ type == " all " || $ type == null )
4558 {
46- $ query [ " mapable_type " ] = $ type ;
59+ $ years = Mapping:: where ( " tmdbid " , " = " , $ tmdbid )-> where ( " info_type " , " = " , " year " )-> get ()-> toArray () ;
4760 }
4861
49- $ mappings = MappingsCache::rememberQuery ($ query );/*Cache::remember($key, Carbon::now()->addMinutes(15), function() use ($query) {
50- return Mapping::where($query)->get()->toArray();
51- });*/
52-
62+ $ movie ["mappings " ] = ["titles " => $ titles , "years " => $ years ];
5363
54- return response ()->json ($ mappings )->header ("Access-Control-Allow-Origin " , "* " );
55- }
64+ return response ()->json ($ movie )->header ("Access-Control-Allow-Origin " , "* " );
65+ }
5666
5767 public function add (Request $ request ) {
58- $ class = "App\TitleMapping " ;
59- $ values = array (
60- "tmdbid " => $ request ->query ("tmdbid " ),
61- "imdbid " => $ request ->query ("imdbid " )
62- );
63- $ new_mapping = null ;
64- if ($ request ->query ("type " ) == "title " )
65- {
66- $ aka_title = $ request ->query ("aka_title " );
67- $ clean_title = Helper::clean_title ($ aka_title );
68-
69- $ mapping = TitleMapping::where ("aka_clean_title " , $ clean_title )->first ();
70- if (isset ($ mapping ))
71- {
72- $ mapping ->map ->first ()->vote ();
73- $ new_mapping = $ mapping ->map ->first ();
74- } else
75- {
76- $ values ["aka_title " ] = $ aka_title ;
77- $ new_mapping = Mapping::newMapping ($ values , $ class );
78- }
79-
80- } else if ($ request ->query ("type " ) == "year " )
81- {
82- $ class = "App\YearMapping " ;
83-
84- $ aka_year = $ request ->query ("aka_year " );
85-
86- $ mapping = YearMapping::where ("aka_year " , $ aka_year )->first ();
87- if (isset ($ mapping ))
88- {
89- $ mapping ->map ->first ()->vote ();
90- $ new_mapping = $ mapping ->map ->first ();
91- } else
92- {
93- $ values ["aka_year " ] = $ aka_year ;
94- $ new_mapping = Mapping::newMapping ($ values , $ class );
95- }
96- }
97-
98- return response ()->json ($ new_mapping )->header ("Access-Control-Allow-Origin " , "* " );
68+ $ tmdbid = $ request ->query ("tmdbid " );
69+ $ type = $ request ->query ("type " );
70+
71+ //Ensure that the movie is in our mapping database!
72+ if (!MappingMovie::find ($ tmdbid ))
73+ {
74+ Movie::find ($ tmdbid )->createMappingMovie ()->save ();
75+ }
76+
77+ $ existing = false ;
78+ $ info = null ;
79+
80+ if ($ type == "title " )
81+ {
82+ $ aka_title = $ request ->get ("aka_title " );
83+ $ aka_clean_title = Helper::clean_title ($ aka_title );
84+ $ existing = Mapping::whereHas ("title_info " , function ($ query ) use ($ aka_clean_title ){
85+ $ query ->where ("aka_clean_title " , "= " , $ aka_clean_title );
86+ })->first ();
87+ $ info = new TitleInfo (["aka_title " => $ aka_title , "aka_clean_title " => $ aka_clean_title ]);
88+ }
89+ else
90+ {
91+ $ aka_year = $ request ->get ("aka_year " );
92+ $ existing = Mapping::whereHas ("year_info " , function ($ query ) use ($ aka_year ){
93+ $ query ->where ("aka_year " , "= " , $ aka_year );
94+ })->first ();
95+ $ info = new YearInfo (["aka_year " => $ aka_year ]);
96+ }
97+
98+ if ($ existing != null && $ existing != false ) {
99+ $ existing ->vote ();
100+ return response ()->json ($ existing );
101+ }
102+
103+ $ info ->save ();
104+
105+ $ mapping = new Mapping (["tmdbid " => $ tmdbid , "info_type " => $ type , "info_id " => $ info ->id ]);
106+
107+ $ mapping ->save ();
108+
109+ $ mapping ->info = $ info ;
110+
111+ $ mapping ->votes = 1 ;
112+ $ mapping ->vote_count = 1 ;
113+ $ mapping ->locked = false ;
114+
115+ $ event = new Event (["type " => EventType::AddedMapping, "mappings_id " => $ mapping ->id , "ip " => md5 ($ _SERVER ['REMOTE_ADDR ' ])]);
116+ $ event ->save ();
117+
118+ return response ()->json ($ mapping )->header ("Access-Control-Allow-Origin " , "* " );
99119 }
100120
101121 public function vote (Request $ request ) {
@@ -110,6 +130,14 @@ public function vote(Request $request) {
110130
111131 return response ()->json ($ mapping )->header ("Access-Control-Allow-Origin " , "* " );
112132 }
133+
134+ public function latest () {
135+ $ events = Cache::remember ("mappings.latest " , Carbon::now ()->addMinutes (1 ), function (){
136+ return Event::where ("type " , "= " , 0 )->orderByDesc ("date " )->limit (5 )->get ()->toArray ();
137+ });
138+
139+ return response ()->json ($ events )->header ("Access-Control-Allow-Origin " , "* " );
140+ }
113141}
114142
115143?>
0 commit comments