From ad4c4e49a2bcda82fc663f38485ad62a6d967d9f Mon Sep 17 00:00:00 2001 From: gnieark Date: Mon, 16 Dec 2019 19:20:43 +0100 Subject: [PATCH] hey --- composer.json | 4 +- composer.lock | 2 +- config/routes.yaml | 2 +- src/Controller/PadController.php | 64 ++++++++++-- src/Entity/Pad.php | 123 +++++++++++++++++++++-- src/Migrations/Version20191214175405.php | 37 +++++++ src/Migrations/Version20191214194119.php | 37 +++++++ src/Repository/NameRepository.php | 50 +++++++++ src/Repository/PadRepository.php | 50 +++++++++ templates/pad-view.html.twig | 16 +++ templates/pad.html.twig | 4 +- 11 files changed, 364 insertions(+), 25 deletions(-) create mode 100644 src/Migrations/Version20191214175405.php create mode 100644 src/Migrations/Version20191214194119.php create mode 100644 src/Repository/NameRepository.php create mode 100644 src/Repository/PadRepository.php create mode 100644 templates/pad-view.html.twig diff --git a/composer.json b/composer.json index 2f09c63..bd8dab7 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "symfony/mailer": "5.0.*", "symfony/monolog-bundle": "^3.1", "symfony/notifier": "5.0.*", - "symfony/orm-pack": "*", + "symfony/orm-pack": "^1.0", "symfony/process": "5.0.*", "symfony/security-bundle": "5.0.*", "symfony/serializer-pack": "*", @@ -32,7 +32,7 @@ }, "require-dev": { "symfony/debug-pack": "*", - "symfony/maker-bundle": "^1.0", + "symfony/maker-bundle": "^1.14", "symfony/profiler-pack": "*", "symfony/test-pack": "*" }, diff --git a/composer.lock b/composer.lock index 190f232..25804a2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cc6cab883cdc77654d1c749f08f71450", + "content-hash": "152e56ea4ef3a500319be7e6561c46cc", "packages": [ { "name": "doctrine/annotations", diff --git a/config/routes.yaml b/config/routes.yaml index 8a6174b..ac7b476 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -3,7 +3,7 @@ index: controller: App\Controller\PadController::showForm methods: GET|HEAD view: - path: '{id}' + path: '/{name}' controller: App\Controller\PadController::view methods: GET|HEAD diff --git a/src/Controller/PadController.php b/src/Controller/PadController.php index 7d19581..5830a78 100644 --- a/src/Controller/PadController.php +++ b/src/Controller/PadController.php @@ -6,6 +6,11 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + +use App\Entity\Pad; +use Doctrine\ORM\EntityManagerInterface; + Class PadController extends AbstractController { @@ -24,13 +29,49 @@ Class PadController extends AbstractController 'form' => $this->get_padform()->createView() ]); } - public function view($id) + public function view($name) { - return new Response( - '

plop

' - ); - } + $pads = $this->getDoctrine() + ->getRepository(Pad::class) + ->findBy(array('name' => $name)); + + if(count($pads) == 0 ) + { + throw new NotFoundHttpException('This pad does not exist'); + } + + $pad = $pads[0]; + + + return $this->render('pad-view.html.twig', [ + 'head_title' => 'Pad id: ' . $pad->getName(), + 'page_title' => 'Pad id: ' . $pad->getName(), + 'pad_content' => $pad->getContent() + ]); + } + private function get_free_name( $depth = 0, $length=6) + { + + + if($depth > 3 ){ + throw new \UnexpectedValueException("I cant generate an unique key"); + } + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $randstring = ''; + for ($i = 0; $i < $length; $i++) { + $randstring .= $characters[rand(0, strlen($characters) -1)]; + } + + $pads = $this->getDoctrine() + ->getRepository(Pad::class) + ->findBy(array('name' => $randstring)); + if(count($pads) > 0){ + return $this->get_free_name( $depth + 1); + } + return $randstring; + + } public function post(Request $request) { @@ -38,10 +79,15 @@ Class PadController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); - return new Response( - $data["content"] - ); - //return $this->redirectToRoute('task_success'); + //save the pad + $entityManager = $this->getDoctrine()->getManager(); + $pad = new PAD(); + $pad->setContent($data["content"]) + ->setName( $this-> get_free_name() ); + $entityManager->persist($pad); + // actually executes the queries (i.e. the INSERT query) + $entityManager->flush(); + return $this->redirectToRoute('view',["name" => $pad->getName() ]); } diff --git a/src/Entity/Pad.php b/src/Entity/Pad.php index ef2470a..6f10b7f 100644 --- a/src/Entity/Pad.php +++ b/src/Entity/Pad.php @@ -1,28 +1,131 @@ create_date = new \DateTime(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getContent(): ?string { return $this->content; } - public function setContent( string $content ) + + public function setContent(?string $content): self { $this->content = $content; - return $this; + + return $this; } - public function getId() + + public function getCreateDate(): ?\DateTimeInterface { - return $this->id; + return $this->create_date; + } + + public function setCreateDate(\DateTimeInterface $create_date): self + { + $this->create_date = $create_date; + + return $this; + } + + public function getIsClear(): ?bool + { + return $this->is_clear; + } + + public function setIsClear(bool $is_clear): self + { + $this->is_clear = $is_clear; + + return $this; } - public function setId( string $id ) + + public function getIsPublished(): ?bool + { + return $this->is_published; + } + + public function setIsPublished(bool $is_published): self { - $this->id = $id; + $this->is_published = $is_published; + return $this; } -} \ No newline at end of file + public function getExpires(): ?bool + { + return $this->expires; + } + + public function setExpires(bool $expires): self + { + $this->expires = $expires; + + return $this; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } +} diff --git a/src/Migrations/Version20191214175405.php b/src/Migrations/Version20191214175405.php new file mode 100644 index 0000000..9c2c452 --- /dev/null +++ b/src/Migrations/Version20191214175405.php @@ -0,0 +1,37 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('CREATE TABLE name (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE pad (id INT AUTO_INCREMENT NOT NULL, content LONGTEXT DEFAULT NULL, create_date DATETIME NOT NULL, is_clear TINYINT(1) NOT NULL, is_published TINYINT(1) NOT NULL, expires TINYINT(1) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('DROP TABLE name'); + $this->addSql('DROP TABLE pad'); + } +} diff --git a/src/Migrations/Version20191214194119.php b/src/Migrations/Version20191214194119.php new file mode 100644 index 0000000..7305a36 --- /dev/null +++ b/src/Migrations/Version20191214194119.php @@ -0,0 +1,37 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('DROP TABLE name'); + $this->addSql('ALTER TABLE pad ADD name VARCHAR(255) NOT NULL'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('CREATE TABLE name (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB COMMENT = \'\' '); + $this->addSql('ALTER TABLE pad DROP name'); + } +} diff --git a/src/Repository/NameRepository.php b/src/Repository/NameRepository.php new file mode 100644 index 0000000..da31a98 --- /dev/null +++ b/src/Repository/NameRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('n') + ->andWhere('n.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('n.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Name + { + return $this->createQueryBuilder('n') + ->andWhere('n.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/src/Repository/PadRepository.php b/src/Repository/PadRepository.php new file mode 100644 index 0000000..ade1024 --- /dev/null +++ b/src/Repository/PadRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('p') + ->andWhere('p.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('p.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Pad + { + return $this->createQueryBuilder('p') + ->andWhere('p.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/templates/pad-view.html.twig b/templates/pad-view.html.twig new file mode 100644 index 0000000..be8afef --- /dev/null +++ b/templates/pad-view.html.twig @@ -0,0 +1,16 @@ + + + + {{page_title|e('html')}} + {% block stylesheets %} + + {% endblock %} + + +

{{head_title|e('html')}}

+
+            {{ pad_content|e('html') }}
+        
+

Créer un nouveau Pad

+ + \ No newline at end of file diff --git a/templates/pad.html.twig b/templates/pad.html.twig index 0e24280..f07c986 100644 --- a/templates/pad.html.twig +++ b/templates/pad.html.twig @@ -2,13 +2,13 @@ - {{page_title}} + {{ page_title|e('html') }} {% block stylesheets %} {% endblock %} -

{{head_title}}

+

{{head_title|e('html')}}

{{ form_start(form) }} {{ form_widget(form.content, {}) }} {{ form_end(form) }}