Skip to content

Commit

Permalink
Merge pull request #6026 from pmattmann/feature/checklist-overview-draft
Browse files Browse the repository at this point in the history
Checklist-Overview with Activities
  • Loading branch information
pmattmann authored Oct 15, 2024
2 parents 5f82d7d + b76d3f1 commit 0284a2c
Show file tree
Hide file tree
Showing 22 changed files with 705 additions and 25 deletions.
85 changes: 85 additions & 0 deletions api/src/Doctrine/Filter/ContentNodeCampFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Doctrine\Filter;

use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation;
use App\Entity\Activity;
use App\Entity\ContentNode;
use App\Repository\FiltersByCampCollaboration;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

final class ContentNodeCampFilter extends AbstractFilter {
use FiltersByCampCollaboration;

public const CAMP_QUERY_NAME = 'camp';

public function __construct(
private IriConverterInterface $iriConverter,
ManagerRegistry $managerRegistry,
?LoggerInterface $logger = null,
?array $properties = null,
?NameConverterInterface $nameConverter = null
) {
parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
}

// This function is only used to hook in documentation generators (supported by Swagger and Hydra)
public function getDescription(string $resourceClass): array {
$description = [];
$description['camp'] = [
'property' => self::CAMP_QUERY_NAME,
'type' => Type::BUILTIN_TYPE_STRING,
'required' => false,
];

return $description;
}

protected function filterProperty(
string $property,
$value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
?Operation $operation = null,
array $context = []
): void {
if (ContentNode::class !== $resourceClass && !is_subclass_of($resourceClass, ContentNode::class)) {
throw new \Exception("ContentNodeCampFilter can only be applied to entities of type ContentNode (received: {$resourceClass}).");
}

if (self::CAMP_QUERY_NAME !== $property) {
return;
}

// load camp from query parameter value
$camp = $this->iriConverter->getResourceFromIri($value);

// generate alias to avoid interference with other filters
$campParameterName = $queryNameGenerator->generateParameterName($property);
$periodJoinAlias = $queryNameGenerator->generateJoinAlias('period');
$activityJoinAlias = $queryNameGenerator->generateJoinAlias('activity');
$scheduleEntryJoinAlias = $queryNameGenerator->generateJoinAlias('scheduleEntry');

$rootAlias = $queryBuilder->getRootAliases()[0];

$rootQry = $queryBuilder->getEntityManager()->createQueryBuilder();
$rootQry
->select("identity({$activityJoinAlias}.rootContentNode)")
->from(Activity::class, $activityJoinAlias)
->join("{$activityJoinAlias}.scheduleEntries", $scheduleEntryJoinAlias)
->join("{$scheduleEntryJoinAlias}.period", $periodJoinAlias)
->where($queryBuilder->expr()->eq("{$periodJoinAlias}.camp", ":{$campParameterName}"))
;

$queryBuilder->andWhere($queryBuilder->expr()->in("{$rootAlias}.root", $rootQry->getDQL()));
$queryBuilder->setParameter($campParameterName, $camp);
}
}
2 changes: 1 addition & 1 deletion api/src/Doctrine/Filter/ContentNodePeriodFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function filterProperty(
?Operation $operation = null,
array $context = []
): void {
if (ContentNode::class !== $resourceClass) {
if (ContentNode::class !== $resourceClass && !is_subclass_of($resourceClass, ContentNode::class)) {
throw new \Exception("ContentNodePeriodFilter can only be applied to entities of type ContentNode (received: {$resourceClass}).");
}

Expand Down
2 changes: 2 additions & 0 deletions api/src/Entity/ChecklistItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class ChecklistItem extends BaseEntity implements BelongsToCampInterface, CopyFr
/**
* All ChecklistNodes that have selected this ChecklistItem.
*/
#[ApiProperty(example: '["/checklist_items/1a2b3c4d"]')]
#[Groups(['read'])]
#[Assert\Count(
exactly: 0,
exactMessage: 'It\'s not possible to delete a checklist item as long as checklist nodes are referencing it.',
Expand Down
2 changes: 2 additions & 0 deletions api/src/Entity/ContentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use App\Doctrine\Filter\ContentNodeCampFilter;
use App\Doctrine\Filter\ContentNodePeriodFilter;
use App\Entity\ContentNode\ColumnLayout;
use App\InputFilter;
Expand Down Expand Up @@ -44,6 +45,7 @@
order: ['root.id', 'parent.id', 'slot', 'position']
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['contentType', 'root'])]
#[ApiFilter(filterClass: ContentNodeCampFilter::class)]
#[ApiFilter(filterClass: ContentNodePeriodFilter::class)]
#[ORM\Entity(repositoryClass: ContentNodeRepository::class)]
#[ORM\InheritanceType('SINGLE_TABLE')]
Expand Down
1 change: 0 additions & 1 deletion api/src/Entity/ContentNode/ChecklistNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Entity\Checklist;
use App\Entity\ChecklistItem;
use App\Entity\ContentNode;
use App\Repository\ChecklistNodeRepository;
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
normalizationContext: ['groups' => ['read']],
order: ['name' => 'ASC']
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['categories'])]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['name', 'categories'])]
#[ORM\Entity]
class ContentType extends BaseEntity {
/**
Expand Down
2 changes: 1 addition & 1 deletion api/tests/Api/ChecklistItems/CreateChecklistItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function getExampleReadPayload($attributes = [], $except = []) {
ChecklistItem::class,
Get::class,
$attributes,
['parent', 'checklist'],
['parent', 'checklist', 'checklistNodes'],
$except
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"checklist": {
"href": "escaped_value"
},
"checklistNodes": [],
"children": [],
"parent": "escaped_value",
"self": {
Expand All @@ -21,6 +22,7 @@
"checklist": {
"href": "escaped_value"
},
"checklistNodes": [],
"children": [],
"parent": "escaped_value",
"self": {
Expand All @@ -36,8 +38,11 @@
"checklist": {
"href": "escaped_value"
},
"checklistNodes": [],
"children": [],
"parent": "escaped_value",
"parent": {
"href": "escaped_value"
},
"self": {
"href": "escaped_value"
}
Expand All @@ -51,10 +56,13 @@
"checklist": {
"href": "escaped_value"
},
"children": [],
"parent": {
"href": "escaped_value"
},
"checklistNodes": [],
"children": [
{
"href": "escaped_value"
}
],
"parent": "escaped_value",
"self": {
"href": "escaped_value"
}
Expand All @@ -68,12 +76,15 @@
"checklist": {
"href": "escaped_value"
},
"checklistNodes": [],
"children": [
{
"href": "escaped_value"
}
],
"parent": "escaped_value",
"parent": {
"href": "escaped_value"
},
"self": {
"href": "escaped_value"
}
Expand All @@ -87,14 +98,13 @@
"checklist": {
"href": "escaped_value"
},
"children": [
"checklistNodes": [
{
"href": "escaped_value"
}
],
"parent": {
"href": "escaped_value"
},
"children": [],
"parent": "escaped_value",
"self": {
"href": "escaped_value"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"checklist": {
"href": "escaped_value"
},
"checklistNodes": [
{
"href": "escaped_value"
}
],
"children": [],
"parent": "escaped_value",
"self": {
Expand Down
Loading

0 comments on commit 0284a2c

Please sign in to comment.