Skip to content

Commit

Permalink
add comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
pyatnitsev committed Dec 12, 2023
1 parent 01ef6f5 commit cb9a8c8
Show file tree
Hide file tree
Showing 24 changed files with 703 additions and 40 deletions.
41 changes: 41 additions & 0 deletions migrations/Version20231212033444.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 Version20231212033444 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 comment (id INT AUTO_INCREMENT NOT NULL, author_id INT DEFAULT NULL, status_id INT NOT NULL, email VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', comment LONGTEXT NOT NULL, INDEX IDX_9474526CF675F31B (author_id), INDEX IDX_9474526C6BF700BD (status_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE comment_status (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_B1133D0E5E237E06 (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526CF675F31B FOREIGN KEY (author_id) REFERENCES `user` (id)');
$this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C6BF700BD FOREIGN KEY (status_id) REFERENCES comment_status (id)');

foreach (['new', 'published', 'rejected'] as $commentStatus) {
$this->addSql('INSERT INTO comment_status (name) values (:name)', ['name' => $commentStatus]);
}
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526CF675F31B');
$this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526C6BF700BD');
$this->addSql('DROP TABLE comment');
$this->addSql('DROP TABLE comment_status');
}
}
35 changes: 35 additions & 0 deletions migrations/Version20231212041343.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 Version20231212041343 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('ALTER TABLE comment ADD article_id INT NOT NULL');
$this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C7294869C FOREIGN KEY (article_id) REFERENCES article (id)');
$this->addSql('CREATE INDEX IDX_9474526C7294869C ON comment (article_id)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526C7294869C');
$this->addSql('DROP INDEX IDX_9474526C7294869C ON comment');
$this->addSql('ALTER TABLE comment DROP article_id');
}
}
6 changes: 3 additions & 3 deletions src/Controller/Admin/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re
return $this->redirectToRoute('homepage', [], Response::HTTP_SEE_OTHER);
}

return $this->render('article/new.html.twig', [
return $this->render('admin/article/new.html.twig', [
'article' => $article,
'form' => $form,
]);
Expand All @@ -45,7 +45,7 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re
#[Route('/{id}', name: 'app_article_show', methods: ['GET'])]
public function show(Article $article): Response
{
return $this->render('article/show.html.twig', [
return $this->render('admin/article/show.html.twig', [
'article' => $article,
]);
}
Expand All @@ -62,7 +62,7 @@ public function edit(Request $request, Article $article, EntityManagerInterface
return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER);
}

return $this->render('article/edit.html.twig', [
return $this->render('admin/article/edit.html.twig', [
'article' => $article,
'form' => $form,
]);
Expand Down
44 changes: 44 additions & 0 deletions src/Controller/ArticleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Controller;

use App\Entity\Article;
use App\Entity\Comment;
use App\Form\AuthorizedCommentType;
use App\Form\UnauthorizedCommentType;
use App\Repository\CommentRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/article', name: 'article.')]
class ArticleController extends AbstractController
{
#[Route('/{article}', name: 'show')]
public function index(Article $article, Request $request, CommentRepository $commentRepository): Response
{
$comment = (new Comment())
->setArticle($article);

$form = $this->createForm($this->getUser() ? AuthorizedCommentType::class : UnauthorizedCommentType::class, $comment);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$commentRepository->save($comment, true);

if ($comment->isStatusPublished()) {
$this->addFlash('success', 'Комментарий опубликован');
} elseif ($comment->isStatusNew()) {
$this->addFlash('success', 'Комментарий будет опубликован после проверки модератором');
}

return $this->redirectToRoute('article.show', ['article' => $article->getId()]);
}

return $this->render('article/index.html.twig', [
'article' => $article,
'form' => $form,
]);
}
}
43 changes: 43 additions & 0 deletions src/Controller/EasyAdmin/CommentCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Controller\EasyAdmin;

use App\Entity\Comment;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class CommentCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Comment::class;
}

public function configureFields(string $pageName): iterable
{
return [
IdField::new('id')
->hideOnForm(),
EmailField::new('email'),
TextareaField::new('comment'),
DateTimeField::new('createdAt')
->hideOnForm(),
AssociationField::new('status'),
AssociationField::new('author')
->hideOnForm()
];
}

public function configureFilters(Filters $filters): Filters
{
return $filters
->add('status');
}

}
3 changes: 2 additions & 1 deletion src/Controller/EasyAdmin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controller\EasyAdmin;

use App\Entity\Comment;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
Expand Down Expand Up @@ -41,6 +42,6 @@ public function configureDashboard(): Dashboard
public function configureMenuItems(): iterable
{
yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
// yield MenuItem::linkToCrud('The Label', 'fas fa-list', EntityClass::class);
yield MenuItem::linkToCrud('Комментарии', 'fas fa-list', Comment::class);
}
}
41 changes: 41 additions & 0 deletions src/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Entity;

use App\Repository\ArticleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
Expand Down Expand Up @@ -36,6 +38,15 @@ class Article
#[ORM\JoinColumn(nullable: false)]
private ?User $author = null;

#[ORM\OneToMany(mappedBy: 'article', targetEntity: Comment::class, orphanRemoval: true)]
#[ORM\OrderBy(['createdAt' => 'DESC'])]
private Collection $comments;

public function __construct()
{
$this->comments = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -90,4 +101,34 @@ public function setAuthor(?User $author): static

return $this;
}

/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}

public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setArticle($this);
}

return $this;
}

public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getArticle() === $this) {
$comment->setArticle(null);
}
}

return $this;
}
}
Loading

0 comments on commit cb9a8c8

Please sign in to comment.