diff options
Diffstat (limited to 'lib/Controller/CommandController.php')
-rw-r--r-- | lib/Controller/CommandController.php | 99 |
1 files changed, 42 insertions, 57 deletions
diff --git a/lib/Controller/CommandController.php b/lib/Controller/CommandController.php index 60858a304..d4d42bcc0 100644 --- a/lib/Controller/CommandController.php +++ b/lib/Controller/CommandController.php @@ -23,8 +23,7 @@ namespace OCA\Spreed\Controller; -use OCA\Spreed\Model\Command; -use OCA\Spreed\Model\CommandMapper; +use OCA\Spreed\Service\CommandService; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -32,35 +31,39 @@ use OCP\AppFramework\OCSController; use OCP\IRequest; class CommandController extends OCSController { - /** @var CommandMapper */ - protected $commandMapper; + + + /** @var CommandService */ + protected $commandService; /** * @param string $appName * @param IRequest $request - * @param CommandMapper $commandMapper + * @param CommandService $commandService */ public function __construct($appName, IRequest $request, - CommandMapper $commandMapper) { + CommandService $commandService) { parent::__construct($appName, $request); - $this->commandMapper = $commandMapper; + $this->commandService = $commandService; } /** * @return DataResponse */ public function index(): DataResponse { - $commands = $this->commandMapper->findAll(); + $commands = $this->commandService->findAll(); $result = []; foreach ($commands as $command) { $result[] = [ 'id' => $command->getId(), + 'app' => $command->getApp(), 'name' => $command->getName(), - 'pattern' => $command->getPattern(), + 'pattern' => $command->getCommand(), 'script' => $command->getScript(), - 'output' => $command->getOutput(), + 'response' => $command->getResponse(), + 'enabled' => $command->getEnabled(), ]; } @@ -68,36 +71,28 @@ class CommandController extends OCSController { } /** + * @param string $cmd * @param string $name - * @param string $pattern * @param string $script - * @param int $output + * @param int $response + * @param int $enabled * @return DataResponse */ - public function create(string $name, string $pattern, string $script, int $output): DataResponse { - $command = new Command(); - - if (!\in_array($output, [Command::OUTPUT_NONE, Command::OUTPUT_USER, Command::OUTPUT_ALL], true)) { - return new DataResponse([], Http::STATUS_BAD_REQUEST); + public function create(string $cmd, string $name, string $script, int $response, int $enabled): DataResponse { + try { + $command = $this->commandService->create('', $name, $cmd, $script, $response, $enabled); + } catch (\InvalidArgumentException $e) { + return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - // FIXME Validate "bot name" - // FIXME Validate "pattern" - // FIXME Validate "script" - - $command->setName($name); - $command->setName($pattern); - $command->setName($script); - $command->setName($output); - - $this->commandMapper->insert($command); - return new DataResponse([ 'id' => $command->getId(), + 'app' => $command->getApp(), 'name' => $command->getName(), - 'pattern' => $command->getPattern(), + 'pattern' => $command->getCommand(), 'script' => $command->getScript(), - 'output' => $command->getOutput(), + 'response' => $command->getResponse(), + 'enabled' => $command->getEnabled(), ]); } @@ -107,56 +102,48 @@ class CommandController extends OCSController { */ public function show(int $id): DataResponse { try { - $command = $this->commandMapper->findById($id); + $command = $this->commandService->findById($id); } catch (DoesNotExistException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } return new DataResponse([ 'id' => $command->getId(), + 'app' => $command->getApp(), 'name' => $command->getName(), - 'pattern' => $command->getPattern(), + 'pattern' => $command->getCommand(), 'script' => $command->getScript(), - 'output' => $command->getOutput(), + 'response' => $command->getResponse(), + 'enabled' => $command->getEnabled(), ]); } /** * @param int $id + * @param string $cmd * @param string $name - * @param string $pattern * @param string $script - * @param int $output + * @param int $response + * @param int $enabled * @return DataResponse */ - public function update(int $id, string $name, string $pattern, string $script, int $output): DataResponse { + public function update(int $id, string $cmd, string $name, string $script, int $response, int $enabled): DataResponse { try { - $command = $this->commandMapper->findById($id); + $command = $this->commandService->update($id, $name, $cmd, $script, $response, $enabled); } catch (DoesNotExistException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); + } catch (\InvalidArgumentException $e) { + return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - if (!\in_array($output, [Command::OUTPUT_NONE, Command::OUTPUT_USER, Command::OUTPUT_ALL], true)) { - return new DataResponse([], Http::STATUS_BAD_REQUEST); - } - - // FIXME Validate "bot name" - // FIXME Validate "pattern" - // FIXME Validate "script" - - $command->setName($name); - $command->setName($pattern); - $command->setName($script); - $command->setName($output); - - $this->commandMapper->update($command); - return new DataResponse([ 'id' => $command->getId(), + 'app' => $command->getApp(), 'name' => $command->getName(), - 'pattern' => $command->getPattern(), + 'pattern' => $command->getCommand(), 'script' => $command->getScript(), - 'output' => $command->getOutput(), + 'response' => $command->getResponse(), + 'enabled' => $command->getEnabled(), ]); } @@ -166,13 +153,11 @@ class CommandController extends OCSController { */ public function destroy(int $id): DataResponse { try { - $command = $this->commandMapper->findById($id); + $this->commandService->delete($id); } catch (DoesNotExistException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } - $this->commandMapper->delete($command); - return new DataResponse(); } |