Adding Abonnements section and correcting errors

pull/20/head
Corentin RICHARD 10 months ago
parent eaf5e4fddc
commit 20719af43b

@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration;
/** /**
* Auto-generated Migration: Please modify to your needs! * Auto-generated Migration: Please modify to your needs!
*/ */
final class Version20240612171415 extends AbstractMigration final class Version20240612212104 extends AbstractMigration
{ {
public function getDescription(): string public function getDescription(): string
{ {
@ -23,7 +23,8 @@ final class Version20240612171415 extends AbstractMigration
$this->addSql('CREATE TABLE commentary (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, post_id INTEGER NOT NULL, profil_id INTEGER NOT NULL, text VARCHAR(255) DEFAULT NULL, CONSTRAINT FK_1CAC12CA4B89032C FOREIGN KEY (post_id) REFERENCES post (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_1CAC12CA275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); $this->addSql('CREATE TABLE commentary (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, post_id INTEGER NOT NULL, profil_id INTEGER NOT NULL, text VARCHAR(255) DEFAULT NULL, CONSTRAINT FK_1CAC12CA4B89032C FOREIGN KEY (post_id) REFERENCES post (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_1CAC12CA275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_1CAC12CA4B89032C ON commentary (post_id)'); $this->addSql('CREATE INDEX IDX_1CAC12CA4B89032C ON commentary (post_id)');
$this->addSql('CREATE INDEX IDX_1CAC12CA275ED078 ON commentary (profil_id)'); $this->addSql('CREATE INDEX IDX_1CAC12CA275ED078 ON commentary (profil_id)');
$this->addSql('CREATE TABLE post (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, profil_id INTEGER NOT NULL, title VARCHAR(255) DEFAULT NULL, text VARCHAR(512) DEFAULT NULL, is_dream BOOLEAN NOT NULL, up_vote INTEGER DEFAULT 0 NOT NULL, down_vote INTEGER DEFAULT 0 NOT NULL, CONSTRAINT FK_5A8A6C8D275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); $this->addSql('CREATE TABLE post (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, profil_id INTEGER NOT NULL, title VARCHAR(255) DEFAULT NULL, text VARCHAR(512) DEFAULT NULL, is_dream BOOLEAN NOT NULL, up_vote INTEGER DEFAULT 0 NOT NULL, down_vote INTEGER DEFAULT 0 NOT NULL, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, CONSTRAINT FK_5A8A6C8D275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_5A8A6C8D275ED078 ON post (profil_id)'); $this->addSql('CREATE INDEX IDX_5A8A6C8D275ED078 ON post (profil_id)');
$this->addSql('CREATE TABLE post_tags (post_id INTEGER NOT NULL, tags_id INTEGER NOT NULL, PRIMARY KEY(post_id, tags_id), CONSTRAINT FK_A6E9F32D4B89032C FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_A6E9F32D8D7B4FB4 FOREIGN KEY (tags_id) REFERENCES tags (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)'); $this->addSql('CREATE TABLE post_tags (post_id INTEGER NOT NULL, tags_id INTEGER NOT NULL, PRIMARY KEY(post_id, tags_id), CONSTRAINT FK_A6E9F32D4B89032C FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_A6E9F32D8D7B4FB4 FOREIGN KEY (tags_id) REFERENCES tags (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_A6E9F32D4B89032C ON post_tags (post_id)'); $this->addSql('CREATE INDEX IDX_A6E9F32D4B89032C ON post_tags (post_id)');

@ -27,7 +27,8 @@ class PostController extends AbstractController
$posts = $this->em->getRepository(Post::class)->findAll(); $posts = $this->em->getRepository(Post::class)->findAll();
return $this->render('post/all.html.twig', [ return $this->render('post/all.html.twig', [
"posts" => $posts "posts" => $posts,
"title" => "Derniers Posts"
]); ]);
} }

@ -4,6 +4,7 @@ namespace App\Controller;
use App\Entity\Profil; use App\Entity\Profil;
use App\Form\ProfilType; use App\Form\ProfilType;
use App\Repository\PostRepository;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use SebastianBergmann\Environment\Console; use SebastianBergmann\Environment\Console;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -15,7 +16,7 @@ class ProfilController extends AbstractController
{ {
public function __construct(private EntityManager $mgr) public function __construct(private EntityManager $mgr, private PostRepository $postRepository)
{ {
} }
#[Route(path: "/profil", name: "profil_perso", methods: ["GET"])] #[Route(path: "/profil", name: "profil_perso", methods: ["GET"])]
@ -45,20 +46,21 @@ class ProfilController extends AbstractController
]); ]);
} }
#[Route('/profil/{id}/follow',name:'profil_follow', requirements: ['page' => '\d+'])]
public function followProfil(int $id): Response #[Route('/profil/post/follow', name: 'profil_post_follow')]
public function postProfilfollow(): Response
{ {
$profil = $this->mgr->find(Profil::class, $id); try{
if ($profil instanceof Profil) { $this->denyAccessUnlessGranted('IS_AUTHENTICATED');
$profil->addFollower($this->getUser()); }catch (\Exception $e){
$this->mgr->persist($profil); return $this->redirectToRoute('app_login');
$this->mgr->flush();
$this->addFlash('success','');
return $this->redirectToRoute('profil_show', ['id' => $id]);
} else {
$this->addFlash('error','');
return $this->render('error.html.twig', []);
} }
$profil = $this->getUser();
$posts = $this->postRepository->getPostFromFollowed($profil);
return $this->render('post/all.html.twig', [
"posts" => $posts,
"title" => "Abonnements"
]);
} }
#[Route('/profil/{id}/unfollow', name: 'profil_unfollow', requirements: ['page' => '\d+'])] #[Route('/profil/{id}/unfollow', name: 'profil_unfollow', requirements: ['page' => '\d+'])]
@ -129,6 +131,22 @@ class ProfilController extends AbstractController
]); ]);
} }
#[Route('/profil/{id}/follow', name: 'profil_follow', requirements: ['page' => '\d+'])]
public function followProfil(int $id): Response
{
$profil = $this->mgr->find(Profil::class, $id);
if ($profil instanceof Profil) {
$profil->addFollower($this->getUser());
$this->mgr->persist($profil);
$this->mgr->flush();
$this->addFlash('success', '');
return $this->redirectToRoute('profil_show', ['id' => $id]);
} else {
$this->addFlash('error', '');
return $this->render('error.html.twig', []);
}
}
#[Route('/profil/{id}/delete', name: 'profil_delete', methods: ['POST'], requirements: ['id' => '\d+'])] #[Route('/profil/{id}/delete', name: 'profil_delete', methods: ['POST'], requirements: ['id' => '\d+'])]
public function delete(int $id, Request $request): Response public function delete(int $id, Request $request): Response
{ {

@ -25,9 +25,6 @@ class Post
#[ORM\Column] #[ORM\Column]
private ?bool $isDream = null; private ?bool $isDream = null;
// #[ORM\Column()]
// private ?DateTime $dateCreated = null;
#[ORM\Column(options: ["default" => 0])] #[ORM\Column(options: ["default" => 0])]
private int $upVote = 0; private int $upVote = 0;
@ -50,10 +47,14 @@ class Post
#[ORM\ManyToMany(targetEntity: Tags::class, inversedBy: 'posts')] #[ORM\ManyToMany(targetEntity: Tags::class, inversedBy: 'posts')]
private Collection $tags; private Collection $tags;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
public function __construct() public function __construct()
{ {
$this->commentaries = new ArrayCollection(); $this->commentaries = new ArrayCollection();
$this->tags = new ArrayCollection(); $this->tags = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
} }
public function getId(): ?int public function getId(): ?int
@ -97,17 +98,6 @@ class Post
return $this; return $this;
} }
public function getDateCreated(): ?DateTime
{
return $this->dateCreated;
}
public function setDateCreated(?DateTime $dateCreated): static
{
$this->dateCreated = $dateCreated;
return $this;
}
public function getUpVote(): ?int public function getUpVote(): ?int
{ {
@ -198,4 +188,16 @@ class Post
return $this; return $this;
} }
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
} }

@ -3,6 +3,7 @@
namespace App\Repository; namespace App\Repository;
use App\Entity\Post; use App\Entity\Post;
use App\Entity\Profil;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
@ -16,20 +17,21 @@ class PostRepository extends ServiceEntityRepository
parent::__construct($registry, Post::class); parent::__construct($registry, Post::class);
} }
// /** /**
// * @return Post[] Returns an array of Post objects * @return Post[] Returns an array of Post objects
// */ */
// public function findByExampleField($value): array public function getPostFromFollowed(Profil $profil): array
// { {
// return $this->createQueryBuilder('p') return $this->createQueryBuilder('p')
// ->andWhere('p.exampleField = :val') ->innerJoin('p.profil', 'a')
// ->setParameter('val', $value) ->innerJoin('a.followers', 'f')
// ->orderBy('p.id', 'ASC') ->where('f.id = :userId')
// ->setMaxResults(10) ->setParameter('userId', $profil->getId())
// ->getQuery() ->orderBy('p.createdAt', 'DESC')
// ->getResult() ->getQuery()
// ; ->getResult();
// } ;
}
// public function findOneBySomeField($value): ?Post // public function findOneBySomeField($value): ?Post
// { // {

@ -21,7 +21,7 @@
<a class="nav-link" href="/"><h1>FukaFukashita</h1></a> <a class="nav-link" href="/"><h1>FukaFukashita</h1></a>
</div> </div>
<div class="nav-links"> <div class="nav-links">
<a href="/" class="nav-link">Abonnements</a> <a href="/profil/post/follow" class="nav-link">Abonnements</a>
<a href="/profil" class="nav-link">Compte</a> <a href="/profil" class="nav-link">Compte</a>
{% if is_granted('ROLE_USER') %} {% if is_granted('ROLE_USER') %}
<a href="/logout" class="nav-link">Se déconnecter</a> <a href="/logout" class="nav-link">Se déconnecter</a>

@ -8,7 +8,7 @@
<link rel="stylesheet" href="{{ asset('css/components/post_all.css') }}"> <link rel="stylesheet" href="{{ asset('css/components/post_all.css') }}">
<div id="wrapper"> <div id="wrapper">
<h1>All posts</h1> <h1>{{title}}</h1>
{% for post in posts %} {% for post in posts %}
{% include 'post/post_mini.html.twig' with {'post' : post} %} {% include 'post/post_mini.html.twig' with {'post' : post} %}

@ -1,10 +1,10 @@
{% extends 'base.html.twig' %} {% extends 'base.html.twig' %}
{% block body %}
<link rel="stylesheet" href="{{ asset('css/components/post.css') }}"> <link rel="stylesheet" href="{{ asset('css/components/post.css') }}">
<title>{{ post.title }}</title> <title>{{ post.title }}</title>
{% block body %}
<div id="post-wrapper"> <div id="post-wrapper">
<div id="post"> <div id="post">
<div id="post-info"> <div id="post-info">

@ -39,4 +39,6 @@
Sign in Sign in
</button> </button>
</form> </form>
<a href="/register">Or Register</a>
{% endblock %} {% endblock %}

Binary file not shown.
Loading…
Cancel
Save