Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

bye bye comment identifiers #109

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Taskfile.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tasks:
cmds:
- 'php bin/console doctrine:schema:drop --force --full-database'
- 'php bin/console doctrine:schema:create'
- task: fake_migrations
- 'php bin/console doctrine:fixtures:load --append'

install:
Expand Down
47 changes: 47 additions & 0 deletions migrations/Version20231018195325.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231018195325 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE paragraph_statement (id INT AUTO_INCREMENT NOT NULL, paragraph_id INT NOT NULL, statement_id INT NOT NULL, thread_id INT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_2C3504148B50597F (paragraph_id), INDEX IDX_2C350414849CB65B (statement_id), UNIQUE INDEX UNIQ_2C350414E2904019 (thread_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE paragraph_statement ADD CONSTRAINT FK_2C3504148B50597F FOREIGN KEY (paragraph_id) REFERENCES paragraph (id)');
$this->addSql('ALTER TABLE paragraph_statement ADD CONSTRAINT FK_2C350414849CB65B FOREIGN KEY (statement_id) REFERENCES statement (id)');
$this->addSql('ALTER TABLE paragraph_statement ADD CONSTRAINT FK_2C350414E2904019 FOREIGN KEY (thread_id) REFERENCES thread (id)');
$this->addSql('ALTER TABLE discussion CHANGE thread_id thread_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE modification_statement ADD thread_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE modification_statement ADD CONSTRAINT FK_BF8C104CE2904019 FOREIGN KEY (thread_id) REFERENCES thread (id)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_BF8C104CE2904019 ON modification_statement (thread_id)');
$this->addSql('ALTER TABLE thread DROP COLUMN identifier');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE paragraph_statement DROP FOREIGN KEY FK_2C3504148B50597F');
$this->addSql('ALTER TABLE paragraph_statement DROP FOREIGN KEY FK_2C350414849CB65B');
$this->addSql('ALTER TABLE paragraph_statement DROP FOREIGN KEY FK_2C350414E2904019');
$this->addSql('DROP TABLE paragraph_statement');
$this->addSql('ALTER TABLE modification_statement DROP FOREIGN KEY FK_BF8C104CE2904019');
$this->addSql('DROP INDEX UNIQ_BF8C104CE2904019 ON modification_statement');
$this->addSql('ALTER TABLE modification_statement DROP thread_id');
$this->addSql('ALTER TABLE discussion CHANGE thread_id thread_id INT NOT NULL');
$this->addSql('ALTER TABLE thread ADD COLUMN identifier varchar(255)');
}
}
14 changes: 2 additions & 12 deletions src/Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,17 @@ public function show(Modification $modification, ThreadRepository $threadReposit
]);
}

#[Route('/add/{identifier}/{parentId}', name: 'app_comment_add', methods: ['GET', 'POST'])]
#[Route('/add/{id}/{parentId}', name: 'app_comment_add', methods: ['GET', 'POST'])]
public function add(
CommentRepository $commentRepository,
ThreadRepository $threadRepository,
EntityManagerInterface $entityManager,
Request $request,
string $identifier,
Thread $thread,
int $parentId = null
): Response {
if (!$this->getUser()) {
return $this->redirectToRoute('app_login');
}

$thread = $threadRepository->findOneBy(['identifier' => $identifier]);

if (!$thread) {
$thread = new Thread();
$thread->setIdentifier($identifier);
$entityManager->persist($thread);
}

$parentComment = $commentRepository->findOneBy(['id' => $parentId]);

$comment = new Comment();
Expand Down
5 changes: 0 additions & 5 deletions src/Controller/DiscussionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Uid\Uuid;

#[Route('/discussion')]
class DiscussionController extends AbstractController
Expand All @@ -34,10 +33,6 @@ public function add(Consultation $consultation, DiscussionRepository $discussion
if ($form->isSubmitted() && $form->isValid()) {
$thread = new Thread();

$uuid = Uuid::v4();
$thread->setIdentifier('consultation-'.$consultation->getId().'-discussion-'.$uuid);
$entityManager->persist($thread);

$discussion = $form->getData();
$discussion->setConsultation($consultation);
$discussion->setThread($thread);
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function serveFile(
$textRun->addText('</w:t><w:br/><w:t xml:space="preserve">');

if ($comments !== 1) {
$thread[$i] = $threadRepository->findOneBy(['identifier' => 'statement-'.$statement->getId().'-modification-'.$paragraphs[$i]['chosen']['modification']->getId()]);
$thread[$i] = $threadRepository->findOneBy(['statement' => $statement, 'modification' => $paragraphs[$i]['chosen']['modification']]);

foreach ($thread[$i]->getComments() as $j => $comment) {
if ($comments === 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
use App\Entity\ModificationStatement;
use App\Entity\Organisation;
use App\Entity\Paragraph;
use App\Entity\ParagraphStatement;
use App\Entity\Statement;
use App\Entity\Thread;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Uid\Uuid;

class ConsultationStimmUndWahlrecht16JaehrigeFixtures extends Fixture implements FixtureGroupInterface, DependentFixtureInterface
{
Expand Down Expand Up @@ -85,7 +85,6 @@ public function load(ObjectManager $manager): void
$manager->persist($document2);

$discussionThread = new Thread();
$discussionThread->setIdentifier('consultation-'.$consultation->getId().'-discussion-'.Uuid::v4());

$manager->persist($discussionThread);

Expand Down Expand Up @@ -238,11 +237,15 @@ public function load(ObjectManager $manager): void
$modification->setJustification('Ich finde das so besser, da es inklusiver und schwammiger formuliert ist.');
$manager->persist($modification);

$thread = new Thread();
$manager->persist($thread);

$modificationStatement1 = new ModificationStatement();
$modificationStatement1->setStatement($statement);
$modificationStatement1->setModification($modification);
$modificationStatement1->setRefused(false);
$modificationStatement1->setDecisionReason('');
$modificationStatement1->setThread($thread);
$manager->persist($modificationStatement1);

// modification is also part of another statement
Expand Down Expand Up @@ -333,11 +336,6 @@ public function load(ObjectManager $manager): void
$manager->persist($paragraph1);
$manager->flush();

$thread = new Thread();
$thread->setIdentifier('statement-'.$statement->getId().'-modification-'.$modification->getId());
$manager->persist($thread);
$manager->flush();

$comment = new Comment();
$comment->setAuthor($user);
$comment->setText('Das ist ein Testkommentar.');
Expand Down Expand Up @@ -384,8 +382,9 @@ public function load(ObjectManager $manager): void
$manager->persist($comment5);

$thread2 = new Thread();
$thread2->setIdentifier('statement-'.$statement->getId().'-paragraph-'.$paragraph2->getId());
$manager->persist($thread2);
$paragraph2Statement = ParagraphStatement::create($statement, $paragraph2, $thread2);
$manager->persist($paragraph2Statement);
$manager->flush();

$comment6 = new Comment();
$comment6->setAuthor($user);
Expand All @@ -395,8 +394,9 @@ public function load(ObjectManager $manager): void
$manager->persist($comment6);

$paragraph1Thread = new Thread();
$paragraph1Thread->setIdentifier('statement-'.$statement->getId().'-paragraph-'.$paragraph1->getId());
$manager->persist($paragraph1Thread);
$paragraph1Statement = ParagraphStatement::create($statement, $paragraph1, $paragraph1Thread);
$manager->persist($paragraph1Statement);
$manager->flush();

$comment11 = new Comment();
$comment11->setAuthor($user);
Expand Down
3 changes: 1 addition & 2 deletions src/Entity/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class Discussion
#[ORM\JoinColumn(nullable: false)]
private ?Consultation $consultation = null;

#[ORM\OneToOne(inversedBy: 'discussion', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: false)]
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
private ?Thread $thread = null;

public function getId(): ?int
Expand Down
28 changes: 28 additions & 0 deletions src/Entity/ModificationStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@ class ModificationStatement
#[ORM\OneToOne(mappedBy: 'modificationStatement', cascade: ['persist', 'remove'])]
private ?ChosenModification $chosen = null;

#[ORM\OneToOne(cascade: ['persist', 'remove'])]
private ?Thread $thread = null;

public function __toString(): string
{
return strval($this->getId());
}

public static function create(
Statement $statement,
Modification $modification,
): self {
$self = new self();
$self->setStatement($statement);
$self->setModification($modification);

return $self;
}

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -122,4 +136,18 @@ public function setChosen(?ChosenModification $chosen): self

return $this;
}

public function getThreadOrCreate(): Thread
{
if ($this->thread === null) {
$this->thread = new Thread();
}

return $this->thread;
}

public function setThread(?Thread $thread): void
{
$this->thread = $thread;
}
}
85 changes: 85 additions & 0 deletions src/Entity/ParagraphStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Entity\Traits\TimestampedEntityTrait;
use App\Repository\ModificationStatementRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ModificationStatementRepository::class)]
class ParagraphStatement
{
use TimestampedEntityTrait;

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\ManyToOne()]
#[ORM\JoinColumn(nullable: false)]
private ?Paragraph $paragraph = null;

#[ORM\ManyToOne()]
#[ORM\JoinColumn(nullable: false)]
private ?Statement $statement = null;

#[ORM\OneToOne(cascade: ['persist', 'remove'])]
private ?Thread $thread = null;

public static function create(Statement $statement, Paragraph $paragraph, Thread $thread = null)
{
$self = new self();
$self->setStatement($statement);
$self->setParagraph($paragraph);
$self->setThread($thread);

return $self;
}

public function getId(): ?int
{
return $this->id;
}

public function setId(?int $id): void
{
$this->id = $id;
}

public function getParagraph(): ?Paragraph
{
return $this->paragraph;
}

public function setParagraph(?Paragraph $paragraph): void
{
$this->paragraph = $paragraph;
}

public function getStatement(): ?Statement
{
return $this->statement;
}

public function setStatement(?Statement $statement): void
{
$this->statement = $statement;
}

public function getThreadOrCreate(): Thread
{
if ($this->thread === null) {
$this->thread = new Thread();
}

return $this->thread;
}

public function setThread(?Thread $thread): void
{
$this->thread = $thread;
}
}
40 changes: 0 additions & 40 deletions src/Entity/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ class Thread
#[ORM\OneToMany(mappedBy: 'thread', targetEntity: Comment::class)]
protected Collection $comments;

#[ORM\Column(length: 255)]
private ?string $identifier = null;

#[ORM\OneToOne(mappedBy: 'thread', cascade: ['persist', 'remove'])]
private ?Discussion $discussion = null;

public function __toString(): string
{
return $this->identifier;
}

public function __construct()
{
$this->comments = new ArrayCollection();
Expand Down Expand Up @@ -116,33 +105,4 @@ public function getTopCommentsForPreview(): ReadableCollection

return new ArrayCollection(array_slice($array, 0, 10));
}

public function getIdentifier(): ?string
{
return $this->identifier;
}

public function setIdentifier(string $identifier): self
{
$this->identifier = $identifier;

return $this;
}

public function getDiscussion(): ?Discussion
{
return $this->discussion;
}

public function setDiscussion(Discussion $discussion): self
{
// set the owning side of the relation if necessary
if ($discussion->getThread() !== $this) {
$discussion->setThread($this);
}

$this->discussion = $discussion;

return $this;
}
}
12 changes: 12 additions & 0 deletions src/Repository/ModificationStatementRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,16 @@ public function findPeers(Statement $statement, array $modifications): array

return $results;
}

public function getOrCreate(Statement $statement, Modification $modification): ModificationStatement
{
$ms = $this->findOneBy(['statement' => $statement, 'modification' => $modification]);
if ($ms === null) {
$ms = ModificationStatement::create($statement, $modification);
$this->getEntityManager()->persist($ms);
$this->getEntityManager()->flush();
}

return $ms;
}
}
Loading
Loading