1
0
mirror of https://github.com/devfake/flox.git synced 2024-11-16 07:02:31 +01:00
flox/backend/app/Services/Models/ItemService.php

138 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\Models;
use App\Item as Model;
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 $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 'tmdb_id':
return $this->model->findByTmdbId($value)->first();
case 'src':
return $this->model->findBySrc($value)->first();
}
return null;
}
2017-01-24 16:11:47 +01:00
}