From 37e756a0249fbc7e6fed32608fa1ade7bb9f0469 Mon Sep 17 00:00:00 2001 From: Julian PEREZ-RAMIREZ <julian.perez-ramirez@imt-atlantique.net> Date: Wed, 12 Mar 2025 19:32:25 +0100 Subject: [PATCH] fixing validations for the db scheme update --- .../migrations/Version20250305103324.php | 51 ------------------- .../web_app/src/Controller/UserController.php | 2 +- server/web_app/src/Entity/User.php | 6 +-- server/web_app/src/Entity/WishListMember.php | 2 +- 4 files changed, 4 insertions(+), 57 deletions(-) delete mode 100644 server/web_app/migrations/Version20250305103324.php diff --git a/server/web_app/migrations/Version20250305103324.php b/server/web_app/migrations/Version20250305103324.php deleted file mode 100644 index 39e2b66..0000000 --- a/server/web_app/migrations/Version20250305103324.php +++ /dev/null @@ -1,51 +0,0 @@ -<?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 Version20250305103324 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 item (id INT AUTO_INCREMENT NOT NULL, wish_list_id INT NOT NULL, title VARCHAR(255) NOT NULL, description VARCHAR(255) DEFAULT NULL, price DOUBLE PRECISION NOT NULL, purchase_url VARCHAR(255) DEFAULT NULL, is_bought TINYINT(1) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_1F1B251ED69F3311 (wish_list_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); - $this->addSql('CREATE TABLE purchase (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, item_id INT NOT NULL, url_proof VARCHAR(255) DEFAULT NULL, message VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_6117D13BA76ED395 (user_id), INDEX IDX_6117D13B126F525E (item_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); - $this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, user_name VARCHAR(255) DEFAULT NULL, name VARCHAR(255) DEFAULT NULL, surname VARCHAR(255) DEFAULT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, is_blocked TINYINT(1) DEFAULT NULL, role VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); - $this->addSql('CREATE TABLE wish_list (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255) NOT NULL, expiration_date DATETIME DEFAULT NULL, is_active TINYINT(1) NOT NULL, url_view_mode VARCHAR(255) NOT NULL, url_edit_mode VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_5B8739BDA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); - $this->addSql('CREATE TABLE wish_list_member (id INT AUTO_INCREMENT NOT NULL, wish_list_id INT NOT NULL, user_id INT NOT NULL, can_edit TINYINT(1) NOT NULL, is_accepted TINYINT(1) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_F4CA81DFD69F3311 (wish_list_id), INDEX IDX_F4CA81DFA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); - $this->addSql('ALTER TABLE item ADD CONSTRAINT FK_1F1B251ED69F3311 FOREIGN KEY (wish_list_id) REFERENCES wish_list (id)'); - $this->addSql('ALTER TABLE purchase ADD CONSTRAINT FK_6117D13BA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); - $this->addSql('ALTER TABLE purchase ADD CONSTRAINT FK_6117D13B126F525E FOREIGN KEY (item_id) REFERENCES item (id)'); - $this->addSql('ALTER TABLE wish_list ADD CONSTRAINT FK_5B8739BDA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); - $this->addSql('ALTER TABLE wish_list_member ADD CONSTRAINT FK_F4CA81DFD69F3311 FOREIGN KEY (wish_list_id) REFERENCES wish_list (id)'); - $this->addSql('ALTER TABLE wish_list_member ADD CONSTRAINT FK_F4CA81DFA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('ALTER TABLE item DROP FOREIGN KEY FK_1F1B251ED69F3311'); - $this->addSql('ALTER TABLE purchase DROP FOREIGN KEY FK_6117D13BA76ED395'); - $this->addSql('ALTER TABLE purchase DROP FOREIGN KEY FK_6117D13B126F525E'); - $this->addSql('ALTER TABLE wish_list DROP FOREIGN KEY FK_5B8739BDA76ED395'); - $this->addSql('ALTER TABLE wish_list_member DROP FOREIGN KEY FK_F4CA81DFD69F3311'); - $this->addSql('ALTER TABLE wish_list_member DROP FOREIGN KEY FK_F4CA81DFA76ED395'); - $this->addSql('DROP TABLE item'); - $this->addSql('DROP TABLE purchase'); - $this->addSql('DROP TABLE user'); - $this->addSql('DROP TABLE wish_list'); - $this->addSql('DROP TABLE wish_list_member'); - } -} diff --git a/server/web_app/src/Controller/UserController.php b/server/web_app/src/Controller/UserController.php index 108b828..2226a94 100644 --- a/server/web_app/src/Controller/UserController.php +++ b/server/web_app/src/Controller/UserController.php @@ -85,7 +85,7 @@ final class UserController extends AbstractController implements UserControllerI $this->entityManager->persist($user); $this->entityManager->flush(); } catch (UniqueConstraintViolationException $e) { - return new JsonResponse(['error' => 'This email is already registered.'], Response::HTTP_BAD_REQUEST); + return new JsonResponse(['error' => 'This username or email is already registered.'], Response::HTTP_BAD_REQUEST); } return $this->json([ diff --git a/server/web_app/src/Entity/User.php b/server/web_app/src/Entity/User.php index 532b2fd..c7dc586 100644 --- a/server/web_app/src/Entity/User.php +++ b/server/web_app/src/Entity/User.php @@ -42,19 +42,17 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[Assert\Length(min: 8, minMessage: "Password must be at least {{ limit }} characters long.")] private ?string $password = null; - #[ORM\Column] #[ORM\Column(type: 'boolean', options: ['default' => false])] #[Assert\NotNull(message: "Blocked status cannot be null.")] private ?bool $isBlocked = false; - #[ORM\Column(enumType: UserRole::class, options: ['default' => 'user'])] + #[ORM\Column(enumType: UserRole::class)] #[Assert\Choice( - callback: [UserRole::class, 'getValues'], + callback: [UserRole::class, 'cases'], message: "Invalid role value. Allowed values: {{ choices }}." )] private UserRole $role = UserRole::USER; - #[ORM\Column] #[Assert\NotNull(message: "CreatedAt must be set.")] #[Assert\Type(type: \DateTimeImmutable::class, message: "CreatedAt must be a valid DateTimeImmutable instance.")] diff --git a/server/web_app/src/Entity/WishListMember.php b/server/web_app/src/Entity/WishListMember.php index 07e3275..0d4deab 100644 --- a/server/web_app/src/Entity/WishListMember.php +++ b/server/web_app/src/Entity/WishListMember.php @@ -24,7 +24,7 @@ class WishListMember #[Assert\NotNull(message: "User must be provided.")] private ?User $user = null; - #[ORM\Column] + #[ORM\Column(nullable: true)] #[Assert\NotNull(message: "Edit permission (canEdit) must be set.")] #[Assert\Type(type: 'bool', message: "canEdit must be a boolean value.")] private ?bool $canEdit = false; -- GitLab