Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ APP_SECRET=3470cd1756600fd6ff56401ad2ef89dc
# 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://test_dev_symfony:!changeMe!@127.0.0.1:3306/test_dev_symfony?serverVersion=8&charset=utf8mb4"
DATABASE_URL="mysql://root:root@127.0.0.1:8889/commentsBlog?serverVersion=5.7.34-mysql"
#DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=14&charset=utf8"
###< doctrine/doctrine-bundle ###
1,408 changes: 840 additions & 568 deletions composer.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20221102135721 extends AbstractMigration
final class Version20230324205750 extends AbstractMigration
{
public function getDescription(): string
{
Expand All @@ -20,12 +20,16 @@ public function getDescription(): string
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE commentaire (id INT AUTO_INCREMENT NOT NULL, poste_id INT NOT NULL, email VARCHAR(255) NOT NULL, nom VARCHAR(255) NOT NULL, note INT NOT NULL, titre VARCHAR(255) NOT NULL, contenu VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_67F068BCA0905086 (poste_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, content LONGTEXT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE commentaire ADD CONSTRAINT FK_67F068BCA0905086 FOREIGN KEY (poste_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 commentaire DROP FOREIGN KEY FK_67F068BCA0905086');
$this->addSql('DROP TABLE commentaire');
$this->addSql('DROP TABLE post');
}
}
12 changes: 8 additions & 4 deletions public/assets/css/noscript.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Spectral by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
/*
Spectral by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
*/

/* Banner */
Expand All @@ -28,4 +28,8 @@

body.is-preload #banner:after {
opacity: 0;
}
.comment{
color: black;
background-color: white;
}
69 changes: 69 additions & 0 deletions public/assets/css/post.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
.comment{
color: black;
background-color: white;
}
.containerH2{
margin-top: 7%;
margin-left: 10%;
}
.comment.container {
width: 80%;
margin: auto;
border-radius: 7px;
padding: 16px;
margin-top: 20px;
margin-bottom: 20px
}
.comment h3{
color: dodgerblue;
}
.post-info {
display: flex;
justify-content: space-between;
}
p.note::after {
content: "\002B50";
}
.formumair label{
color: black;
}
.formumair{
width: 80%;
margin: auto;
border-radius: 7px;
background-color: white;
padding: 14px 0 14px 0;
/*padding: 16px;
margin-top: 20px;
margin-bottom: 20px*/
}
#comments_envoyer{
background-color: dodgerblue;
}
input#comments_note {
color: black;
}
div#error-messages{
color: #D8000C;
background-color: #FFBABA;
margin-left: 10%;
margin-right: 10%;
border-radius: 6px;
padding: 13px;
margin-bottom: 1%;
display: none;
}
div#success-messages{
color: #4F8A10;
background-color: #DFF2BF;
margin-left: 10%;
margin-right: 10%;
border-radius: 6px;
padding: 13px;
margin-bottom: 1%;
display: none;
}

input[type="text"], input[type="password"], input[type="email"], select, textarea {
background-color: darkgrey;
}
41 changes: 36 additions & 5 deletions public/assets/js/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
/*
Spectral by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
*/
// L'ajax pour soumettre la formulaire de commentaire

$(function() {
$('#comment-form').submit(function(e) {
e.preventDefault();
$('#comment-form button[type="submit"]').prop('disabled', true);
var formData = $(this).serialize();
$.ajax({
type: 'POST',
url: commentPath.replace('postId', postId),
data: formData + '&post_id=' + postId,
success: function(response) {
$('div#error-messages').hide();
$('div#success-messages').hide();
var parser = new DOMParser();
var htmlDoc = parser.parseFromString(response, 'text/html');
var ulElement = htmlDoc.querySelector('#comment-form ul li');
var errorElement = ulElement ? ulElement.textContent : '';
console.log(errorElement);
if (errorElement != '') {
$('div#error-messages').show();
$('#error-messages').html(errorElement);
}else{
$('div#success-messages').html('Success! votre commentaire est bien ajouté');
$('div#success-messages').show();
}
var commentsHtml = $(response).find('#comment-list').html();
$('#comment-form button[type="submit"]').prop('disabled', false);
$('#comment-list').html(commentsHtml);
$('#comment-form')[0].reset();

},
});
});
});


(function($) {

Expand Down
56 changes: 54 additions & 2 deletions src/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
namespace App\Controller;

use App\Entity\Post;
use App\Entity\Commentaire;
use App\Form\CommentsType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;


class PostController extends AbstractController
{
Expand All @@ -15,7 +20,6 @@ class PostController extends AbstractController
public function index(): Response
{
$posts = $this->getDoctrine()->getRepository(Post::class)->findAll();

return $this->render('post/index.html.twig', [
'posts' => $posts,
]);
Expand All @@ -24,14 +28,62 @@ public function index(): Response
/**
* @Route("/post/{id}", name="app_post")
*/
public function post($id)
public function post($id, Request $request)
{
$post = $this->getDoctrine()
->getRepository(Post::class)
->find($id);

//création de commentaire vide
$comment = new Commentaire;

// génère le formulaire
$commentForm = $this->createForm(CommentsType::class, $comment);
$commentForm->handleRequest($request);
$commentdt = [];

if ($commentForm->isSubmitted() && $commentForm->isValid()) {
//dd($commentdt);
// récupère les données saisies dans le formulaire
$comment->setPoste($post);
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
// Return un json reponse

$comments = $this->getDoctrine()->getRepository(Commentaire::class)->findBy(['poste' => $post]);

foreach ($comments as $comment) {
$commentdt[] = [
'id' => $comment->getId(),
'email' => $comment->getEmail(),
'nom' => $comment->getNom(),
'note' => $comment->getNote(),
'titre' => $comment->getTitre(),
'content' => $comment->getContenu(),
];
}
$commentsHtml = $this->renderView('post/post.html.twig', ['comments' => $commentdt, 'post' => $post, 'formulaire' => $commentForm->createView()]);
return new Response($commentsHtml);
}
// Get the comments from the database
$comments = $this->getDoctrine()->getRepository(Commentaire::class)->findBy(['poste' => $post]);
$commentdt = [];
foreach ($comments as $comment) {
$commentdt[] = [
'id' => $comment->getId(),
'email' => $comment->getEmail(),
'nom' => $comment->getNom(),
'note' => $comment->getNote(),
'titre' => $comment->getTitre(),
'content' => $comment->getContenu(),
];
}
return $this->render('post/post.html.twig', [
'post' => $post,
'formulaire' => $commentForm->createView(),
'comments' => $commentdt
]);
}

}
Loading