1
0
mirror of https://github.com/devfake/flox.git synced 2024-11-15 06:32:34 +01:00

split APIController

This commit is contained in:
devfake 2015-07-31 13:49:59 +02:00
parent 74276dca21
commit 321e973897
3 changed files with 55 additions and 42 deletions

View File

@ -2,16 +2,13 @@
namespace Flox\Http\Controllers; namespace Flox\Http\Controllers;
use DateTime;
use Flox\Item; use Flox\Item;
use Flox\Category; use Flox\Category;
use Flox\Http\Controllers\Controller; use Flox\Http\Controllers\Controller;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str; use Illuminate\Support\Str;
// todo: Rewrite API. class FloxController extends Controller {
class APIController extends Controller {
public function homeItems($category, $orderBy, $loading = 5) public function homeItems($category, $orderBy, $loading = 5)
{ {
@ -41,7 +38,6 @@
private function getItems($category, $orderBy, $count) private function getItems($category, $orderBy, $count)
{ {
$category = Category::where('slug', $category)->with('itemsCount')->first(); $category = Category::where('slug', $category)->with('itemsCount')->first();
$items = Item::where('category_id', $category->id)->orderBy($orderBy, 'desc')->take($count)->get(); $items = Item::where('category_id', $category->id)->orderBy($orderBy, 'desc')->take($count)->get();
return [ return [
@ -50,37 +46,10 @@
]; ];
} }
public function searchFloxByTitle($title) public function searchFloxByTitle($title)
{
// todo: Implement Levenshtein ;)
return Item::where('title', 'LIKE', '%' . $title . '%')->with('categories')->get();
}
public function searchTMDBByTitle($title)
{ {
$items = []; // todo: Implement Levenshtein ;)
$client = new Client(['base_uri' => 'http://api.themoviedb.org/']); return Item::where('title', 'LIKE', '%' . $title . '%')->with('categories')->get();
$response = $client->get('/3/search/multi', ['query' => ['api_key' => env('TMDB_API_KEY'), 'query' => $title]]);
$response = json_decode($response->getBody());
foreach($response->results as $result) {
if($result->media_type == 'person') continue;
$dtime = DateTime::createFromFormat('Y-m-d', (array_key_exists('release_date', $result)
? ($result->release_date ?: '1970-12-1')
: ($result->first_air_date ?: '1970-12-1')
));
$items[] = [
'tmdb_id' => $result->id,
'title' => array_key_exists('name', $result) ? $result->name : $result->title,
'poster' => $result->poster_path,
'released' => $dtime->getTimestamp(),
];
}
return $items;
} }
public function newItem() public function newItem()

View File

@ -0,0 +1,43 @@
<?php
namespace Flox\Http\Controllers;
use DateTime;
use Flox\Http\Controllers\Controller;
use GuzzleHttp\Client;
class TMDBController extends Controller {
private $client;
public function __construct()
{
$this->client = new Client(['base_uri' => 'http://api.themoviedb.org/']);
}
public function searchTMDBByTitle($title)
{
$items = [];
$response = $this->client->get('/3/search/multi', ['query' => ['api_key' => env('TMDB_API_KEY'), 'query' => $title]]);
$response = json_decode($response->getBody());
foreach($response->results as $result) {
if($result->media_type == 'person') continue;
$dtime = DateTime::createFromFormat('Y-m-d', (array_key_exists('release_date', $result)
? ($result->release_date ?: '1970-12-1')
: ($result->first_air_date ?: '1970-12-1')
));
$items[] = [
'tmdb_id' => $result->id,
'title' => array_key_exists('name', $result) ? $result->name : $result->title,
'poster' => $result->poster_path,
'released' => $dtime->getTimestamp(),
];
}
return $items;
}
}

View File

@ -1,16 +1,17 @@
<?php <?php
// todo: Rewrite API.
Route::group(['prefix' => 'api'], function() { Route::group(['prefix' => 'api'], function() {
get('all-categories', 'APIController@allCategories'); get('all-categories', 'FloxController@allCategories');
get('home-items/{category}/{orderBy}/{loading?}', 'APIController@homeItems'); get('home-items/{category}/{orderBy}/{loading?}', 'FloxController@homeItems');
get('category-items/{category}/{orderBy}/{loading}', 'APIController@categoryItems'); get('category-items/{category}/{orderBy}/{loading}', 'FloxController@categoryItems');
get('more-category-items/{categoryID}/{orderBy}/{loading}/{loaded}', 'APIController@moreCategoryItems'); get('more-category-items/{categoryID}/{orderBy}/{loading}/{loaded}', 'FloxController@moreCategoryItems');
get('search/flox/{title}', 'APIController@searchFloxByTitle'); get('search/flox/{title}', 'FloxController@searchFloxByTitle');
get('search/tmdb/{title}', 'APIController@searchTMDBByTitle'); get('search/tmdb/{title}', 'TMDBController@searchTMDBByTitle');
post('new', 'APIController@newItem'); post('new', 'FloxController@newItem');
}); });