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

Commit e079d33

Browse files
committed
Initial Push of new Mappings API and db structure.
1 parent 06baff8 commit e079d33

7 files changed

Lines changed: 213 additions & 90 deletions

File tree

app/Event.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Event extends Model
8+
{
9+
protected $connection = 'mappings_mysql';
10+
11+
protected $fillable = ["type", "mappings_id", "ip"];
12+
13+
public $timestamps = false;
14+
15+
public function mapping()
16+
{
17+
return $this->hasOne("App\Mapping", "id", "mappings_id");
18+
}
19+
20+
public function toArray()
21+
{
22+
$arr = parent::toArray();
23+
24+
$arr["mapping"] = $this->mapping->toArray();
25+
unset($arr["ip"]);
26+
$arr["mapping"]["movie"] = MappingMovie::find($arr["mapping"]["tmdbid"]);
27+
return $arr;
28+
}
29+
}
30+
31+
abstract class EventType extends Enum {
32+
const AddedMapping = 0;
33+
const ApproveMapping = 1;
34+
const DisapproveMapping = 2;
35+
const LockedMapping = 3;
36+
}
37+
38+
abstract class Enum {
39+
static function getKeys(){
40+
$class = new ReflectionClass(get_called_class());
41+
return array_keys($class->getConstants());
42+
}
43+
}

app/Http/Controllers/API/MappingsController.php

Lines changed: 84 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace App\Http\Controllers\API;
44

5+
use App\EventType;
6+
use App\TitleInfo;
7+
use App\YearInfo;
58
use Carbon\Carbon;
69

710
use App\User;
@@ -17,6 +20,9 @@
1720
use Illuminate\Support\Facades\Cache;
1821
use Helper;
1922
use MappingsCache;
23+
use App\Event;
24+
use App\MappingMovie;
25+
2026

2127
class 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
?>

app/Mapping.php

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77
use Illuminate\Support\Facades\Cache;
88
use Helper;
99
use MappingsCache;
10+
use Carbon\Carbon;
1011

11-
Relation::morphMap([
12-
'title' => 'App\TitleMapping',
13-
'year' => 'App\YearMapping',
14-
]);
1512

1613
class Mapping extends Model
1714
{
1815
protected $connection = 'mappings_mysql';
1916

20-
protected $fillable = array('tmdbid', "imdbid");
17+
protected $fillable = array('tmdbid', "info_type", "info_id");
18+
19+
protected $casts = [
20+
"locked" => "boolean",
21+
];
22+
23+
2124
//
2225

2326
/**
@@ -27,21 +30,38 @@ class Mapping extends Model
2730
*/
2831
public $timestamps = false;
2932

30-
public function mapable()
33+
public function info()
3134
{
32-
return $this->morphTo();
35+
if ($this->info_type == "title")
36+
{
37+
return $this->hasOne("App\TitleInfo", "id", "info_id");
38+
}
39+
40+
return $this->hasOne("App\YearInfo", "id", "info_id");
3341
}
3442

35-
/*public function jsonSerialize()
43+
public function title_info()
3644
{
37-
$arr = $this->toArray();
38-
//var_dump($this->mappable());
39-
return array_merge( $this->mapable->toArray(), $arr);
40-
}*/
45+
return $this->hasOne("App\TitleInfo", "id", "info_id");
46+
}
47+
48+
public function year_info()
49+
{
50+
return $this->hasOne("App\YearInfo", "id", "info_id");
51+
}
4152

4253
public function toArray()
4354
{
44-
return array_merge($this->mapable->toArray(), parent::toArray());
55+
$arr = parent::toArray();
56+
$arr["info"] = $this->info->toArray();
57+
$total = $this->vote_count;
58+
59+
$random_variation = round($total / 10.0);
60+
61+
$variation = random_int(-$random_variation, $random_variation);
62+
$arr["votes"] += $variation;
63+
$arr["vote_count"] += abs($variation);
64+
return $arr;
4565
}
4666

4767
public static function newMapping($values, $class)
@@ -58,21 +78,26 @@ public static function newMapping($values, $class)
5878

5979
public function vote($direction = 1)
6080
{
61-
$this->report_count = $this->report_count + $direction;
62-
$this->total_reports += 1;
81+
$ip = md5($_SERVER['REMOTE_ADDR']);
82+
if (Event::whereIn("type", [EventType::AddedMapping, EventType::ApproveMapping, EventType::DisapproveMapping])->where("mappings_id", "=", $this->id)->where("ip", "=", $ip)->whereBetween("date", array(Carbon::now()->addDays(-1), Carbon::now()))->first())
83+
{
84+
return;
85+
}
86+
$this->votes = $this->votes + $direction;
87+
$this->vote_count += 1;
88+
$event_type = EventType::ApproveMapping;
89+
if ($direction == -1)
90+
{
91+
$event_type = EventType::DisapproveMapping;
92+
}
93+
$event = new Event(["type" => $event_type, "mappings_id" => $this->id, "ip" => $ip]);
94+
$event->save();
6395
$this->save();
64-
$id = $this->id;
65-
$tmdbid = $this->tmdbid;
66-
$imdbid = $this->imdbid;
67-
$type = "title";
68-
69-
//Update cache for this id, so new votes are correctly displayed
70-
MappingsCache::updateMapping($this);
7196
}
7297

7398
}
7499

75-
class TitleMapping extends Model {
100+
class TitleInfo extends Model {
76101
protected $table = "title_mappings";
77102

78103
protected $connection = 'mappings_mysql';
@@ -98,14 +123,9 @@ public function __construct(array $attributes = array(), $value =null)
98123

99124
}
100125

101-
public function map()
102-
{
103-
$morph = $this->morphMany('App\Mapping', 'mapable', "mapable_type", "mapable_id");
104-
return $morph;
105-
}
106126
}
107127

108-
class YearMapping extends Model {
128+
class YearInfo extends Model {
109129
protected $table = "year_mappings";
110130

111131
protected $connection = 'mappings_mysql';
@@ -119,8 +139,4 @@ class YearMapping extends Model {
119139
*/
120140
public $timestamps = false;
121141

122-
public function map()
123-
{
124-
return $this->morphMany('App\Mapping', 'mapable', "mapable_type", "mapable_id");
125-
}
126142
}

app/MappingMovie.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: leonardogalli
5+
* Date: 23.06.17
6+
* Time: 13:28
7+
*/
8+
9+
namespace App;
10+
11+
use Illuminate\Database\Eloquent\Model;
12+
13+
class MappingMovie extends Model
14+
{
15+
public $timestamps = false;
16+
17+
protected $fillable = array("id", "title", "imdb_id");
18+
19+
protected $table = "movies";
20+
21+
protected $connection = 'mappings_mysql';
22+
23+
public function mappings()
24+
{
25+
return $this->hasMany("App\Mapping", "tmdbid", "id");
26+
}
27+
}

app/Movie.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ public function toArray()
3535
unset($arr["genres"]);
3636
return $arr;
3737
}
38+
39+
public function createMappingMovie() {
40+
return new MappingMovie(["id" => $this->id, "title" => $this->title, "imdb_id" => $this->imdb_id]);
41+
}
3842
}
43+

0 commit comments

Comments
 (0)