2017-01-23 22:26:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Models;
|
|
|
|
|
|
|
|
use App\Item as Model;
|
2017-02-02 17:38:07 +01:00
|
|
|
use App\Item;
|
2017-01-23 22:26:04 +01:00
|
|
|
use App\Services\Storage;
|
|
|
|
use App\Services\TMDB;
|
|
|
|
|
|
|
|
class ItemService {
|
|
|
|
|
|
|
|
private $model;
|
|
|
|
private $tmdb;
|
|
|
|
private $storage;
|
|
|
|
private $alternativeTitleService;
|
|
|
|
private $episodeService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Model $model
|
|
|
|
* @param TMDB $tmdb
|
|
|
|
* @param Storage $storage
|
|
|
|
* @param AlternativeTitleService $alternativeTitleService
|
|
|
|
* @param EpisodeService $episodeService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Model $model,
|
|
|
|
TMDB $tmdb,
|
|
|
|
Storage $storage,
|
|
|
|
AlternativeTitleService $alternativeTitleService,
|
|
|
|
EpisodeService $episodeService
|
|
|
|
){
|
|
|
|
$this->model = $model;
|
|
|
|
$this->tmdb = $tmdb;
|
|
|
|
$this->storage = $storage;
|
|
|
|
$this->alternativeTitleService = $alternativeTitleService;
|
|
|
|
$this->episodeService = $episodeService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @return Model
|
|
|
|
*/
|
|
|
|
public function create($data)
|
|
|
|
{
|
|
|
|
$item = $this->model->store($data);
|
|
|
|
|
|
|
|
$this->storage->downloadPoster($item->poster);
|
|
|
|
|
|
|
|
$this->episodeService->create($item);
|
|
|
|
|
|
|
|
$this->alternativeTitleService->create($item);
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
2017-02-02 17:38:07 +01:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @param $mediaType
|
|
|
|
* @return Item
|
|
|
|
*/
|
|
|
|
public function createEmpty($data, $mediaType)
|
|
|
|
{
|
|
|
|
$mediaType = $mediaType == 'movies' ? 'movie' : 'tv';
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'name' => isset($data->changed->name) ? $data->changed->name : $data->name,
|
|
|
|
'src' => isset($data->changed->src) ? $data->changed->src : $data->src,
|
|
|
|
'subtitles' => isset($data->changed->subtitles) ? $data->changed->subtitles : $data->subtitles,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->model->storeEmpty($data, $mediaType);
|
|
|
|
}
|
|
|
|
|
2017-01-23 22:26:04 +01:00
|
|
|
/**
|
|
|
|
* @param $itemId
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function remove($itemId)
|
|
|
|
{
|
|
|
|
$item = $this->model->find($itemId);
|
|
|
|
|
|
|
|
if( ! $item) {
|
|
|
|
return response('Not Found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmdbId = $item->tmdb_id;
|
|
|
|
|
|
|
|
$item->delete();
|
|
|
|
|
|
|
|
// Delete all related episodes, alternative titles and poster image.
|
|
|
|
$this->episodeService->remove($tmdbId);
|
|
|
|
$this->alternativeTitleService->remove($tmdbId);
|
|
|
|
$this->storage->removePosterFile($item->poster);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all items with pagination.
|
|
|
|
*
|
|
|
|
* @param $type
|
|
|
|
* @param $orderBy
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getWithPagination($type, $orderBy)
|
|
|
|
{
|
|
|
|
$orderType = $orderBy == 'rating' ? 'asc' : 'desc';
|
|
|
|
|
|
|
|
$item = $this->model->orderBy($orderBy, $orderType)->with('latestEpisode');
|
|
|
|
|
|
|
|
if($type != 'home') {
|
|
|
|
$item = $item->where('media_type', $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $item->simplePaginate(config('app.LOADING_ITEMS'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update rating for an movie.
|
|
|
|
*
|
|
|
|
* @param $itemId
|
|
|
|
* @param $rating
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function changeRating($itemId, $rating)
|
|
|
|
{
|
|
|
|
$item = $this->model->find($itemId);
|
|
|
|
|
|
|
|
if( ! $item) {
|
|
|
|
return response('Not Found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->update([
|
|
|
|
'rating' => $rating,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-25 14:44:51 +01:00
|
|
|
* See if we can find a item by title, tmdb_id or src in our database.
|
2017-01-23 22:26:04 +01:00
|
|
|
*
|
|
|
|
* @param $type
|
|
|
|
* @param $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function findBy($type, $value)
|
|
|
|
{
|
2017-01-25 14:44:51 +01:00
|
|
|
switch($type) {
|
|
|
|
case 'title':
|
|
|
|
return $this->model->findByTitle($value)->first();
|
2017-02-02 17:38:07 +01:00
|
|
|
case 'fp_name':
|
|
|
|
return $this->model->findByFPName($value)->first();
|
2017-01-25 14:44:51 +01:00
|
|
|
case 'tmdb_id':
|
|
|
|
return $this->model->findByTmdbId($value)->first();
|
|
|
|
case 'src':
|
|
|
|
return $this->model->findBySrc($value)->first();
|
2017-01-23 22:26:04 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
return null;
|
2017-01-23 22:26:04 +01:00
|
|
|
}
|
2017-01-24 16:11:47 +01:00
|
|
|
}
|