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;
use DateTime;
use Flox\Item;
use Flox\Category;
use Flox\Http\Controllers\Controller;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;
// todo: Rewrite API.
class APIController extends Controller {
class FloxController extends Controller {
public function homeItems($category, $orderBy, $loading = 5)
{
@ -41,7 +38,6 @@
private function getItems($category, $orderBy, $count)
{
$category = Category::where('slug', $category)->with('itemsCount')->first();
$items = Item::where('category_id', $category->id)->orderBy($orderBy, 'desc')->take($count)->get();
return [
@ -56,33 +52,6 @@
return Item::where('title', 'LIKE', '%' . $title . '%')->with('categories')->get();
}
public function searchTMDBByTitle($title)
{
$items = [];
$client = new Client(['base_uri' => 'http://api.themoviedb.org/']);
$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()
{
$item = new Item();

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