Skip to content
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
[#383](https://github.com/nextcloud/cookbook/pull/383/) @christianlupus
- Unit tests for JSON object service
[#387](https://github.com/nextcloud/cookbook/pull/387) @TobiasMie
- Rating recipes for nextcloud users
[#342](https://github.com/nextcloud/cookbook/pull/342/) @sam-19

### Changed
- Switch of project ownership to neextcloud organization in GitHub
Expand Down
3 changes: 3 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
['name' => 'main#category', 'url' => '/api/category/{category}', 'verb' => 'GET'],
['name' => 'main#tags', 'url' => '/api/tags/{keywords}', 'verb' => 'GET'],
['name' => 'main#search', 'url' => '/api/search/{query}', 'verb' => 'GET'],
/* Rating routes */
['name' => 'rating#save', 'url' => '/api/v1/recipes/{id}/rating', 'verb' => 'POST', 'requirements' => ['id' => '\d+']],
['name' => 'rating#remove', 'url' => '/api/v1/recipes/{id}/rating', 'verb' => 'DELETE', 'requirements' => ['id' => '\d+']],
],

/* API resources */
Expand Down
71 changes: 71 additions & 0 deletions lib/Controller/RatingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace OCA\Cookbook\Controller;

use OCA\Cookbook\Service\DbCacheService;
use OCA\Cookbook\Service\RatingService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;

/**
* A controller that allows altering of the ratings of a recipe
*
* @author Christian Wolf <[email protected]>
*
*/
class RatingController extends Controller
{

/**
* @var DbCacheService
*/
private $dbCacheService;

/**
* @var RatingService
*/
private $ratingService;

/**
* @var string
*/
private $userId;

public function __construct(
?string $UserId, DbCacheService $dbCacheService, RatingService $ratingService)
{
$this->dbCacheService = $dbCacheService;
$this->ratingService = $ratingService;
$this->userId = $UserId;
}

/**
* Add or update a rating for the current user to a recipe
* @param int $id The id of the recipe to be rated
*/
public function save($id)
{
$this->dbCacheService->triggerCheck();

$data = [];
parse_str(file_get_contents('php://input'), $data);

$this->ratingService->addRating((int) $id, $this->userId, $data);

return new DataResponse([], Http::STATUS_OK, ['Content-Type' => 'application/json']);
}

/**
* Remove a rating from a recipe for the currently logged in user
* @param int $id The id of the recipe to be altered
*/
public function remove($id)
{
$this->dbCacheService->triggerCheck();
$this->ratingService->removeRating($id, $this->userId);

return new DataResponse([], Http::STATUS_OK, ['Content-Type' => 'application/json']);
}

}
53 changes: 53 additions & 0 deletions lib/Migration/Version000000Date20201114165448.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace OCA\Cookbook\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* Create a table to store the aggregate ratings in the database
*/
class Version000000Date20201114165448 extends SimpleMigrationStep {

private const RATINGS = 'cookbook_ratings';

/**
* @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();

$tableRatings = $schema->createTable(self::RATINGS);

// Set up columns
$tableRatings->addColumn('recipe_id', 'integer', [
'notnull' => true
]);
$tableRatings->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64
]);
$tableRatings->addColumn('rating', 'float', [
'notnull' => true
]);

// Set up indices
$tableRatings->addIndex(['recipe_id', 'user_id']);
// XXX ForeignKeyConstraints

return $schema;
}

}
Loading