Newer
Older
Krzysztof Domańczy
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
declare(strict_types=1);
namespace Modules\Api\Rest\V1\Controllers;
use App\Entities\Episode;
use App\Models\EpisodeModel;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\Response;
use Modules\Api\Rest\V1\Config\Services;
class EpisodeController extends Controller
{
use ResponseTrait;
public function __construct()
{
Services::restApiExceptions()->initialize();
}
public function list(): Response
{
$query = $this->request->getGet('query');
$order = $this->request->getGet('order') ?? 'newest';
$podcastIds = $this->request->getGet('podcastIds');
$builder = (new EpisodeModel());
if ($podcastIds !== null) {
$builder->whereIn('podcast_id', explode(',', (string) $podcastIds));
}
if ($query !== null) {
$builder->fullTextSearch($query);
if ($order === 'search') {
Krzysztof Domańczy
committed
$builder->orderBy('(episodes_score + podcasts_score)', 'desc');
}
}
if ($order === 'newest') {
$builder->orderBy('episodes.created_at', 'desc');
Krzysztof Domańczy
committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
}
$data = $builder->findAll(
(int) ($this->request->getGet('limit') ?? config('RestApi')->limit),
(int) $this->request->getGet('offset')
);
array_map(static function ($episode): void {
self::mapEpisode($episode);
}, $data);
return $this->respond($data);
}
public function view(int $id): Response
{
$episode = (new EpisodeModel())->getEpisodeById($id);
if (! $episode instanceof Episode) {
return $this->failNotFound('Episode not found');
}
return $this->respond($this->mapEpisode($episode));
}
protected static function mapEpisode(Episode $episode): Episode
{
$episode->cover_url = $episode->getCover()
->file_url;
$episode->audio_url = $episode->getAudioUrl();
$episode->duration = round($episode->audio->duration);
return $episode;
}
}