mirror of
https://github.com/devfake/flox.git
synced 2024-11-16 07:02:31 +01:00
24b950787f
* create and move fixtures * migrations, no-image file * create empty item if file not found * update tests * remove migration comments * hide buttons for suggestions and seasons if it's empty
159 lines
3.9 KiB
PHP
159 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Models;
|
|
|
|
use App\Item as Model;
|
|
use App\Item;
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
|
|
/**
|
|
* @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,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* See if we can find a item by title, tmdb_id or src in our database.
|
|
*
|
|
* @param $type
|
|
* @param $value
|
|
* @return mixed
|
|
*/
|
|
public function findBy($type, $value)
|
|
{
|
|
switch($type) {
|
|
case 'title':
|
|
return $this->model->findByTitle($value)->first();
|
|
case 'fp_name':
|
|
return $this->model->findByFPName($value)->first();
|
|
case 'tmdb_id':
|
|
return $this->model->findByTmdbId($value)->first();
|
|
case 'src':
|
|
return $this->model->findBySrc($value)->first();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|