From c98ef24cf4893eb427f6248cfc08cf5586a5370b Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 14 Jan 2021 11:14:37 +0100 Subject: [PATCH] Add controllers and entities --- .env | 4 +- migrations/Version20210114101112.php | 43 +++++ src/Controller/CategoryController.php | 20 +++ src/Controller/CommentController.php | 20 +++ src/Controller/PostController.php | 20 +++ src/Entity/Category.php | 77 +++++++++ src/Entity/Comment.php | 110 +++++++++++++ src/Entity/Post.php | 218 ++++++++++++++++++++++++++ src/Repository/CategoryRepository.php | 50 ++++++ src/Repository/CommentRepository.php | 50 ++++++ src/Repository/PostRepository.php | 50 ++++++ templates/category/index.html.twig | 20 +++ templates/comment/index.html.twig | 20 +++ templates/post/index.html.twig | 20 +++ 14 files changed, 720 insertions(+), 2 deletions(-) create mode 100644 migrations/Version20210114101112.php create mode 100644 src/Controller/CategoryController.php create mode 100644 src/Controller/CommentController.php create mode 100644 src/Controller/PostController.php create mode 100644 src/Entity/Category.php create mode 100644 src/Entity/Comment.php create mode 100644 src/Entity/Post.php create mode 100644 src/Repository/CategoryRepository.php create mode 100644 src/Repository/CommentRepository.php create mode 100644 src/Repository/PostRepository.php create mode 100644 templates/category/index.html.twig create mode 100644 templates/comment/index.html.twig create mode 100644 templates/post/index.html.twig diff --git a/.env b/.env index 11e6ef4..73ce1b2 100644 --- a/.env +++ b/.env @@ -27,6 +27,6 @@ APP_SECRET=63ce7b9cb40c3e40df97b34df7d23f53 # IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml # # DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" -# DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7" -DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8" +DATABASE_URL="mysql://project:project@192.168.140.104:3306/project?serverVersion=mariadb-10.4.11" +#DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8" ###< doctrine/doctrine-bundle ### diff --git a/migrations/Version20210114101112.php b/migrations/Version20210114101112.php new file mode 100644 index 0000000..d8ccb53 --- /dev/null +++ b/migrations/Version20210114101112.php @@ -0,0 +1,43 @@ +addSql('CREATE TABLE category (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE category_post (category_id INT NOT NULL, post_id INT NOT NULL, INDEX IDX_D11116CA12469DE2 (category_id), INDEX IDX_D11116CA4B89032C (post_id), PRIMARY KEY(category_id, post_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE comment (id INT AUTO_INCREMENT NOT NULL, post_id INT NOT NULL, username VARCHAR(255) NOT NULL, content LONGTEXT NOT NULL, valid TINYINT(1) NOT NULL, created_at DATETIME NOT NULL, INDEX IDX_9474526C4B89032C (post_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE post (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, description LONGTEXT NOT NULL, content LONGTEXT NOT NULL, slug VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, published_at DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE category_post ADD CONSTRAINT FK_D11116CA12469DE2 FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE category_post ADD CONSTRAINT FK_D11116CA4B89032C FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C4B89032C FOREIGN KEY (post_id) REFERENCES post (id)'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE category_post DROP FOREIGN KEY FK_D11116CA12469DE2'); + $this->addSql('ALTER TABLE category_post DROP FOREIGN KEY FK_D11116CA4B89032C'); + $this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526C4B89032C'); + $this->addSql('DROP TABLE category'); + $this->addSql('DROP TABLE category_post'); + $this->addSql('DROP TABLE comment'); + $this->addSql('DROP TABLE post'); + } +} diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php new file mode 100644 index 0000000..5877329 --- /dev/null +++ b/src/Controller/CategoryController.php @@ -0,0 +1,20 @@ +render('category/index.html.twig', [ + 'controller_name' => 'CategoryController', + ]); + } +} diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php new file mode 100644 index 0000000..7e7e4bc --- /dev/null +++ b/src/Controller/CommentController.php @@ -0,0 +1,20 @@ +render('comment/index.html.twig', [ + 'controller_name' => 'CommentController', + ]); + } +} diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php new file mode 100644 index 0000000..7eab068 --- /dev/null +++ b/src/Controller/PostController.php @@ -0,0 +1,20 @@ +render('post/index.html.twig', [ + 'controller_name' => 'PostController', + ]); + } +} diff --git a/src/Entity/Category.php b/src/Entity/Category.php new file mode 100644 index 0000000..cfdcd79 --- /dev/null +++ b/src/Entity/Category.php @@ -0,0 +1,77 @@ +posts = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + /** + * @return Collection|Post[] + */ + public function getPosts(): Collection + { + return $this->posts; + } + + public function addPost(Post $post): self + { + if (!$this->posts->contains($post)) { + $this->posts[] = $post; + } + + return $this; + } + + public function removePost(Post $post): self + { + $this->posts->removeElement($post); + + return $this; + } +} diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php new file mode 100644 index 0000000..1295da9 --- /dev/null +++ b/src/Entity/Comment.php @@ -0,0 +1,110 @@ +id; + } + + public function getUsername(): ?string + { + return $this->username; + } + + public function setUsername(string $username): self + { + $this->username = $username; + + return $this; + } + + public function getContent(): ?string + { + return $this->content; + } + + public function setContent(string $content): self + { + $this->content = $content; + + return $this; + } + + public function getValid(): ?bool + { + return $this->valid; + } + + public function setValid(bool $valid): self + { + $this->valid = $valid; + + return $this; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(\DateTimeInterface $createdAt): self + { + $this->createdAt = $createdAt; + + return $this; + } + + public function getPost(): ?Post + { + return $this->post; + } + + public function setPost(?Post $post): self + { + $this->post = $post; + + return $this; + } +} diff --git a/src/Entity/Post.php b/src/Entity/Post.php new file mode 100644 index 0000000..97c634d --- /dev/null +++ b/src/Entity/Post.php @@ -0,0 +1,218 @@ +comments = new ArrayCollection(); + $this->categories = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(string $description): self + { + $this->description = $description; + + return $this; + } + + public function getContent(): ?string + { + return $this->content; + } + + public function setContent(string $content): self + { + $this->content = $content; + + return $this; + } + + public function getSlug(): ?string + { + return $this->slug; + } + + public function setSlug(string $slug): self + { + $this->slug = $slug; + + return $this; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(\DateTimeInterface $createdAt): self + { + $this->createdAt = $createdAt; + + return $this; + } + + public function getUpdatedAt(): ?\DateTimeInterface + { + return $this->updatedAt; + } + + public function setUpdatedAt(\DateTimeInterface $updatedAt): self + { + $this->updatedAt = $updatedAt; + + return $this; + } + + public function getPublishedAt(): ?\DateTimeInterface + { + return $this->publishedAt; + } + + public function setPublishedAt(\DateTimeInterface $publishedAt): self + { + $this->publishedAt = $publishedAt; + + return $this; + } + + /** + * @return Collection|Comment[] + */ + public function getComments(): Collection + { + return $this->comments; + } + + public function addComment(Comment $comment): self + { + if (!$this->comments->contains($comment)) { + $this->comments[] = $comment; + $comment->setPost($this); + } + + return $this; + } + + public function removeComment(Comment $comment): self + { + if ($this->comments->removeElement($comment)) { + // set the owning side to null (unless already changed) + if ($comment->getPost() === $this) { + $comment->setPost(null); + } + } + + return $this; + } + + /** + * @return Collection|Category[] + */ + public function getCategories(): Collection + { + return $this->categories; + } + + public function addCategory(Category $category): self + { + if (!$this->categories->contains($category)) { + $this->categories[] = $category; + $category->addPost($this); + } + + return $this; + } + + public function removeCategory(Category $category): self + { + if ($this->categories->removeElement($category)) { + $category->removePost($this); + } + + return $this; + } +} diff --git a/src/Repository/CategoryRepository.php b/src/Repository/CategoryRepository.php new file mode 100644 index 0000000..a12b228 --- /dev/null +++ b/src/Repository/CategoryRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('c.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Category + { + return $this->createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/src/Repository/CommentRepository.php b/src/Repository/CommentRepository.php new file mode 100644 index 0000000..25dc4de --- /dev/null +++ b/src/Repository/CommentRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('c.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Comment + { + return $this->createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/src/Repository/PostRepository.php b/src/Repository/PostRepository.php new file mode 100644 index 0000000..e5bbf8a --- /dev/null +++ b/src/Repository/PostRepository.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): ?Post + { + return $this->createQueryBuilder('p') + ->andWhere('p.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/templates/category/index.html.twig b/templates/category/index.html.twig new file mode 100644 index 0000000..126e408 --- /dev/null +++ b/templates/category/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello CategoryController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: + +
+{% endblock %} diff --git a/templates/comment/index.html.twig b/templates/comment/index.html.twig new file mode 100644 index 0000000..64bed2e --- /dev/null +++ b/templates/comment/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello CommentController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: + +
+{% endblock %} diff --git a/templates/post/index.html.twig b/templates/post/index.html.twig new file mode 100644 index 0000000..aa636fd --- /dev/null +++ b/templates/post/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello PostController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: + +
+{% endblock %}