add many2many relation between socialIssue and accompanying period + api endpoint

This commit is contained in:
Julien Fastré 2021-05-18 13:48:11 +02:00
parent a6d6a962cd
commit eaac97221f
10 changed files with 260 additions and 13 deletions

View File

@ -358,6 +358,27 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
]
],
]
],
[
'class' => \Chill\PersonBundle\Entity\SocialWork\SocialIssue::class,
'name' => 'social_work_social_issue',
'base_path' => '/api/1.0/person/social-work/social-issue',
// 'controller' => \Chill\PersonBundle\Controller\OpeningApiController::class,
'base_role' => 'ROLE_USER',
'actions' => [
'_index' => [
'methods' => [
Request::METHOD_GET => true,
Request::METHOD_HEAD => true
],
],
'_entity' => [
'methods' => [
Request::METHOD_GET => true,
Request::METHOD_HEAD => true
]
],
]
]
]
]);

View File

@ -27,6 +27,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
@ -215,6 +216,16 @@ class AccompanyingPeriod
*/
private $resources;
/**
* @ORM\ManyToMany(
* targetEntity=SocialIssue::class
* )
* @ORM\JoinTable(
* name="chill_person_accompanying_period_social_issues"
* )
*/
private Collection $socialIssues;
/**
* AccompanyingPeriod constructor.
*
@ -225,6 +236,7 @@ class AccompanyingPeriod
$this->setOpeningDate($dateOpening);
$this->participations = new ArrayCollection();
$this->scopes = new ArrayCollection();
$this->socialIssues = new ArrayCollection();
}
/**
@ -648,6 +660,23 @@ class AccompanyingPeriod
$this->resources->removeElement($resource);
}
public function getSocialIssues(): Collection
{
return $this->socialIssues;
}
public function addSocialIssues(SocialIssue $socialIssue): self
{
$this->socialIssues[] = $socialIssue;
return $this;
}
public function removeSocialIssue(SocialIssue $socialissue): void
{
$this->socialIssues->removeElement($socialIssue);
}
/**
* Get a list of all persons which are participating to this course
*/

View File

@ -6,10 +6,15 @@ use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
/**
* @ORM\Entity(repositoryClass=SocialIssueRepository::class)
* @ORM\Table(name="chill_person_social_issue")
* @DiscriminatorMap(typeProperty="type", mapping={
* "social_issue"=SocialIssue::class
* })
*/
class SocialIssue
{
@ -37,6 +42,7 @@ class SocialIssue
/**
* @ORM\Column(type="json")
* @Groups({"read"})
*/
private $title = [];
@ -61,6 +67,11 @@ class SocialIssue
return $this->parent;
}
public function hasParent(): bool
{
return $this->parent !== null;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;

View File

@ -0,0 +1,43 @@
<?php
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
class SocialIssueNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
private SocialIssueRender $render;
use NormalizerAwareTrait;
/**
* @param SocialIssueRender $render
*/
public function __construct(SocialIssueRender $render)
{
$this->render = $render;
}
public function normalize($socialIssue, string $format = null, array $context = [])
{
/** @var SocialIssue $socialIssue */
return [
'type' => 'social_issue',
'id' => $socialIssue->getId(),
'parent_id' => $socialIssue->hasParent() ? $socialIssue->getParent()->getId() : null,
'children_ids' => $socialIssue->getChildren()->map(function (SocialIssue $si) { return $si->getId(); }),
'title' => $socialIssue->getTitle(),
'text' => $this->render->renderString($socialIssue, [])
];
}
public function supportsNormalization($data, string $format = null): bool
{
return $data instanceof SocialIssue;
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Chill\PersonBundle\Templating\Entity;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderInterface;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\MainBundle\Templating\TranslatableStringHelper;
class SocialIssueRender implements ChillEntityRenderInterface
{
private TranslatableStringHelper $translatableStringHelper;
public const SEPARATOR_KEY = 'default.separator';
public const DEFAULT_ARGS = [
self::SEPARATOR_KEY => ' > ',
];
public function __construct(TranslatableStringHelper $translatableStringHelper)
{
$this->translatableStringHelper = $translatableStringHelper;
}
public function supports($entity, array $options): bool
{
return $entity instanceof SocialIssueRender;
}
public function renderString($socialIssue, array $options): string
{
/** @var $socialIssue SocialIssue */
$options = \array_merge(self::DEFAULT_ARGS, $options);
$str = $this->translatableStringHelper->localize($socialIssue->getTitle());
while ($socialIssue->hasParent()) {
$socialIssue = $socialIssue->getParent();
$str .= $options[self::SEPARATOR_KEY].$this->translatableStringHelper->localize(
$socialIssue->getTitle()
);
}
return $str;
}
public function renderBox($entity, array $options): string
{
return "renderBox not implemented for social issue";
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Chill\PersonBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use Chill\MainBundle\Test\PrepareClientTrait;
class SocialIssueApiControllerTest extends WebTestCase
{
use PrepareClientTrait;
public function setUp()
{
parent::setUp();
self::bootKernel();
}
public function testList(): array
{
$client = $this->getClientAuthenticated();
$client->request(Request::METHOD_GET, '/api/1.0/person/social-work/social-issue.json');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$data = \json_decode($client->getResponse()->getContent(), true);
$this->assertGreaterThan(0, $data['count']);
$this->assertGreaterThan(0, count($data['results']));
return $data;
}
/**
* @depends testList
*/
public function testItem(array $data): void
{
$socialIssues = $data['results'];
shuffle($socialIssues);
$socialIssue = \array_pop($socialIssues);
$client = $this->getClientAuthenticated();
$client->request(Request::METHOD_GET, sprintf('/api/1.0/person/social-work/social-issue/%d.json', $socialIssue['id']));
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$data = \json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('id', $data);
$this->assertArrayHasKey('type', $data);
}
}

View File

@ -28,3 +28,8 @@ services:
arguments:
- '@Doctrine\Persistence\ManagerRegistry'
tags: [ doctrine.repository_service ]
Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository:
arguments:
- '@Doctrine\Persistence\ManagerRegistry'
tags: [ doctrine.repository_service ]

View File

@ -1,15 +1,8 @@
---
services:
Chill\PersonBundle\Serializer\Normalizer\PersonNormalizer:
arguments:
$repository: '@Chill\PersonBundle\Repository\PersonRepository'
Chill\PersonBundle\Serializer\Normalizer\:
autowire: true
resource: '../../Serializer/Normalizer'
tags:
- { name: 'serializer.normalizer', priority: 64 }
Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodNormalizer:
tags:
- { name: 'serializer.normalizer', priority: 64 }
Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodParticipationNormalizer:
tags:
- { name: 'serializer.normalizer', priority: 64 }

View File

@ -1,12 +1,19 @@
services:
Chill\PersonBundle\Templating\Entity\:
resource: '../../Templating/Entity'
tags:
- 'chill.render_entity'
Chill\PersonBundle\Templating\Entity\PersonRender:
arguments:
$configAltNamesHelper: '@Chill\PersonBundle\Config\ConfigPersonAltNamesHelper'
tags:
- 'chill.render_entity'
Chill\PersonBundle\Templating\Entity\ClosingMotiveRender:
arguments:
$translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper'
tags:
- 'chill.render_entity'
Chill\PersonBundle\Templating\Entity\SocialIssueRender:
arguments:
$translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper'

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Add a link between social issues and accompanying periods
*/
final class Version20210518075908 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add a link between social issue and accompanying period';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE chill_person_accompanying_period_social_issues (accompanyingperiod_id INT NOT NULL, socialissue_id INT NOT NULL, PRIMARY KEY(accompanyingperiod_id, socialissue_id))');
$this->addSql('CREATE INDEX IDX_CAFE078F550B0C53 ON chill_person_accompanying_period_social_issues (accompanyingperiod_id)');
$this->addSql('CREATE INDEX IDX_CAFE078FA549916C ON chill_person_accompanying_period_social_issues (socialissue_id)');
$this->addSql('ALTER TABLE chill_person_accompanying_period_social_issues ADD CONSTRAINT FK_CAFE078F550B0C53 FOREIGN KEY (accompanyingperiod_id) REFERENCES chill_person_accompanying_period (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_accompanying_period_social_issues ADD CONSTRAINT FK_CAFE078FA549916C FOREIGN KEY (socialissue_id) REFERENCES chill_person_social_issue (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema): void
{
$this->addSql('DROP TABLE chill_person_accompanying_period_social_issues');
}
}