trying to implement database backend

This commit is contained in:
Jonas Heinrich 2020-10-31 13:14:24 +01:00
parent e174e3d16a
commit 5546a3f3bd
8 changed files with 68 additions and 154 deletions

View File

@ -1,2 +0,0 @@
ARG NEXTCLOUD_VERSION=20.0.0
FROM rootlogin/nextcloud

View File

@ -37,7 +37,7 @@ Mount or move the ``radio`` folder into your Nextcloud ``apps/`` directory. Go t
Can be easily tested using Docker:
```
docker build -t nextcloud .
docker build -t nextcloud https://git.project-insanity.org/onny/docker-nextcloud.git
docker run -v /tmp/nextcloud-app-radio:/opt/nextcloud/apps/radio -d --name nextcloud-app-radio -p 80:80 nextcloud
```
First part of -v is the path to the cloned and compiled or downloaded Nextcloud Radio app. Debug running container it with:

View File

@ -14,7 +14,7 @@
<bugs>https://git.project-insanity.org/onny/nextcloud-app-radio/issues</bugs>
<screenshot>https://git.project-insanity.org/onny/nextcloud-app-radio/raw/master/screenshot.png</screenshot>
<dependencies>
<nextcloud min-version="19" max-version="20"/>
<nextcloud min-version="20" max-version="20"/>
</dependencies>
<commands>
<command>OCA\Radio\Command\Search</command>

View File

@ -1,51 +0,0 @@
<?php
declare(strict_types=1);
namespace OCA\Radio\Command;
use OCA\Radio\Service\SearchService;
use OCA\Mail\Service\CleanupService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Search extends Command {
/** @var CleanupService */
private $cleanupService;
/**
* @var SearchService
*/
private $searchService;
public function __construct(SearchService $searchService) {
parent::__construct();
$this->searchService = $searchService;
}
/**
* @return void
*/
protected function configure() {
$this->setName('radio:search');
$this->setDescription('Search for radio stations');
$this->addArgument("term", InputArgument::OPTIONAL);
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$radioStations = $this->searchService->findRadioStations(
$input->getArgument("term")
);
$output->writeln("Found radio stations");
foreach ($radioStations as $radioStation) {
$output->writeln("* $radioStation");
}
return 0;
}
}

View File

@ -15,8 +15,6 @@ class StationController extends Controller {
/** @var string */
private $userId;
use Errors;
public function __construct(IRequest $request,
StationService $service,
$userId) {

View File

@ -1,59 +0,0 @@
<?php
declare(strict_types=1);
namespace OCA\Radio\Service;
use Exception;
use OCP\Http\Client\IClientService;
use Psr\Log\LoggerInterface;
use function array_filter;
use function array_map;
use function json_decode;
use function mb_strpos;
use function mb_strtolower;
class SearchService {
/** @var IClientService */
private $clientService;
/** @var LoggerInterface */
private $logger;
public function __construct(IClientService $clientService,
LoggerInterface $logger) {
$this->clientService = $clientService;
$this->logger = $logger;
}
public function findRadioStations(?string $term = null): array {
$this->logger->debug('searching radio stations');
$client = $this->clientService->newClient();
try {
$response = $client->get("https://cat-fact.herokuapp.com/facts?animal_type=cat");
} catch (Exception $e) {
$this->logger->error("Could not search for radio stations. Please check connection to radio-browser API.");
throw $e;
}
$body = $response->getBody();
$parsed = json_decode($body, true);
$mapped = array_map(function(array $radioStation) {
return $radioStation['text'];
}, $parsed['all']);
if (empty($term)) {
return $mapped;
}
return array_filter($mapped, function(string $radioStation) use ($term) {
return mb_strpos(mb_strtolower($radioStation), mb_strtolower($term));
});
}
}

View File

@ -1,48 +1,76 @@
<?php
declare(strict_types=1);
namespace OCA\Radio\Service;
namespace OCA\Radio\Migration;
use Exception;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
class Version000000Date20181013124731 extends SimpleMigrationStep {
use OCA\Radio\Db\Station;
use OCA\Radio\Db\StationMapper;
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
class StationService {
if (!$schema->hasTable('radio')) {
$table = $schema->createTable('radio');
$table->addColumn('id', 'integer', [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('title', 'string', [
'notnull' => true,
'length' => 200
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 200,
]);
$table->addColumn('content', 'text', [
'notnull' => true,
'default' => ''
]);
/** @var StationMapper */
private $mapper;
$table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'radio_user_id_index');
public function __construct(StationMapper $mapper) {
$this->mapper = $mapper;
}
public function findAll(string $userId): array {
return $this->mapper->findAll($userId);
}
private function handleException(Exception $e): void {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new StationNotFound($e->getMessage());
} else {
throw $e;
}
}
public function find($id, $userId) {
try {
return $this->mapper->find($id, $userId);
// in order to be able to plug in different storage backends like files
// for instance it is a good idea to turn storage related exceptions
// into service related exceptions so controllers and service users
// have to deal with only one type of exception
} catch (Exception $e) {
$this->handleException($e);
}
}
public function create($title, $content, $userId) {
$station = new Station();
$station->setTitle($title);
$station->setContent($content);
$station->setUserId($userId);
return $this->mapper->insert($station);
}
public function update($id, $title, $content, $userId) {
try {
$station = $this->mapper->find($id, $userId);
$station->setTitle($title);
$station->setContent($content);
return $this->mapper->update($station);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function delete($id, $userId) {
try {
$station = $this->mapper->find($id, $userId);
$this->mapper->delete($station);
return $station;
} catch (Exception $e) {
$this->handleException($e);
}
return $schema;
}
}

View File

@ -98,7 +98,7 @@ export default {
title: 'test',
content: 'testhallo',
}
const response = await axios.post(generateUrl('/apps/radio/station'), station)
const response = await axios.post(generateUrl('/apps/radio/stations'), station)
console.log(response)
// const index = this.stations.findIndex((match) => match.id === this.currentStationId)
// this.$set(this.stations, index, response.data)