added internal rates (#1591)

This commit is contained in:
Kevin Papst
2020-04-08 14:50:15 +02:00
committed by GitHub
parent 82c713031e
commit 68d703d1e3
128 changed files with 4044 additions and 1571 deletions

View File

@@ -12,8 +12,11 @@ declare(strict_types=1);
namespace App\API;
use App\Entity\Activity;
use App\Entity\ActivityRate;
use App\Event\ActivityMetaDefinitionEvent;
use App\Form\API\ActivityApiEditForm;
use App\Form\API\ActivityRateApiForm;
use App\Repository\ActivityRateRepository;
use App\Repository\ActivityRepository;
use App\Repository\Query\ActivityQuery;
use App\Utils\SearchTerm;
@@ -32,6 +35,7 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* @RouteResource("Activity")
* @SWG\Tag(name="Activity")
*
* @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')")
*/
@@ -49,12 +53,17 @@ class ActivityController extends BaseApiController
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var ActivityRateRepository
*/
private $activityRateRepository;
public function __construct(ViewHandlerInterface $viewHandler, ActivityRepository $repository, EventDispatcherInterface $dispatcher)
public function __construct(ViewHandlerInterface $viewHandler, ActivityRepository $repository, EventDispatcherInterface $dispatcher, ActivityRateRepository $activityRateRepository)
{
$this->viewHandler = $viewHandler;
$this->repository = $repository;
$this->dispatcher = $dispatcher;
$this->activityRateRepository = $activityRateRepository;
}
/**
@@ -276,7 +285,7 @@ class ActivityController extends BaseApiController
}
/**
* Sets the value of a meta-field for an existing activity.
* Sets the value of a meta-field for an existing activity
*
* @SWG\Response(
* response=200,
@@ -327,4 +336,166 @@ class ActivityController extends BaseApiController
return $this->viewHandler->handle($view);
}
/**
* Returns a collection of all rates for one activity
*
* @SWG\Response(
* response=200,
* description="Returns a collection of activity rate entities",
* @SWG\Schema(
* type="array",
* @SWG\Items(ref="#/definitions/ActivityRate")
* )
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
* description="The activity whose rates will be returned",
* required=true,
* )
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/
public function getRatesAction(int $id): Response
{
/** @var Activity|null $activity */
$activity = $this->repository->find($id);
if (null === $activity) {
throw new NotFoundException();
}
if (!$this->isGranted('edit', $activity)) {
throw new AccessDeniedHttpException('Access denied.');
}
$rates = $this->activityRateRepository->getRatesForActivity($activity);
$view = new View($rates, 200);
$view->getContext()->setGroups(['Default', 'Entity', 'ActivityRate']);
return $this->viewHandler->handle($view);
}
/**
* Deletes one rate for an activity
*
* @SWG\Delete(
* @SWG\Response(
* response=204,
* description="Returns no content: 204 on successful delete"
* )
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
* description="The activity whose rate will be removed",
* required=true,
* )
* @SWG\Parameter(
* name="rateId",
* in="path",
* type="integer",
* description="The rate to remove",
* required=true,
* )
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/
public function deleteRateAction(string $id, string $rateId): Response
{
/** @var Activity|null $activity */
$activity = $this->repository->find($id);
if (null === $activity) {
throw new NotFoundException();
}
if (!$this->isGranted('edit', $activity)) {
throw new AccessDeniedHttpException('Access denied.');
}
/** @var ActivityRate|null $rate */
$rate = $this->activityRateRepository->find($rateId);
if (null === $rate || $rate->getActivity() !== $activity) {
throw new NotFoundException();
}
$this->activityRateRepository->deleteRate($rate);
$view = new View(null, Response::HTTP_NO_CONTENT);
return $this->viewHandler->handle($view);
}
/**
* Adds a new rate to an activity
*
* @SWG\Post(
* @SWG\Response(
* response=200,
* description="Returns the new created rate",
* @SWG\Schema(ref="#/definitions/ActivityRate")
* )
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
* description="The activity to add the rate for",
* required=true,
* )
* @SWG\Parameter(
* name="body",
* in="body",
* required=true,
* @SWG\Schema(ref="#/definitions/ActivityRateForm")
* )
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/
public function postRateAction(int $id, Request $request): Response
{
/** @var Activity|null $activity */
$activity = $this->repository->find($id);
if (null === $activity) {
throw new NotFoundException();
}
if (!$this->isGranted('edit', $activity)) {
throw new AccessDeniedHttpException('Access denied.');
}
$rate = new ActivityRate();
$rate->setActivity($activity);
$form = $this->createForm(ActivityRateApiForm::class, $rate, [
'method' => 'POST',
]);
$form->setData($rate);
$form->submit($request->request->all(), false);
if (false === $form->isValid()) {
$view = new View($form, Response::HTTP_OK);
$view->getContext()->setGroups(['Default', 'Entity', 'ActivityRate']);
return $this->viewHandler->handle($view);
}
$this->activityRateRepository->saveRate($rate);
$view = new View($rate, Response::HTTP_OK);
$view->getContext()->setGroups(['Default', 'Entity', 'ActivityRate']);
return $this->viewHandler->handle($view);
}
}