Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add restful API #458

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
['name' => 'recommendation#index', 'url' => '/api/recommendations', 'verb' => 'GET'],
['name' => 'recommendation#always', 'url' => '/api/recommendations/always', 'verb' => 'GET'],
],
'ocs' => [
['name' => 'recommendation_api#index', 'url' => '/api/v1/recommendations', 'verb' => 'GET'],
['name' => 'recommendation_api#always', 'url' => '/api/v1/recommendations/always', 'verb' => 'GET'],
],
];
91 changes: 91 additions & 0 deletions lib/Controller/RecommendationApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

/**
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Recommendations\Controller;

use OCA\Recommendations\AppInfo\Application;
use OCA\Recommendations\Service\RecommendationService;
use OCP\AppFramework\OCSController;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserManager;

class RecommendationApiController extends OCSController {

/** @var IConfig */
private $config;

/** @var IUserManager */
private $userManager;

/** @var RecommendationService */
private $service;
Comment on lines +34 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @var IConfig */
private $config;
/** @var IUserManager */
private $userManager;
/** @var RecommendationService */
private $service;
private IConfig $config;
private IUserManager $userManager;
private RecommendationService $service;
private ?string $userId;


public function __construct($appName, IRequest $request, IConfig $config,
IUserManager $userManager,
RecommendationService $service, $userId) {
Comment on lines +44 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function __construct($appName, IRequest $request, IConfig $config,
IUserManager $userManager,
RecommendationService $service, $userId) {
public function __construct(string $appName, IRequest $request, IConfig $config,
IUserManager $userManager,
RecommendationService $service, ?string $userId) {

parent::__construct($appName, $request, 'GET');
$this->config = $config;
$this->service = $service;
$this->userManager = $userManager;
$this->userId = $userId;
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
/**
* Get the recommendations for the user if the recommendations are enabled for the user

* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function index(): DataResponse {
$user = $this->userManager->get($this->userId);

$response = [];
$response['enabled'] = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true';

if ($response['enabled']) {
$response['recommendations'] = $this->service->getRecommendations($user);
}

return new DataResponse(
$response
);
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
/**
* Get the recommendations for the user even if the recommendations are disablde for the user

* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function always(): DataResponse {
$user = $this->userManager->get($this->userId);

$response = [
'enabled' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true',
'recommendations' => $this->service->getRecommendations($user),
];

return new DataResponse(
$response
);
}
}