Skip to content
Snippets Groups Projects
ShowController.php 3.59 KiB
Newer Older
onny's avatar
onny committed
<?php

/**
onny's avatar
onny committed
 * Podcast App
onny's avatar
onny committed
 *
 * @author Jonas Heinrich
 * @copyright 2021 Jonas Heinrich <onny@project-insanity.org>
onny's avatar
onny committed
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

onny's avatar
onny committed
namespace OCA\Podcast\Controller;
onny's avatar
onny committed

onny's avatar
onny committed
use OCA\Podcast\AppInfo\Application;
use OCA\Podcast\Service\ShowService;
use OCA\Podcast\Service\FyydApiService;
onny's avatar
onny committed
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;

class ShowController extends Controller {
	/** @var ShowsService */
onny's avatar
onny committed
	private $service;

	/** @var FyydApiService */
	private $fyydapi;

onny's avatar
onny committed
	/** @var string */
	private $userId;

	use Errors;

	public function __construct(IRequest $request,
								ShowService $service,
								FyydApiService $fyydapi,
onny's avatar
onny committed
								$userId) {
		parent::__construct(Application::APP_ID, $request);
		$this->service = $service;
		$this->fyydapi = $fyydapi;
onny's avatar
onny committed
		$this->userId = $userId;
	}

	/**
	 * @NoAdminRequired
	 */
	public function index(int $podcast_id = null, string $category = null,
		int $count = 20, int $page = 0): DataResponse {

		$data = $this->service->findAll($this->userId);

		if ($category) {
			$list = $this->fyydapi->queryCategory($category, $count, $page);
			return new DataResponse($list);
		} else if ($podcast_id) {
			$list = $this->fyydapi->queryPodcast($podcast_id);

			foreach($data as $localEpisode) {
onny's avatar
onny committed
				if ($localEpisode->getId() === $list['data']['id']) {
						$list['data']['dateadded'] = $localEpisode->getDateadded();
				}
			}

			return new DataResponse($list);
		$data = array_slice($data, $page * $count, $count);

onny's avatar
onny committed
		if (count($data) === $count) {
			$nextPage = $page + 1;
		} else {
			$nextPage = null;
		};

		$response = [
			"meta" => [
				"paging" => [
					"next_page" => $nextPage,
				],
			],
			"data" => $data,
		];
		return new DataResponse($response);

onny's avatar
onny committed
	}

	/**
	 * @NoAdminRequired
	 */
	public function show(int $id): DataResponse {
		return $this->handleNotFound(function () use ($id) {
			return $this->service->find($id, $this->userId);
		});
	}

	/**
	 * @NoAdminRequired
	 */
	 public function create(int $id, string $smallImageURL, string $author,
	 	string $title, string $lastpub, int $dateadded, string $homepage,
		string $feedurl): DataResponse {
 		return new DataResponse($this->service->create($id, $smallImageURL, $author,
			$title, $lastpub, $dateadded, $homepage, $feedurl, $this->userId));
onny's avatar
onny committed

	/**
	 * @NoAdminRequired
	 */
	public function update(int $id, string $smallImageURL, string $author,
	 	string $title, string $lastpub, int $dateadded, string $homepage,
		string $feedurl): DataResponse {
		return $this->handleNotFound(function () use ($id, $smallImageURL, $author,
			$title, $lastpub, $dateadded, $homepage, $feedurl) {
			return $this->service->update($id, $smallImageURL, $author, $title, $lastpub,
				$dateadded, $homepage, $feedurl, $this->userId);
onny's avatar
onny committed

	/**
	 * @NoAdminRequired
	 */
	public function destroy(int $id): DataResponse {
		return $this->handleNotFound(function () use ($id) {
			return $this->service->delete($id, $this->userId);
		});
	}
}