From d8dd3ed69c9e157bf1f97fec1d32d8c932dbb61c Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 26 Mar 2024 17:52:39 +0100 Subject: [PATCH] partner follow unique --- ...20240325145137.php => Version20240326161210.php} | 4 ++-- src/ApiResource/SaleApi.php | 1 + src/Entity/PartnerFollow.php | 3 +++ src/Factory/PartnerFollowFactory.php | 13 +++++++++++-- 4 files changed, 17 insertions(+), 4 deletions(-) rename migrations/{Version20240325145137.php => Version20240326161210.php} (98%) diff --git a/migrations/Version20240325145137.php b/migrations/Version20240326161210.php similarity index 98% rename from migrations/Version20240325145137.php rename to migrations/Version20240326161210.php index b63d216..9023414 100644 --- a/migrations/Version20240325145137.php +++ b/migrations/Version20240326161210.php @@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240325145137 extends AbstractMigration +final class Version20240326161210 extends AbstractMigration { public function getDescription(): string { @@ -26,7 +26,7 @@ final class Version20240325145137 extends AbstractMigration $this->addSql('CREATE TABLE document_object (id INT AUTO_INCREMENT NOT NULL, file_path VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE media_object (id INT AUTO_INCREMENT NOT NULL, file_path VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE partner (id INT AUTO_INCREMENT NOT NULL, logo_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, street VARCHAR(255) DEFAULT NULL, street_no VARCHAR(255) DEFAULT NULL, zip VARCHAR(255) DEFAULT NULL, city VARCHAR(255) DEFAULT NULL, country VARCHAR(255) DEFAULT NULL, website VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_312B3E16F98F144A (logo_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); - $this->addSql('CREATE TABLE partner_follow (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, partner_id INT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_55FFED0BA76ED395 (user_id), INDEX IDX_55FFED0B9393F8FE (partner_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE partner_follow (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, partner_id INT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_55FFED0BA76ED395 (user_id), INDEX IDX_55FFED0B9393F8FE (partner_id), UNIQUE INDEX unique_follow (user_id, partner_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE posting (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, partner_id INT NOT NULL, contact_id INT DEFAULT NULL, sale_id INT DEFAULT NULL, headline VARCHAR(255) NOT NULL, message LONGTEXT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_BD275D737E3C61F9 (owner_id), INDEX IDX_BD275D739393F8FE (partner_id), INDEX IDX_BD275D73E7A1254A (contact_id), INDEX IDX_BD275D734A7E4868 (sale_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE product (id INT AUTO_INCREMENT NOT NULL, image_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_D34A04AD3DA5256D (image_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE sale (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, partner_id INT NOT NULL, product_id INT NOT NULL, turnover INT NOT NULL, profit INT DEFAULT NULL, comment LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_E54BC0057E3C61F9 (owner_id), INDEX IDX_E54BC0059393F8FE (partner_id), INDEX IDX_E54BC0054584665A (product_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); diff --git a/src/ApiResource/SaleApi.php b/src/ApiResource/SaleApi.php index 905a8fc..4320ef7 100644 --- a/src/ApiResource/SaleApi.php +++ b/src/ApiResource/SaleApi.php @@ -40,6 +40,7 @@ use Symfony\Component\Validator\Constraints\NotBlank; security: 'is_granted("ROLE_ADMIN")', ) ], + order: ['createdAt' => 'DESC'], security: 'is_granted("ROLE_USER")', provider: EntityToDtoStateProvider::class, processor: EntityClassDtoStateProcessor::class, diff --git a/src/Entity/PartnerFollow.php b/src/Entity/PartnerFollow.php index 57f0a61..69eb33f 100644 --- a/src/Entity/PartnerFollow.php +++ b/src/Entity/PartnerFollow.php @@ -6,6 +6,7 @@ use App\Repository\PartnerFollowRepository; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: PartnerFollowRepository::class)] +#[ORM\UniqueConstraint(name: "unique_follow", columns: ["user_id", "partner_id"])] class PartnerFollow { #[ORM\Id] @@ -72,3 +73,5 @@ class PartnerFollow return $this; } } +///api/users/155 +///api/partners/289 \ No newline at end of file diff --git a/src/Factory/PartnerFollowFactory.php b/src/Factory/PartnerFollowFactory.php index d9c45df..f89747f 100644 --- a/src/Factory/PartnerFollowFactory.php +++ b/src/Factory/PartnerFollowFactory.php @@ -46,10 +46,19 @@ final class PartnerFollowFactory extends ModelFactory */ protected function getDefaults(): array { + $found = true; + while ($found) { + $partner = PartnerFactory::random(); + $user = UserFactory::random(); + $follows = PartnerFollowFactory::findBy(['user' => $user, 'partner' => $partner]); + if (count($follows) === 0) { + $found = false; + } + } return [ 'createdAt' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()), - 'partner' => PartnerFactory::random(), - 'user' => UserFactory::random(), + 'partner' => $partner, + 'user' => $user, ]; }