2022-06-22 10:48:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Modules\Api\Rest\V1\Controllers;
|
|
|
|
|
|
|
|
use App\Entities\Podcast;
|
|
|
|
use App\Models\PodcastModel;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
use CodeIgniter\Controller;
|
2024-04-28 16:39:01 +00:00
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
2022-06-22 10:48:58 +00:00
|
|
|
|
|
|
|
class PodcastController extends Controller
|
|
|
|
{
|
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2025-01-08 11:11:19 +00:00
|
|
|
service('restApiExceptions')->initialize();
|
2022-06-22 10:48:58 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
public function list(): ResponseInterface
|
2022-06-22 10:48:58 +00:00
|
|
|
{
|
|
|
|
$data = (new PodcastModel())->findAll();
|
2022-09-28 14:00:05 +00:00
|
|
|
array_map(static function ($podcast): void {
|
2023-06-21 10:07:31 +00:00
|
|
|
self::mapPodcast($podcast);
|
2022-06-22 10:48:58 +00:00
|
|
|
}, $data);
|
|
|
|
return $this->respond($data);
|
|
|
|
}
|
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
public function view(int $id): ResponseInterface
|
2022-06-22 10:48:58 +00:00
|
|
|
{
|
2023-06-21 10:07:31 +00:00
|
|
|
$podcast = (new PodcastModel())->getPodcastById($id);
|
|
|
|
if (! $podcast instanceof Podcast) {
|
2022-06-22 10:48:58 +00:00
|
|
|
return $this->failNotFound('Podcast not found');
|
|
|
|
}
|
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
// @phpstan-ignore-next-line
|
2023-06-21 10:07:31 +00:00
|
|
|
return $this->respond(self::mapPodcast($podcast));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function mapPodcast(Podcast $podcast): Podcast
|
|
|
|
{
|
|
|
|
$podcast->feed_url = $podcast->getFeedUrl();
|
|
|
|
$podcast->actor_display_name = $podcast->getActor()
|
2025-01-08 11:11:19 +00:00
|
|
|
->display_name;
|
2023-06-21 10:07:31 +00:00
|
|
|
$podcast->cover_url = $podcast->getCover()
|
2025-01-08 11:11:19 +00:00
|
|
|
->file_url;
|
2023-06-21 10:07:31 +00:00
|
|
|
|
|
|
|
$categories = [$podcast->getCategory(), ...$podcast->getOtherCategories()];
|
|
|
|
|
|
|
|
foreach ($categories as $category) {
|
2024-04-28 16:39:01 +00:00
|
|
|
$category->translated = lang('Podcast.category_options.' . $category->code);
|
2023-06-21 10:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$podcast->categories = $categories;
|
|
|
|
|
|
|
|
return $podcast;
|
2022-06-22 10:48:58 +00:00
|
|
|
}
|
|
|
|
}
|