From 32e8e11882d2faced3f1cd55602ef644789024c9 Mon Sep 17 00:00:00 2001
From: tracer <tracer@24unix.net>
Date: Sun, 30 May 2021 18:18:30 +0200
Subject: [PATCH] added Blog entity

---
 src/Entity/Blog.php               | 214 ++++++++++++++++++++++++++++++
 src/Entity/Section.php            |  39 ++++++
 src/Entity/User.php               |  42 ++++++
 src/Repository/BlogRepository.php |  50 +++++++
 src/Security/AppAuthenticator.php |  15 +--
 5 files changed, 346 insertions(+), 14 deletions(-)
 create mode 100644 src/Entity/Blog.php
 create mode 100644 src/Repository/BlogRepository.php

diff --git a/src/Entity/Blog.php b/src/Entity/Blog.php
new file mode 100644
index 0000000..30eebfe
--- /dev/null
+++ b/src/Entity/Blog.php
@@ -0,0 +1,214 @@
+<?php
+
+namespace App\Entity;
+
+use App\Repository\BlogRepository;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity(repositoryClass=BlogRepository::class)
+ */
+class Blog
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(type="string", length=255)
+     */
+    private $title;
+
+    /**
+     * @ORM\Column(type="text", nullable=true)
+     */
+    private $teaser;
+
+    /**
+     * @ORM\Column(type="string", length=255, nullable=true)
+     */
+    private $teaserImage;
+
+    /**
+     * @ORM\Column(type="text")
+     */
+    private $content;
+
+    /**
+     * @ORM\ManyToOne(targetEntity=User::class, inversedBy="blogs")
+     * @ORM\JoinColumn(nullable=false)
+     */
+    private $author;
+
+    /**
+     * @ORM\ManyToMany(targetEntity=Section::class, inversedBy="blogs")
+     */
+    private $section;
+
+    /**
+     * @ORM\Column(type="datetime")
+     */
+    private $createdAt;
+
+    /**
+     * @ORM\Column(type="datetime", nullable=true)
+     */
+    private $editedAt;
+
+    /**
+     * @ORM\ManyToOne(targetEntity=User::class)
+     */
+    private $editedBy;
+
+    /**
+     * @ORM\Column(type="string", length=255, nullable=true)
+     */
+    private $editReason;
+
+    public function __construct()
+    {
+        $this->section = 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 getTeaser(): ?string
+    {
+        return $this->teaser;
+    }
+
+    public function setTeaser(?string $teaser): self
+    {
+        $this->teaser = $teaser;
+
+        return $this;
+    }
+
+    public function getTeaserImage(): ?string
+    {
+        return $this->teaserImage;
+    }
+
+    public function setTeaserImage(?string $teaserImage): self
+    {
+        $this->teaserImage = $teaserImage;
+
+        return $this;
+    }
+
+    public function getContent(): ?string
+    {
+        return $this->content;
+    }
+
+    public function setContent(string $content): self
+    {
+        $this->content = $content;
+
+        return $this;
+    }
+
+    public function getAuthor(): ?User
+    {
+        return $this->author;
+    }
+
+    public function setAuthor(?User $author): self
+    {
+        $this->author = $author;
+
+        return $this;
+    }
+
+    /**
+     * @return Collection|Section[]
+     */
+    public function getSection(): Collection
+    {
+        return $this->section;
+    }
+
+    public function addSection(Section $section): self
+    {
+        if (!$this->section->contains($section)) {
+            $this->section[] = $section;
+        }
+
+        return $this;
+    }
+
+    public function removeSection(Section $section): self
+    {
+        $this->section->removeElement($section);
+
+        return $this;
+    }
+
+    public function getCreatedAt(): ?\DateTimeInterface
+    {
+        return $this->createdAt;
+    }
+
+    public function setCreatedAt(\DateTimeInterface $createdAt): self
+    {
+        $this->createdAt = $createdAt;
+
+        return $this;
+    }
+
+    public function getEditedAt(): ?\DateTimeInterface
+    {
+        return $this->editedAt;
+    }
+
+    public function setEditedAt(?\DateTimeInterface $editedAt): self
+    {
+        $this->editedAt = $editedAt;
+
+        return $this;
+    }
+
+    public function getEditedBy(): ?User
+    {
+        return $this->editedBy;
+    }
+
+    public function setEditedBy(?User $editedBy): self
+    {
+        $this->editedBy = $editedBy;
+
+        return $this;
+    }
+
+    public function getEditReason(): ?string
+    {
+        return $this->editReason;
+    }
+
+    public function setEditReason(?string $editReason): self
+    {
+        $this->editReason = $editReason;
+
+        return $this;
+    }
+}
diff --git a/src/Entity/Section.php b/src/Entity/Section.php
index 5d7af8b..37216af 100644
--- a/src/Entity/Section.php
+++ b/src/Entity/Section.php
@@ -3,6 +3,8 @@
 namespace App\Entity;
 
 use App\Repository\SectionRepository;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
 use Doctrine\ORM\Mapping as ORM;
 
 /**
@@ -32,6 +34,16 @@ class Section
      */
     private $teaserImage;
 
+    /**
+     * @ORM\ManyToMany(targetEntity=Blog::class, mappedBy="section")
+     */
+    private $blogs;
+
+    public function __construct()
+    {
+        $this->blogs = new ArrayCollection();
+    }
+
     public function getId(): ?int
     {
         return $this->id;
@@ -72,4 +84,31 @@ class Section
 
         return $this;
     }
+
+    /**
+     * @return Collection|Blog[]
+     */
+    public function getBlogs(): Collection
+    {
+        return $this->blogs;
+    }
+
+    public function addBlog(Blog $blog): self
+    {
+        if (!$this->blogs->contains($blog)) {
+            $this->blogs[] = $blog;
+            $blog->addSection($this);
+        }
+
+        return $this;
+    }
+
+    public function removeBlog(Blog $blog): self
+    {
+        if ($this->blogs->removeElement($blog)) {
+            $blog->removeSection($this);
+        }
+
+        return $this;
+    }
 }
diff --git a/src/Entity/User.php b/src/Entity/User.php
index 6792c54..479add4 100644
--- a/src/Entity/User.php
+++ b/src/Entity/User.php
@@ -3,6 +3,8 @@
 namespace App\Entity;
 
 use App\Repository\UserRepository;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Security\Core\User\UserInterface;
 
@@ -59,6 +61,16 @@ class User implements UserInterface
      */
     private $lastLoginAt;
 
+    /**
+     * @ORM\OneToMany(targetEntity=Blog::class, mappedBy="author")
+     */
+    private $blogs;
+
+    public function __construct()
+    {
+        $this->blogs = new ArrayCollection();
+    }
+
     public function getId(): ?int
     {
         return $this->id;
@@ -194,4 +206,34 @@ class User implements UserInterface
 
         return $this;
     }
+
+    /**
+     * @return Collection|Blog[]
+     */
+    public function getBlogs(): Collection
+    {
+        return $this->blogs;
+    }
+
+    public function addBlog(Blog $blog): self
+    {
+        if (!$this->blogs->contains($blog)) {
+            $this->blogs[] = $blog;
+            $blog->setAuthor($this);
+        }
+
+        return $this;
+    }
+
+    public function removeBlog(Blog $blog): self
+    {
+        if ($this->blogs->removeElement($blog)) {
+            // set the owning side to null (unless already changed)
+            if ($blog->getAuthor() === $this) {
+                $blog->setAuthor(null);
+            }
+        }
+
+        return $this;
+    }
 }
diff --git a/src/Repository/BlogRepository.php b/src/Repository/BlogRepository.php
new file mode 100644
index 0000000..2398b11
--- /dev/null
+++ b/src/Repository/BlogRepository.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace App\Repository;
+
+use App\Entity\Blog;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Persistence\ManagerRegistry;
+
+/**
+ * @method Blog|null find($id, $lockMode = null, $lockVersion = null)
+ * @method Blog|null findOneBy(array $criteria, array $orderBy = null)
+ * @method Blog[]    findAll()
+ * @method Blog[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
+ */
+class BlogRepository extends ServiceEntityRepository
+{
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, Blog::class);
+    }
+
+    // /**
+    //  * @return Blog[] Returns an array of Blog objects
+    //  */
+    /*
+    public function findByExampleField($value)
+    {
+        return $this->createQueryBuilder('b')
+            ->andWhere('b.exampleField = :val')
+            ->setParameter('val', $value)
+            ->orderBy('b.id', 'ASC')
+            ->setMaxResults(10)
+            ->getQuery()
+            ->getResult()
+        ;
+    }
+    */
+
+    /*
+    public function findOneBySomeField($value): ?Blog
+    {
+        return $this->createQueryBuilder('b')
+            ->andWhere('b.exampleField = :val')
+            ->setParameter('val', $value)
+            ->getQuery()
+            ->getOneOrNullResult()
+        ;
+    }
+    */
+}
diff --git a/src/Security/AppAuthenticator.php b/src/Security/AppAuthenticator.php
index 74352cd..3ef67ca 100644
--- a/src/Security/AppAuthenticator.php
+++ b/src/Security/AppAuthenticator.php
@@ -175,23 +175,10 @@ class AppAuthenticator extends AbstractFormLoginAuthenticator implements Passwor
 }
 
 /*
-section:
-
-title,
-description,
-teaserImage
-
 
 blog:
 
-title,
-teaser,
-teaserImage
-content,
-autor => user,
-section => section,
-createdAt,
-editedAt,
+
 editedby => user
 editreason