Skip to content

Commit

Permalink
Merge pull request #23 from WildCodeSchool-2023-09/S6_US302_admin_add…
Browse files Browse the repository at this point in the history
…_prize

S6 us302 admin add prize
  • Loading branch information
nicolasalibert authored Feb 7, 2024
2 parents 7a1f5bd + 5b72578 commit cd179c0
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 62 deletions.
1 change: 0 additions & 1 deletion assets/styles/dashboard-admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
}



//

@media screen and (min-width: $desktopWidth) {
Expand Down
1 change: 0 additions & 1 deletion assets/styles/prizes-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@

.guest-message {
margin-top: 5px;
color: red;
font-size: 1rem;
}

Expand Down
55 changes: 55 additions & 0 deletions migrations/Version20240204135041.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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 Version20240204135041 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 category (id INT AUTO_INCREMENT NOT NULL, label VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE game (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(100) NOT NULL, poster VARCHAR(255) DEFAULT NULL, update_at DATETIME DEFAULT NULL, description LONGTEXT NOT NULL, is_virtual TINYINT(1) NOT NULL, is_visible TINYINT(1) DEFAULT NULL, slug VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE game_category (game_id INT NOT NULL, category_id INT NOT NULL, INDEX IDX_AD08E6E7E48FD905 (game_id), INDEX IDX_AD08E6E712469DE2 (category_id), PRIMARY KEY(game_id, category_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE game_played (id INT AUTO_INCREMENT NOT NULL, game_id INT NOT NULL, player_id INT NOT NULL, score INT NOT NULL, date DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', duration INT NOT NULL, uuid VARCHAR(255) NOT NULL, INDEX IDX_11F862FCE48FD905 (game_id), INDEX IDX_11F862FC99E6F5DF (player_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE picture (id INT AUTO_INCREMENT NOT NULL, game_id INT NOT NULL, file_name VARCHAR(255) NOT NULL, INDEX IDX_16DB4F89E48FD905 (game_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE prize (id INT AUTO_INCREMENT NOT NULL, label VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, poster VARCHAR(255) DEFAULT NULL, update_at DATETIME DEFAULT NULL, value INT NOT NULL, quantity INT NOT NULL, slug VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(180) NOT NULL, roles JSON NOT NULL, password VARCHAR(255) NOT NULL, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL, token INT NOT NULL, phonenumber VARCHAR(16) DEFAULT NULL, adress VARCHAR(100) DEFAULT NULL, city VARCHAR(100) DEFAULT NULL, zipcode VARCHAR(5) DEFAULT NULL, email VARCHAR(40) NOT NULL, experience INT NOT NULL, level INT NOT NULL, UNIQUE INDEX UNIQ_8D93D649F85E0677 (username), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE messenger_messages (id BIGINT AUTO_INCREMENT NOT NULL, body LONGTEXT NOT NULL, headers LONGTEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', available_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', delivered_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_75EA56E0FB7336F0 (queue_name), INDEX IDX_75EA56E0E3BD61CE (available_at), INDEX IDX_75EA56E016BA31DB (delivered_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE game_category ADD CONSTRAINT FK_AD08E6E7E48FD905 FOREIGN KEY (game_id) REFERENCES game (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE game_category ADD CONSTRAINT FK_AD08E6E712469DE2 FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE game_played ADD CONSTRAINT FK_11F862FCE48FD905 FOREIGN KEY (game_id) REFERENCES game (id)');
$this->addSql('ALTER TABLE game_played ADD CONSTRAINT FK_11F862FC99E6F5DF FOREIGN KEY (player_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE picture ADD CONSTRAINT FK_16DB4F89E48FD905 FOREIGN KEY (game_id) REFERENCES game (id)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE game_category DROP FOREIGN KEY FK_AD08E6E7E48FD905');
$this->addSql('ALTER TABLE game_category DROP FOREIGN KEY FK_AD08E6E712469DE2');
$this->addSql('ALTER TABLE game_played DROP FOREIGN KEY FK_11F862FCE48FD905');
$this->addSql('ALTER TABLE game_played DROP FOREIGN KEY FK_11F862FC99E6F5DF');
$this->addSql('ALTER TABLE picture DROP FOREIGN KEY FK_16DB4F89E48FD905');
$this->addSql('DROP TABLE category');
$this->addSql('DROP TABLE game');
$this->addSql('DROP TABLE game_category');
$this->addSql('DROP TABLE game_played');
$this->addSql('DROP TABLE picture');
$this->addSql('DROP TABLE prize');
$this->addSql('DROP TABLE user');
$this->addSql('DROP TABLE messenger_messages');
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/game-posters/tshirt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 28 additions & 18 deletions src/Controller/PrizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\String\Slugger\SluggerInterface;

#[Route('/prize', 'prize_')]
Expand All @@ -24,23 +25,33 @@ public function index(PrizeRepository $prizeRepository): Response
]);
}

#[IsGranted('ROLE_ADMIN')]
#[Route('/new', name: 'new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
public function new(
Request $request,
EntityManagerInterface $entityManager,
SluggerInterface $slugger
): Response {
$prize = new Prize();
$form = $this->createForm(PrizeType::class, $prize);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if ($prize->getLabel()) {
$slug = $slugger->slug($prize->getLabel());
$prize->setSlug($slug);
}
$entityManager->persist($prize);
$entityManager->flush();

return $this->redirectToRoute('app_prize_index', [], Response::HTTP_SEE_OTHER);
$this->addFlash("Success", "The prize has been added");

return $this->redirectToRoute('dashboard', [], Response::HTTP_SEE_OTHER);
}

return $this->render('prize/new.html.twig', [
'prize' => $prize,
'form' => $form,
'prizeForm' => $form,
'pageTitle' => 'Admin Add prize',
]);
}

Expand All @@ -54,38 +65,37 @@ public function show(Prize $prize): Response
}

#[Route('/{slug}/edit', name: 'edit', methods: ['GET', 'POST'])]
#[IsGranted('ROLE_ADMIN')]
public function edit(
Request $request,
Prize $prize,
EntityManagerInterface $entityManager,
SluggerInterface $slugger
): Response {
$form = $this->createForm(PrizeType::class, $prize);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$slug = $slugger->slug($prize->getLabel());
$prize->setSlug($slug);
$entityManager->flush();

return $this->redirectToRoute('app_game_index', [], Response::HTTP_SEE_OTHER);
$this->addFlash("Success", "The prize has been added");
return $this->redirectToRoute('dashboard', [], Response::HTTP_SEE_OTHER);
}

return $this->render('prize/edit.html.twig', [
'prize' => $prize,
'form' => $form,
'pageTitle' => 'Prizes'
'prizeForm' => $form,
'pageTitle' => 'Edit prize',
]);
}

#[Route('/{slug}', name: 'delete', methods: ['POST'])]
public function delete(Request $request, Prize $prize, EntityManagerInterface $entityManager): Response
{
public function delete(
Request $request,
Prize $prize,
EntityManagerInterface $entityManager
): Response {
if ($this->isCsrfTokenValid('delete' . $prize->getId(), $request->request->get('_token'))) {
$entityManager->remove($prize);
$entityManager->flush();
}

return $this->redirectToRoute('app_prize_index', [], Response::HTTP_SEE_OTHER);
$this->addFlash("Success", "The prize has been deleted");
return $this->redirectToRoute('dashboard', [], Response::HTTP_SEE_OTHER);
}
}
14 changes: 13 additions & 1 deletion src/Entity/Prize.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getPoster(): ?string
return $this->poster;
}

public function setPoster(string $poster): static
public function setPoster(?string $poster): static
{
$this->poster = $poster;

Expand All @@ -104,6 +104,18 @@ public function setPosterFile(File $image = null): Prize
return $this;
}

public function getUpdateAt(): ?DateTimeInterface
{
return $this->updateAt;
}

public function setUpdateAt(?DateTimeInterface $updateAt): static
{
$this->updateAt = $updateAt;

return $this;
}

public function getValue(): ?int
{
return $this->value;
Expand Down
68 changes: 68 additions & 0 deletions src/Form/PrizeFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Form;

use App\Entity\Prize;
use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Vich\UploaderBundle\Form\Type\VichFileType;

class PrizeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('label', TextType::class, [
'constraints' => [
new NotBlank([
'message' => 'Please enter the title',
]),
new Length([
'min' => 2,
'max' => 100,
]),
],
])
->add('description', TextareaType::class, [
'constraints' => [
new NotBlank([
'message' => 'Please enter the description',
]),
new Length([
'min' => 2,
'max' => 200,
'minMessage' => 'Your description should be at least {{ limit }} characters',
'maxMessage' => 'Your description should not be longer than {{ limit }} characters',
]),
],
])
->add('posterFile', VichFileType::class, [
'required' => true,
])
->add('value', TextType::class, [
'attr' => [
'required' => true,
]
])
->add('quantity', TextType::class, [
'attr' => [
'required' => false,
]
])
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Prize::class,
]);
}
}
37 changes: 0 additions & 37 deletions src/Form/PrizeType.php

This file was deleted.

2 changes: 1 addition & 1 deletion templates/dashboard/admin.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<h1>New Game</h1>
<img class="icon" src="{{ asset('build/images/icons/add-new-game.png') }}" alt="Add new game icon">
</a>
<a href="">
<a href="{{ path('prize_new') }}">
<h1>New Prize</h1>
<img class="icon" src="{{ asset('build/images/icons/add-new-game.png') }}" alt="Add new game icon">
</a>
Expand Down
1 change: 0 additions & 1 deletion templates/game/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
</div>
</form>


<div class="card" id="add-game-btn">
<div class="content">
<a href="{{ path('game_new') }}">
Expand Down
4 changes: 4 additions & 0 deletions templates/prize/_delete_form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<form method="post" action="{{ path('prize_delete', {'slug': prize.slug}) }}" onsubmit="return confirm('Are you sure you want to delete this prize?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ prize.id) }}">
<input type="image" src="{{ asset('build/images/icons/deleteprize.png') }}" alt="Delete">
</form>
18 changes: 18 additions & 0 deletions templates/prize/_form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div id="game-add">
{{ form_start(prizeForm) }}
<div class="game-input">
{{ form_errors(prizeForm) }}

{{ form_row(prizeForm.label) }}
{{ form_row(prizeForm.description, {'attr': {'class': 'description'}}) }}
{{ form_row(prizeForm.posterFile) }}
{{ form_row(prizeForm.value) }}
{{ form_row(prizeForm.quantity) }}

<div>
<button type="submit" id="add-edit-game">Save Prize</button>
</div>
</div>
{{ form_end(prizeForm) }}


10 changes: 10 additions & 0 deletions templates/prize/edit.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.html.twig' %}

{% block content %}
<div class="card full-size">
<div class="content">
<h2>Edit Game</h2>
{{ include('prize/_form.html.twig') }}
</div>
</div>
{% endblock %}
19 changes: 17 additions & 2 deletions templates/prize/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
</div>
</div>
</div>
{% endif %}

{% if is_granted('ROLE_ADMIN') %}
<div class="card" id="add-game-btn">
<div class="content">
<a href="{{ path('prize_new') }}">
<img src="{{ asset('build/images/icons/add-new-game.png') }}" alt="Add new game icon">
<h2>New prize</h2>
</a>
</div>
</div>
{% endif %}

{% if prizes is not empty %}
Expand All @@ -33,9 +44,13 @@
{% if (y + 6*i) < prizes|length %}
{% set prize = prizes[y + 6*i] %}
<section class="card">
<div>

<div class="content">
<div class="flex-coin">
{% if is_granted('ROLE_ADMIN') %}
<a href="{{ path("prize_edit", { slug: prize.slug }) }}">
<img src="{{ asset('build/images/icons/edit-icon.png') }}">
</a>
{% endif %}
<h2>{{ prize.label }}</h2>
</div>
{# <a href="{{ path('prize_show', {slug: prize.slug}) }}"> #}
Expand Down
Loading

0 comments on commit cd179c0

Please sign in to comment.