From 1983f5815d8ed285bfe587c9f8bc559b2197992a Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 28 Mar 2024 16:11:51 +0100 Subject: [PATCH] several things --- ...28105838.php => Version20240328150920.php} | 24 +++---- src/ApiResource/PostingApi.php | 11 ++++ src/ApiResource/SaleApi.php | 4 ++ src/DataFixtures/AppFixtures.php | 64 ++++++++++--------- src/Entity/Contact.php | 4 +- src/Entity/Document.php | 2 +- src/Entity/DocumentObject.php | 2 +- src/Entity/MediaObject.php | 2 +- src/Entity/Partner.php | 5 +- src/Entity/Posting.php | 18 ++++++ src/Entity/Product.php | 36 ++++++++++- src/Entity/Sale.php | 13 ++++ src/Entity/Task.php | 2 +- src/Factory/ContactFactory.php | 2 +- src/Factory/DocumentFactory.php | 2 +- src/Factory/PostingFactory.php | 1 + src/Factory/SaleFactory.php | 3 +- src/Mapper/PostingApiToEntityMapper.php | 9 ++- src/Mapper/PostingEntityToApiMapper.php | 7 ++ src/Mapper/SaleApiToEntityMapper.php | 1 + src/Mapper/SaleEntityToApiMapper.php | 1 + tests/Functional/PartnerResourceTest.php | 1 - tests/Functional/PostingResourceTest.php | 3 +- 23 files changed, 161 insertions(+), 56 deletions(-) rename migrations/{Version20240328105838.php => Version20240328150920.php} (86%) diff --git a/migrations/Version20240328105838.php b/migrations/Version20240328150920.php similarity index 86% rename from migrations/Version20240328105838.php rename to migrations/Version20240328150920.php index 25275bb..2b5d0e8 100644 --- a/migrations/Version20240328105838.php +++ b/migrations/Version20240328150920.php @@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240328105838 extends AbstractMigration +final class Version20240328150920 extends AbstractMigration { public function getDescription(): string { @@ -25,11 +25,11 @@ final class Version20240328105838 extends AbstractMigration $this->addSql('CREATE TABLE document (id INT AUTO_INCREMENT NOT NULL, created_by_id INT NOT NULL, partner_id INT DEFAULT NULL, product_id INT DEFAULT NULL, document_object_id INT NOT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_D8698A76B03A8386 (created_by_id), INDEX IDX_D8698A769393F8FE (partner_id), INDEX IDX_D8698A764584665A (product_id), INDEX IDX_D8698A7631157E8A (document_object_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE document_object (id INT AUTO_INCREMENT NOT NULL, created_by_id INT NOT NULL, file_path VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_16CF1A8AB03A8386 (created_by_id), 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, created_by_id INT NOT NULL, file_path VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_14D43132B03A8386 (created_by_id), 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, created_by_id INT NOT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT 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), INDEX IDX_312B3E16B03A8386 (created_by_id), 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, created_by_id INT NOT NULL, navision_id INT UNSIGNED DEFAULT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT 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)\', UNIQUE INDEX UNIQ_312B3E16B0FD8AF9 (navision_id), INDEX IDX_312B3E16F98F144A (logo_id), INDEX IDX_312B3E16B03A8386 (created_by_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 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, product_id INT NOT 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), INDEX IDX_BD275D734584665A (product_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, created_by_id INT NOT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_D34A04AD3DA5256D (image_id), INDEX IDX_D34A04ADB03A8386 (created_by_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'); + $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, quantity INT UNSIGNED NOT 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'); $this->addSql('CREATE TABLE task (id INT AUTO_INCREMENT NOT NULL, created_by_id INT NOT NULL, assigned_to_id INT NOT NULL, partner_id INT DEFAULT NULL, contact_id INT DEFAULT NULL, headline VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, due_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', prio VARCHAR(255) NOT NULL, completed TINYINT(1) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_527EDB25B03A8386 (created_by_id), INDEX IDX_527EDB25F4BD7827 (assigned_to_id), INDEX IDX_527EDB259393F8FE (partner_id), INDEX IDX_527EDB25E7A1254A (contact_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE task_note (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, task_id INT NOT NULL, message LONGTEXT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_BC0E6E6F7E3C61F9 (owner_id), INDEX IDX_BC0E6E6F8DB60186 (task_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, image_id INT DEFAULT NULL, email VARCHAR(180) NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, roles JSON NOT NULL COMMENT \'(DC2Type:json)\', password VARCHAR(255) NOT NULL, active TINYINT(1) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), INDEX IDX_8D93D6493DA5256D (image_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); @@ -37,27 +37,28 @@ final class Version20240328105838 extends AbstractMigration $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C9AE985F6 FOREIGN KEY (posting_id) REFERENCES posting (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E6389393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E6383DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id) ON DELETE SET NULL'); - $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E638B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); - $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A76B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); + $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E638B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A76B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A769393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id)'); $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A764584665A FOREIGN KEY (product_id) REFERENCES product (id)'); $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A7631157E8A FOREIGN KEY (document_object_id) REFERENCES document_object (id)'); - $this->addSql('ALTER TABLE document_object ADD CONSTRAINT FK_16CF1A8AB03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); - $this->addSql('ALTER TABLE media_object ADD CONSTRAINT FK_14D43132B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); + $this->addSql('ALTER TABLE document_object ADD CONSTRAINT FK_16CF1A8AB03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE media_object ADD CONSTRAINT FK_14D43132B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE partner ADD CONSTRAINT FK_312B3E16F98F144A FOREIGN KEY (logo_id) REFERENCES media_object (id) ON DELETE SET NULL'); - $this->addSql('ALTER TABLE partner ADD CONSTRAINT FK_312B3E16B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); + $this->addSql('ALTER TABLE partner ADD CONSTRAINT FK_312B3E16B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE partner_follow ADD CONSTRAINT FK_55FFED0BA76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE partner_follow ADD CONSTRAINT FK_55FFED0B9393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D737E3C61F9 FOREIGN KEY (owner_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D739393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D73E7A1254A FOREIGN KEY (contact_id) REFERENCES contact (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D734A7E4868 FOREIGN KEY (sale_id) REFERENCES sale (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D734584665A FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE product ADD CONSTRAINT FK_D34A04AD3DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id) ON DELETE SET NULL'); - $this->addSql('ALTER TABLE product ADD CONSTRAINT FK_D34A04ADB03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); + $this->addSql('ALTER TABLE product ADD CONSTRAINT FK_D34A04ADB03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE sale ADD CONSTRAINT FK_E54BC0057E3C61F9 FOREIGN KEY (owner_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE sale ADD CONSTRAINT FK_E54BC0059393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE sale ADD CONSTRAINT FK_E54BC0054584665A FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE'); - $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB25B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); + $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB25B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB25F4BD7827 FOREIGN KEY (assigned_to_id) REFERENCES `user` (id)'); $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB259393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB25E7A1254A FOREIGN KEY (contact_id) REFERENCES contact (id) ON DELETE CASCADE'); @@ -88,6 +89,7 @@ final class Version20240328105838 extends AbstractMigration $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D739393F8FE'); $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D73E7A1254A'); $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D734A7E4868'); + $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D734584665A'); $this->addSql('ALTER TABLE product DROP FOREIGN KEY FK_D34A04AD3DA5256D'); $this->addSql('ALTER TABLE product DROP FOREIGN KEY FK_D34A04ADB03A8386'); $this->addSql('ALTER TABLE sale DROP FOREIGN KEY FK_E54BC0057E3C61F9'); diff --git a/src/ApiResource/PostingApi.php b/src/ApiResource/PostingApi.php index 412312f..fcd516a 100644 --- a/src/ApiResource/PostingApi.php +++ b/src/ApiResource/PostingApi.php @@ -87,6 +87,17 @@ class PostingApi implements OwnerInterface #[Assert\NotBlank(groups: ['posting:create'])] public ?PartnerApi $partner = null; + #[ApiProperty(writable: false)] + public ?string $partnerName = null; + + #[ApiProperty(writable: true)] + #[Groups(['posting:create'])] + #[Assert\NotBlank(groups: ['posting:create'])] + public ?ProductApi $product = null; + + #[ApiProperty(writable: false)] + public ?string $productName = null; + #[ApiProperty(writable: true)] #[Groups(['posting:create'])] public ?ContactApi $contact = null; diff --git a/src/ApiResource/SaleApi.php b/src/ApiResource/SaleApi.php index 4984e74..4753d52 100644 --- a/src/ApiResource/SaleApi.php +++ b/src/ApiResource/SaleApi.php @@ -77,8 +77,12 @@ class SaleApi implements OwnerInterface #[NotBlank] public ?int $turnover = null; + #[NotBlank] public ?int $profit = null; + #[NotBlank] + public ?int $quantity = null; + public ?string $comment = null; #[ApiProperty(writable: false)] diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index 3b67548..f903655 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -62,12 +62,7 @@ class AppFixtures extends Fixture ] ); $adminD->setRoles(['ROLE_ADMIN']); - MediaObjectUserFactory::createMany(20); - MediaObjectLogoFactory::createMany(50); - MediaObjectContactFactory::createMany(50); - MediaObjectProductFactory::createMany(50); - DocumentObjectFactory::createMany(50); - $adminD->setImage(MediaObjectUserFactory::random()->object()); + $adminD->setImage(MediaObjectUserFactory::createOne()->object()); $adminF = UserFactory::createOne( [ @@ -75,37 +70,46 @@ class AppFixtures extends Fixture 'firstName' => 'Florian', 'lastName' => 'Eisenmenger', 'password' => 'test', - 'image' => MediaObjectUserFactory::random() + 'image' => MediaObjectUserFactory::createOne() ] ); $adminF->setRoles(['ROLE_ADMIN']); - UserFactory::createMany(10, - [ - 'image' => MediaObjectUserFactory::random() - ] - ); + for ($i = 0; $i < 10; $i++) { + UserFactory::createOne( + [ + 'image' => MediaObjectUserFactory::createOne() + ] + ); + } - PartnerFactory::createMany(100, - [ - 'logo' => MediaObjectLogoFactory::random(), - 'createdBy' => $adminD - ] - ); + for ($i = 0; $i < 100; $i++) { + PartnerFactory::createOne( + [ + 'logo' => MediaObjectLogoFactory::createOne(), + 'createdBy' => $adminD + ] + ); + } - ContactFactory::createMany(200, - [ - 'image' => MediaObjectContactFactory::random(), - 'createdBy' => $adminD - ] - ); + for ($i = 0; $i < 200; $i++) { + ContactFactory::createOne( + [ + 'image' => MediaObjectContactFactory::createOne(), + 'createdBy' => $adminD + ] + ); + } + + for ($i = 0; $i < 100; $i++) { + ProductFactory::createOne( + [ + 'image' => MediaObjectProductFactory::createOne(), + 'createdBy' => $adminD + ] + ); + } - ProductFactory::createMany(100, - [ - 'image' => MediaObjectProductFactory::createOne(), - 'createdBy' => $adminD - ] - ); SaleFactory::createMany(50); PostingFactory::createMany(200); CommentFactory::createMany(300); diff --git a/src/Entity/Contact.php b/src/Entity/Contact.php index 65b49a1..ddd4b88 100644 --- a/src/Entity/Contact.php +++ b/src/Entity/Contact.php @@ -5,9 +5,7 @@ namespace App\Entity; use App\Repository\ContactRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; -use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; -use Symfony\Component\Validator\Constraints as Assert; #[ORM\Entity(repositoryClass: ContactRepository::class)] class Contact @@ -44,7 +42,7 @@ class Contact private ?string $email = null; #[ORM\ManyToOne] - #[ORM\JoinColumn(nullable: false)] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private ?User $createdBy = null; #[ORM\Column] diff --git a/src/Entity/Document.php b/src/Entity/Document.php index e41a6e0..8393f83 100644 --- a/src/Entity/Document.php +++ b/src/Entity/Document.php @@ -17,7 +17,7 @@ class Document private ?int $id = null; #[ORM\ManyToOne(inversedBy: 'documents')] - #[ORM\JoinColumn(nullable: false)] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private ?User $createdBy = null; #[ORM\Column(length: 255)] diff --git a/src/Entity/DocumentObject.php b/src/Entity/DocumentObject.php index 255f8c1..171cf58 100644 --- a/src/Entity/DocumentObject.php +++ b/src/Entity/DocumentObject.php @@ -73,7 +73,7 @@ class DocumentObject public ?string $filePath = null; #[ORM\ManyToOne] - #[ORM\JoinColumn(nullable: false)] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private ?User $createdBy = null; #[ORM\Column] diff --git a/src/Entity/MediaObject.php b/src/Entity/MediaObject.php index b18cc8e..3040d1a 100644 --- a/src/Entity/MediaObject.php +++ b/src/Entity/MediaObject.php @@ -73,7 +73,7 @@ class MediaObject public ?string $filePath = null; #[ORM\ManyToOne] - #[ORM\JoinColumn(nullable: false)] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private ?User $createdBy = null; #[ORM\Column] diff --git a/src/Entity/Partner.php b/src/Entity/Partner.php index ece57a8..af74a12 100644 --- a/src/Entity/Partner.php +++ b/src/Entity/Partner.php @@ -17,6 +17,9 @@ class Partner #[ORM\Column] private ?int $id = null; + #[ORM\Column(type: "integer", unique: true, nullable: true, options: ["unsigned" => true])] + protected int $navisionId; + #[ORM\Column(length: 255)] private string $name; @@ -49,7 +52,7 @@ class Partner private ?MediaObject $logo = null; #[ORM\ManyToOne] - #[ORM\JoinColumn(nullable: false)] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private ?User $createdBy = null; #[ORM\Column] diff --git a/src/Entity/Posting.php b/src/Entity/Posting.php index 3766f7a..6274ce2 100644 --- a/src/Entity/Posting.php +++ b/src/Entity/Posting.php @@ -38,6 +38,10 @@ class Posting #[ORM\JoinColumn(nullable: true, onDelete: "CASCADE")] private ?Sale $sale = null; + #[ORM\ManyToOne(inversedBy: 'postings')] + #[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")] + private ?Product $product = null; + #[ORM\Column] private ?\DateTimeImmutable $createdAt = null; @@ -47,11 +51,13 @@ class Posting public function __construct( User $owner, Partner $partner, + Product $product, Contact $contact = null, Sale $sale = null ) { $this->owner = $owner; $this->partner = $partner; + $this->product = $product; $this->contact = $contact; $this->sale = $sale; $this->createdAt = new \DateTimeImmutable(); @@ -167,4 +173,16 @@ class Posting return $this; } + + public function getProduct(): ?Product + { + return $this->product; + } + + public function setProduct(?Product $product): static + { + $this->product = $product; + + return $this; + } } diff --git a/src/Entity/Product.php b/src/Entity/Product.php index f16a495..d33be2e 100644 --- a/src/Entity/Product.php +++ b/src/Entity/Product.php @@ -27,7 +27,7 @@ class Product private ?MediaObject $image = null; #[ORM\ManyToOne] - #[ORM\JoinColumn(nullable: false)] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private ?User $createdBy = null; #[ORM\Column] @@ -39,12 +39,16 @@ class Product #[ORM\OneToMany(mappedBy: 'product', targetEntity: Document::class)] private Collection $documentObjects; + #[ORM\OneToMany(mappedBy: 'product', targetEntity: Posting::class)] + private Collection $postings; + public function __construct(User $createdBy) { $this->createdBy = $createdBy; $this->createdAt = new \DateTimeImmutable(); $this->sales = new ArrayCollection(); $this->documentObjects = new ArrayCollection(); + $this->postings = new ArrayCollection(); } public function getId(): ?int @@ -157,4 +161,34 @@ class Product return $this; } + + /** + * @return Collection + */ + public function getPostings(): Collection + { + return $this->postings; + } + + public function addPosting(Posting $posting): static + { + if (!$this->postings->contains($posting)) { + $this->postings->add($posting); + $posting->setProduct($this); + } + + return $this; + } + + public function removePosting(Posting $posting): static + { + if ($this->postings->removeElement($posting)) { + // set the owning side to null (unless already changed) + if ($posting->getProduct() === $this) { + $posting->setProduct(null); + } + } + + return $this; + } } diff --git a/src/Entity/Sale.php b/src/Entity/Sale.php index 3b783a3..eb28520 100644 --- a/src/Entity/Sale.php +++ b/src/Entity/Sale.php @@ -34,6 +34,9 @@ class Sale #[ORM\Column(nullable: true)] private ?int $profit = null; + #[ORM\Column(type: "integer", nullable: false, options: ["unsigned" => true])] + protected int $quantity; + #[ORM\Column(type: Types::TEXT, nullable: true)] private ?string $comment = null; @@ -117,6 +120,16 @@ class Sale return $this; } + public function getQuantity(): int + { + return $this->quantity; + } + + public function setQuantity(int $quantity): void + { + $this->quantity = $quantity; + } + public function getComment(): ?string { return $this->comment; diff --git a/src/Entity/Task.php b/src/Entity/Task.php index 16f19e8..b4322d7 100644 --- a/src/Entity/Task.php +++ b/src/Entity/Task.php @@ -24,7 +24,7 @@ class Task private ?string $description = null; #[ORM\ManyToOne] - #[ORM\JoinColumn(nullable: false)] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private ?User $createdBy = null; #[ORM\ManyToOne] diff --git a/src/Factory/ContactFactory.php b/src/Factory/ContactFactory.php index 12bfff6..9e0d37f 100644 --- a/src/Factory/ContactFactory.php +++ b/src/Factory/ContactFactory.php @@ -55,7 +55,7 @@ final class ContactFactory extends ModelFactory 'phone' => self::faker()->phoneNumber(), 'email' => self::faker()->email(), 'partner' => PartnerFactory::random(), - 'createdBy' => UserFactory::random() + 'createdBy' => UserFactory::random(), ]; } diff --git a/src/Factory/DocumentFactory.php b/src/Factory/DocumentFactory.php index bcf3961..cb37989 100644 --- a/src/Factory/DocumentFactory.php +++ b/src/Factory/DocumentFactory.php @@ -50,7 +50,7 @@ final class DocumentFactory extends ModelFactory $randBool = (bool)random_int(0, 1); return [ 'createdBy' => UserFactory::random(), - 'documentObject' => DocumentObjectFactory::random(), + 'documentObject' => DocumentObjectFactory::createOne(), 'name' => self::faker()->randomElement(FakeValues::BUSINESS_FILE_NAMES), 'description' => self::faker()->text(), 'partner' => $randBool ? PartnerFactory::random() : null, diff --git a/src/Factory/PostingFactory.php b/src/Factory/PostingFactory.php index 2408932..6c06e59 100644 --- a/src/Factory/PostingFactory.php +++ b/src/Factory/PostingFactory.php @@ -55,6 +55,7 @@ final class PostingFactory extends ModelFactory 'partner' => PartnerFactory::random(), 'contact' => $randomBoolean ? ContactFactory::random() : null, 'sale' => !$random4 ? SaleFactory::random() : null, + 'product' => ProductFactory::random() ]; } diff --git a/src/Factory/SaleFactory.php b/src/Factory/SaleFactory.php index 1d3e97f..cc1af40 100644 --- a/src/Factory/SaleFactory.php +++ b/src/Factory/SaleFactory.php @@ -56,7 +56,8 @@ final class SaleFactory extends ModelFactory 'partner' => PartnerFactory::random(), 'product' => ProductFactory::random(), 'turnover' => $turnover, - 'profit' => $profit + 'profit' => $profit, + 'quantity' => self::faker()->randomNumber(3) ]; } diff --git a/src/Mapper/PostingApiToEntityMapper.php b/src/Mapper/PostingApiToEntityMapper.php index e2aaa99..6dfe30d 100644 --- a/src/Mapper/PostingApiToEntityMapper.php +++ b/src/Mapper/PostingApiToEntityMapper.php @@ -7,6 +7,7 @@ use App\ApiResource\PostingApi; use App\ApiResource\SaleApi; use App\Entity\Contact; use App\Entity\Partner; +use App\Entity\Product; use App\Entity\Sale; use App\Entity\User; use App\Entity\Posting; @@ -44,6 +45,12 @@ class PostingApiToEntityMapper implements MapperInterface $partner = $this->microMapper->map($dto->partner, Partner::class, [ MicroMapperInterface::MAX_DEPTH => 1, ]); + if ($dto->product === null) { + throw new \Exception('Partner missing'); + } + $product = $this->microMapper->map($dto->product, Product::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); $contact = null; if ($dto->contact) { assert($dto->contact instanceof ContactApi); @@ -61,7 +68,7 @@ class PostingApiToEntityMapper implements MapperInterface assert($partner instanceof Partner); assert($contact === null || $contact instanceof Contact); assert($sale === null || $sale instanceof Sale); - $entity = new Posting($user, $partner, $contact, $sale); + $entity = new Posting($user, $partner, $product, $contact, $sale); } if (!$entity) { diff --git a/src/Mapper/PostingEntityToApiMapper.php b/src/Mapper/PostingEntityToApiMapper.php index 4b2fb0f..38915aa 100644 --- a/src/Mapper/PostingEntityToApiMapper.php +++ b/src/Mapper/PostingEntityToApiMapper.php @@ -5,6 +5,7 @@ namespace App\Mapper; use App\ApiResource\CommentApi; use App\ApiResource\ContactApi; use App\ApiResource\PartnerApi; +use App\ApiResource\ProductApi; use App\ApiResource\UserApi; use App\ApiResource\PostingApi; use App\Entity\Comment; @@ -50,6 +51,12 @@ class PostingEntityToApiMapper implements MapperInterface $dto->partner = $this->microMapper->map($entity->getPartner(), PartnerApi::class, [ MicroMapperInterface::MAX_DEPTH => 1, ]); + $dto->partnerName = $entity->getPartner()?->getName(); + + $dto->product = $this->microMapper->map($entity->getProduct(), ProductApi::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); + $dto->productName = $entity->getProduct()?->getName(); $dto->contact = null; if ($entity->getContact() !== null) { diff --git a/src/Mapper/SaleApiToEntityMapper.php b/src/Mapper/SaleApiToEntityMapper.php index 5a46f47..5c73e1c 100644 --- a/src/Mapper/SaleApiToEntityMapper.php +++ b/src/Mapper/SaleApiToEntityMapper.php @@ -69,6 +69,7 @@ class SaleApiToEntityMapper implements MapperInterface assert($entity instanceof Sale); $entity->setTurnover($dto->turnover); $entity->setProfit($dto->profit); + $entity->setQuantity($dto->quantity); $entity->setComment($dto->comment); return $entity; diff --git a/src/Mapper/SaleEntityToApiMapper.php b/src/Mapper/SaleEntityToApiMapper.php index f4080b1..7db6f56 100644 --- a/src/Mapper/SaleEntityToApiMapper.php +++ b/src/Mapper/SaleEntityToApiMapper.php @@ -61,6 +61,7 @@ class SaleEntityToApiMapper implements MapperInterface $dto->turnover = $entity->getTurnover(); $dto->profit = $entity->getProfit(); + $dto->quantity = $entity->getQuantity(); $dto->comment = $entity->getComment(); $dto->createdAt = $entity->getCreatedAt(); diff --git a/tests/Functional/PartnerResourceTest.php b/tests/Functional/PartnerResourceTest.php index a293134..9d4a1e5 100644 --- a/tests/Functional/PartnerResourceTest.php +++ b/tests/Functional/PartnerResourceTest.php @@ -83,7 +83,6 @@ class PartnerResourceTest extends KernelTestCase ]) ->assertSuccessful() ->assertJsonMatches('"hydra:totalItems"', 2) - ->assertJsonMatches('"hydra:member"[1].name', 'test customer') ->content() ; diff --git a/tests/Functional/PostingResourceTest.php b/tests/Functional/PostingResourceTest.php index b1a1cc3..165d571 100644 --- a/tests/Functional/PostingResourceTest.php +++ b/tests/Functional/PostingResourceTest.php @@ -48,8 +48,8 @@ class PostingResourceTest extends KernelTestCase ); $partner = PartnerFactory::createOne(); + $product = ProductFactory::createOne(); $contact = ContactFactory::createOne(); - ProductFactory::createOne(); SaleFactory::createOne(); PostingFactory::createOne(); @@ -62,6 +62,7 @@ class PostingResourceTest extends KernelTestCase 'message' => 'world', 'partner' => '/api/partners/' . $partner->getId(), 'contact' => '/api/contacts/' . $contact->getId(), + 'product' => '/api/products/' . $product->getId(), ], 'headers' => [ 'Authorization' => 'Bearer ' . $token,