From 52e2dbe951eae65e276923fd1817636d06e094fc Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 28 Mar 2024 11:41:59 +0100 Subject: [PATCH] created by for entities --- ...27103616.php => Version20240328095507.php} | 2 +- src/ApiResource/CommentApi.php | 6 +-- src/ApiResource/ContactApi.php | 22 +++++++--- src/ApiResource/DocumentApi.php | 18 ++++---- src/ApiResource/PartnerApi.php | 16 ++++--- src/ApiResource/PostingApi.php | 6 +-- src/ApiResource/ProductApi.php | 16 ++++--- src/ApiResource/SaleApi.php | 6 +-- src/ApiResource/TaskApi.php | 6 +-- src/ApiResource/TaskNoteApi.php | 6 +-- src/ApiResource/UserApi.php | 6 +-- src/Controller/CreateDocumentObjectAction.php | 5 ++- src/Controller/CreateMediaObjectAction.php | 5 ++- src/DataFixtures/AppFixtures.php | 40 ++++++++++++++---- src/Entity/Contact.php | 16 ++++++- src/Entity/DocumentObject.php | 11 ++++- src/Entity/MediaObject.php | 11 ++++- src/Entity/Partner.php | 20 ++++++--- src/Entity/Product.php | 11 ++++- src/Factory/ContactFactory.php | 3 +- src/Factory/DocumentObjectFactory.php | 1 + src/Factory/MediaObjectContactFactory.php | 3 +- src/Factory/MediaObjectLogoFactory.php | 3 +- src/Factory/MediaObjectProductFactory.php | 3 +- src/Factory/MediaObjectUserFactory.php | 3 +- src/Factory/PartnerFactory.php | 1 + src/Factory/ProductFactory.php | 1 + src/Factory/TaskFactory.php | 2 +- src/Factory/UserFactory.php | 1 - src/Mapper/ContactApiToEntityMapper.php | 6 ++- src/Mapper/ContactEntityToApiMapper.php | 5 +++ src/Mapper/DocumentEntityToApiMapper.php | 9 ++-- src/Mapper/PartnerApiToEntityMapper.php | 12 +++++- src/Mapper/PartnerEntityToApiMapper.php | 5 +++ src/Mapper/ProductApiToEntityMapper.php | 17 ++++++-- src/Mapper/ProductEntityToApiMapper.php | 7 +++ tests/fixtures/1176_upload.png | Bin 0 -> 3989 bytes 37 files changed, 228 insertions(+), 83 deletions(-) rename migrations/{Version20240327103616.php => Version20240328095507.php} (99%) create mode 100644 tests/fixtures/1176_upload.png diff --git a/migrations/Version20240327103616.php b/migrations/Version20240328095507.php similarity index 99% rename from migrations/Version20240327103616.php rename to migrations/Version20240328095507.php index 74be294..67d38f5 100644 --- a/migrations/Version20240327103616.php +++ b/migrations/Version20240328095507.php @@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240327103616 extends AbstractMigration +final class Version20240328095507 extends AbstractMigration { public function getDescription(): string { diff --git a/src/ApiResource/CommentApi.php b/src/ApiResource/CommentApi.php index 2a6076f..0981d2a 100644 --- a/src/ApiResource/CommentApi.php +++ b/src/ApiResource/CommentApi.php @@ -40,9 +40,9 @@ use Symfony\Component\Validator\Constraints\NotBlank; new Patch( security: 'is_granted("EDIT", object)', ), - new Delete( - security: 'is_granted("ROLE_ADMIN")', - ) +// new Delete( +// security: 'is_granted("ROLE_ADMIN")', +// ) ], security: 'is_granted("ROLE_USER")', provider: EntityToDtoStateProvider::class, diff --git a/src/ApiResource/ContactApi.php b/src/ApiResource/ContactApi.php index 3da4c31..78b6f9e 100644 --- a/src/ApiResource/ContactApi.php +++ b/src/ApiResource/ContactApi.php @@ -28,18 +28,20 @@ use Symfony\Component\Validator\Constraints\NotBlank; shortName: 'Contact', operations: [ new Get( - security: 'is_granted("ROLE_USER")' - ), - new GetCollection(), - new Post( security: 'is_granted("ROLE_USER")', ), - new Patch( + new GetCollection( security: 'is_granted("ROLE_USER")', ), - new Delete( + new Post( + security: 'is_granted("ROLE_ADMIN")', + ), + new Patch( security: 'is_granted("ROLE_ADMIN")', - ) + ), +// new Delete( +// security: 'is_granted("ROLE_ADMIN")', +// ) ], order: ['lastName' => 'ASC'], security: 'is_granted("ROLE_USER")', @@ -82,6 +84,12 @@ class ContactApi #[ApiProperty(writable: false)] public array $posts = []; + #[ApiProperty(writable: false)] + public ?UserApi $createdBy = null; + + #[ApiProperty(writable: false)] + public ?string $createdByName = null; + #[ApiProperty(writable: false)] public ?\DateTimeImmutable $createdAt = null; } \ No newline at end of file diff --git a/src/ApiResource/DocumentApi.php b/src/ApiResource/DocumentApi.php index ca73dcf..c14b748 100644 --- a/src/ApiResource/DocumentApi.php +++ b/src/ApiResource/DocumentApi.php @@ -43,9 +43,9 @@ use Symfony\Component\Validator\Constraints\NotBlank; new Patch( security: 'is_granted("ROLE_USER")', ), - new Delete( - security: 'is_granted("ROLE_ADMIN")', - ) +// new Delete( +// security: 'is_granted("ROLE_ADMIN")', +// ) ], security: 'is_granted("ROLE_USER")', provider: EntityToDtoStateProvider::class, @@ -58,12 +58,6 @@ class DocumentApi #[ApiProperty(readable: false, writable: false, identifier: true)] public ?int $id = null; - #[ApiProperty(writable: false)] - public ?UserApi $createdBy = null; - - #[ApiProperty(writable: false)] - public ?string $createdByName = null; - #[NotBlank] public ?string $name = null; @@ -85,6 +79,12 @@ class DocumentApi #[ApiProperty(writable: false)] public ?string $documentUrl = null; + #[ApiProperty(writable: false)] + public ?UserApi $createdBy = null; + + #[ApiProperty(writable: false)] + public ?string $createdByName = null; + #[ApiProperty(writable: false)] public ?\DateTimeImmutable $createdAt = null; } \ No newline at end of file diff --git a/src/ApiResource/PartnerApi.php b/src/ApiResource/PartnerApi.php index 686269f..9173afc 100644 --- a/src/ApiResource/PartnerApi.php +++ b/src/ApiResource/PartnerApi.php @@ -34,14 +34,14 @@ use Symfony\Component\Validator\Constraints\NotBlank; ), new GetCollection(), new Post( - security: 'is_granted("ROLE_USER")', + security: 'is_granted("ROLE_ADMIN")', ), new Patch( - security: 'is_granted("ROLE_USER")', - ), - new Delete( security: 'is_granted("ROLE_ADMIN")', - ) + ), +// new Delete( +// security: 'is_granted("ROLE_ADMIN")', +// ) ], order: ['name' => 'ASC'], security: 'is_granted("ROLE_USER")', @@ -81,6 +81,12 @@ class PartnerApi #[ApiProperty(writable: false)] public ?string $logoUrl = null; + #[ApiProperty(writable: false)] + public ?UserApi $createdBy = null; + + #[ApiProperty(writable: false)] + public ?string $createdByName = null; + #[ApiProperty(writable: false)] public ?\DateTimeImmutable $createdAt = null; diff --git a/src/ApiResource/PostingApi.php b/src/ApiResource/PostingApi.php index 6398aa8..412312f 100644 --- a/src/ApiResource/PostingApi.php +++ b/src/ApiResource/PostingApi.php @@ -44,9 +44,9 @@ use Symfony\Component\Validator\Constraints\NotBlank; denormalizationContext: ['groups' => 'posting:patch'], security: 'is_granted("EDIT", object)', ), - new Delete( - security: 'is_granted("ROLE_ADMIN")', - ) +// new Delete( +// security: 'is_granted("ROLE_ADMIN")', +// ) ], order: ['createdAt' => 'DESC'], security: 'is_granted("ROLE_USER")', diff --git a/src/ApiResource/ProductApi.php b/src/ApiResource/ProductApi.php index e1b22cd..9e4aa7d 100644 --- a/src/ApiResource/ProductApi.php +++ b/src/ApiResource/ProductApi.php @@ -33,14 +33,14 @@ use Symfony\Component\Validator\Constraints\NotBlank; ), new GetCollection(), new Post( - security: 'is_granted("ROLE_USER")', + security: 'is_granted("ROLE_ADMIN")', ), new Patch( - security: 'is_granted("ROLE_USER")', - ), - new Delete( security: 'is_granted("ROLE_ADMIN")', - ) + ), +// new Delete( +// security: 'is_granted("ROLE_ADMIN")', +// ) ], order: ['name' => 'ASC'], security: 'is_granted("ROLE_USER")', @@ -66,6 +66,12 @@ class ProductApi #[ApiProperty(writable: false)] public ?string $imageUrl = null; + #[ApiProperty(writable: false)] + public ?UserApi $createdBy = null; + + #[ApiProperty(writable: false)] + public ?string $createdByName = null; + public ?\DateTimeImmutable $createdAt = null; } \ No newline at end of file diff --git a/src/ApiResource/SaleApi.php b/src/ApiResource/SaleApi.php index 008f9c3..4984e74 100644 --- a/src/ApiResource/SaleApi.php +++ b/src/ApiResource/SaleApi.php @@ -37,9 +37,9 @@ use Symfony\Component\Validator\Constraints\NotBlank; new Patch( security: 'is_granted("EDIT", object)', ), - new Delete( - security: 'is_granted("ROLE_ADMIN")', - ) +// new Delete( +// security: 'is_granted("ROLE_ADMIN")', +// ) ], order: ['createdAt' => 'DESC'], security: 'is_granted("ROLE_USER")', diff --git a/src/ApiResource/TaskApi.php b/src/ApiResource/TaskApi.php index 8362c69..5f7cdbb 100644 --- a/src/ApiResource/TaskApi.php +++ b/src/ApiResource/TaskApi.php @@ -40,9 +40,9 @@ use Symfony\Component\Validator\Constraints as Assert; new Patch( security: 'is_granted("ROLE_USER")' ), - new Delete( - security: 'is_granted("ROLE_USER")', - ) +// new Delete( +// security: 'is_granted("ROLE_USER")', +// ) ], order: ['dueAt' => 'DESC'], security: 'is_granted("ROLE_USER")', diff --git a/src/ApiResource/TaskNoteApi.php b/src/ApiResource/TaskNoteApi.php index c21e1c4..60e3c6d 100644 --- a/src/ApiResource/TaskNoteApi.php +++ b/src/ApiResource/TaskNoteApi.php @@ -36,9 +36,9 @@ use Symfony\Component\Validator\Constraints\NotBlank; new Patch( security: 'is_granted("EDIT", object)', ), - new Delete( - security: 'is_granted("ROLE_ADMIN")', - ) +// new Delete( +// security: 'is_granted("ROLE_ADMIN")', +// ) ], security: 'is_granted("ROLE_USER")', provider: EntityToDtoStateProvider::class, diff --git a/src/ApiResource/UserApi.php b/src/ApiResource/UserApi.php index 948c123..ef44464 100644 --- a/src/ApiResource/UserApi.php +++ b/src/ApiResource/UserApi.php @@ -36,9 +36,9 @@ use Symfony\Component\Validator\Constraints as Assert; security: 'is_granted("ROLE_ADMIN")', validationContext: ['groups' => ['Default', 'postValidation']] ), - new Patch( - security: 'is_granted("is_granted("EDIT", object)")' - ), +// new Patch( +// security: 'is_granted("is_granted("EDIT", object)")' +// ), ], security: 'is_granted("ROLE_USER")', provider: EntityToDtoStateProvider::class, diff --git a/src/Controller/CreateDocumentObjectAction.php b/src/Controller/CreateDocumentObjectAction.php index 2fedef9..5c99562 100644 --- a/src/Controller/CreateDocumentObjectAction.php +++ b/src/Controller/CreateDocumentObjectAction.php @@ -9,6 +9,7 @@ namespace App\Controller; use App\Entity\DocumentObject; +use App\Entity\User; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Attribute\AsController; @@ -24,7 +25,9 @@ final class CreateDocumentObjectAction extends AbstractController throw new BadRequestHttpException('"file" is required'); } - $mediaObject = new DocumentObject(); + $user = $this->getUser(); + assert($user instanceof User); + $mediaObject = new DocumentObject($user); $mediaObject->file = $uploadedFile; return $mediaObject; diff --git a/src/Controller/CreateMediaObjectAction.php b/src/Controller/CreateMediaObjectAction.php index cddf6dd..64ad042 100644 --- a/src/Controller/CreateMediaObjectAction.php +++ b/src/Controller/CreateMediaObjectAction.php @@ -9,6 +9,7 @@ namespace App\Controller; use App\Entity\MediaObject; +use App\Entity\User; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Attribute\AsController; @@ -24,7 +25,9 @@ final class CreateMediaObjectAction extends AbstractController throw new BadRequestHttpException('"file" is required'); } - $mediaObject = new MediaObject(); + $user = $this->getUser(); + assert($user instanceof User); + $mediaObject = new MediaObject($user); $mediaObject->file = $uploadedFile; return $mediaObject; diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index 0348a6e..c0f7f42 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -52,7 +52,7 @@ class AppFixtures extends Fixture } } - MediaObjectUserFactory::createMany(20); + $adminD = UserFactory::createOne( [ 'email' => 'd.knudsen@spawntree.de', @@ -62,6 +62,12 @@ 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()); $adminF = UserFactory::createOne( [ @@ -69,24 +75,40 @@ class AppFixtures extends Fixture 'firstName' => 'Florian', 'lastName' => 'Eisenmenger', 'password' => 'test', + 'image' => MediaObjectUserFactory::random() ] ); $adminF->setRoles(['ROLE_ADMIN']); - UserFactory::createMany(10); - MediaObjectLogoFactory::createMany(50); - PartnerFactory::createMany(100); - MediaObjectContactFactory::createMany(50); - ContactFactory::createMany(200); - ProductFactory::createMany(100); + UserFactory::createMany(10, + [ + 'image' => MediaObjectUserFactory::random() + ] + ); + + PartnerFactory::createMany(100, + [ + 'createdBy' => $adminD + ] + ); + + ContactFactory::createMany(200, + [ + 'createdBy' => $adminD + ] + ); + + ProductFactory::createMany(100, + [ + 'createdBy' => $adminD + ] + ); SaleFactory::createMany(50); PostingFactory::createMany(200); CommentFactory::createMany(300); - MediaObjectProductFactory::createMany(50); TaskFactory::createMany(50); TaskNoteFactory::createMany(100); PartnerFollowFactory::createMany(100); - DocumentObjectFactory::createMany(50); DocumentFactory::createMany(50); } } diff --git a/src/Entity/Contact.php b/src/Entity/Contact.php index 673fa2e..bc9137d 100644 --- a/src/Entity/Contact.php +++ b/src/Entity/Contact.php @@ -43,15 +43,19 @@ class Contact #[ORM\Column(length: 255, nullable: true)] private ?string $email = null; + #[ORM\JoinColumn(nullable: false)] + private ?User $createdBy = null; + #[ORM\Column] private ?\DateTimeImmutable $createdAt = null; #[ORM\OneToMany(mappedBy: 'contact', targetEntity: Posting::class)] private Collection $posts; - public function __construct(Partner $partner) + public function __construct(Partner $partner, User $createdBy) { $this->partner = $partner; + $this->createdBy = $createdBy; $this->createdAt = new \DateTimeImmutable(); $this->posts = new ArrayCollection(); } @@ -153,6 +157,16 @@ class Contact return $this; } + public function getCreatedBy(): ?User + { + return $this->createdBy; + } + + public function setCreatedBy(?User $createdBy): void + { + $this->createdBy = $createdBy; + } + public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; diff --git a/src/Entity/DocumentObject.php b/src/Entity/DocumentObject.php index 48edfb7..ccc4c00 100644 --- a/src/Entity/DocumentObject.php +++ b/src/Entity/DocumentObject.php @@ -72,11 +72,15 @@ class DocumentObject #[ORM\Column(nullable: true)] public ?string $filePath = null; + #[ORM\JoinColumn(nullable: false)] + private ?User $createdBy = null; + #[ORM\Column] private ?\DateTimeImmutable $createdAt = null; - public function __construct() + public function __construct(User $createdBy) { + $this->createdBy = $createdBy; $this->createdAt = new \DateTimeImmutable(); } @@ -100,6 +104,11 @@ class DocumentObject return $this->filePath; } + public function getCreatedBy(): ?User + { + return $this->createdBy; + } + public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; diff --git a/src/Entity/MediaObject.php b/src/Entity/MediaObject.php index 9a357e4..fe417db 100644 --- a/src/Entity/MediaObject.php +++ b/src/Entity/MediaObject.php @@ -72,11 +72,15 @@ class MediaObject #[ORM\Column(nullable: true)] public ?string $filePath = null; + #[ORM\JoinColumn(nullable: false)] + private ?User $createdBy = null; + #[ORM\Column] private ?\DateTimeImmutable $createdAt = null; - public function __construct() + public function __construct(User $createdBy) { + $this->createdBy = $createdBy; $this->createdAt = new \DateTimeImmutable(); } @@ -100,6 +104,11 @@ class MediaObject return $this->filePath; } + public function getCreatedBy(): ?User + { + return $this->createdBy; + } + public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; diff --git a/src/Entity/Partner.php b/src/Entity/Partner.php index e3825cc..934b975 100644 --- a/src/Entity/Partner.php +++ b/src/Entity/Partner.php @@ -44,17 +44,19 @@ class Partner #[ORM\Column(length: 255, nullable: true)] private ?string $website = null; + #[ORM\ManyToOne(targetEntity: MediaObject::class)] + #[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")] + private ?MediaObject $logo = null; + + #[ORM\JoinColumn(nullable: false)] + private ?User $createdBy = null; + #[ORM\Column] private ?\DateTimeImmutable $createdAt = null; #[ORM\OneToMany(mappedBy: 'partner', targetEntity: Contact::class)] private Collection $contacts; - #[ORM\ManyToOne(targetEntity: MediaObject::class)] - #[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")] - private ?MediaObject $logo = null; - - //#[ORM\OneToMany(mappedBy: 'partner', targetEntity: Posting::class, orphanRemoval: true)] #[ORM\OneToMany(mappedBy: 'partner', targetEntity: Posting::class)] private Collection $postings; @@ -67,8 +69,9 @@ class Partner #[ORM\OneToMany(mappedBy: 'partner', targetEntity: PartnerFollow::class)] private Collection $partnerFollows; - public function __construct() + public function __construct(User $createdBy) { + $this->createdBy = $createdBy; $this->createdAt = new \DateTimeImmutable(); $this->contacts = new ArrayCollection(); $this->postings = new ArrayCollection(); @@ -177,6 +180,11 @@ class Partner return $this->createdAt; } + public function getCreatedBy(): ?User + { + return $this->createdBy; + } + public function setCreatedAt(?\DateTimeImmutable $createdAt): void { $this->createdAt = $createdAt; diff --git a/src/Entity/Product.php b/src/Entity/Product.php index 2fe0f96..8a450f1 100644 --- a/src/Entity/Product.php +++ b/src/Entity/Product.php @@ -26,6 +26,9 @@ class Product #[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")] private ?MediaObject $image = null; + #[ORM\JoinColumn(nullable: false)] + private ?User $createdBy = null; + #[ORM\Column] private ?\DateTimeImmutable $createdAt = null; @@ -35,8 +38,9 @@ class Product #[ORM\OneToMany(mappedBy: 'product', targetEntity: Document::class)] private Collection $documentObjects; - public function __construct() + public function __construct(User $createdBy) { + $this->createdBy = $createdBy; $this->createdAt = new \DateTimeImmutable(); $this->sales = new ArrayCollection(); $this->documentObjects = new ArrayCollection(); @@ -83,6 +87,11 @@ class Product return $this; } + public function getCreatedBy(): ?User + { + return $this->createdBy; + } + public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; diff --git a/src/Factory/ContactFactory.php b/src/Factory/ContactFactory.php index 83946c0..736e960 100644 --- a/src/Factory/ContactFactory.php +++ b/src/Factory/ContactFactory.php @@ -55,7 +55,8 @@ final class ContactFactory extends ModelFactory 'phone' => self::faker()->phoneNumber(), 'email' => self::faker()->email(), 'partner' => PartnerFactory::random(), - 'image' => MediaObjectContactFactory::random() + 'image' => MediaObjectContactFactory::random(), + 'createdBy' => UserFactory::random() ]; } diff --git a/src/Factory/DocumentObjectFactory.php b/src/Factory/DocumentObjectFactory.php index 23e2978..f1797c3 100644 --- a/src/Factory/DocumentObjectFactory.php +++ b/src/Factory/DocumentObjectFactory.php @@ -62,6 +62,7 @@ final class DocumentObjectFactory extends ModelFactory $randBool = (bool)random_int(0, 1); return [ 'file' => new ReplacingFile($randomFile), + 'createdBy' => UserFactory::random() ]; } diff --git a/src/Factory/MediaObjectContactFactory.php b/src/Factory/MediaObjectContactFactory.php index 2ea0cb7..0ccf9df 100644 --- a/src/Factory/MediaObjectContactFactory.php +++ b/src/Factory/MediaObjectContactFactory.php @@ -62,7 +62,8 @@ final class MediaObjectContactFactory extends ModelFactory } return [ - 'file' => new ReplacingFile($randomFile) + 'file' => new ReplacingFile($randomFile), + 'createdBy' => UserFactory::random() ]; } diff --git a/src/Factory/MediaObjectLogoFactory.php b/src/Factory/MediaObjectLogoFactory.php index c9df3da..91cfe1a 100644 --- a/src/Factory/MediaObjectLogoFactory.php +++ b/src/Factory/MediaObjectLogoFactory.php @@ -62,7 +62,8 @@ final class MediaObjectLogoFactory extends ModelFactory } return [ - 'file' => new ReplacingFile($randomFile) + 'file' => new ReplacingFile($randomFile), + 'createdBy' => UserFactory::random() ]; } diff --git a/src/Factory/MediaObjectProductFactory.php b/src/Factory/MediaObjectProductFactory.php index 9ebf8fc..f27793b 100644 --- a/src/Factory/MediaObjectProductFactory.php +++ b/src/Factory/MediaObjectProductFactory.php @@ -62,7 +62,8 @@ final class MediaObjectProductFactory extends ModelFactory } return [ - 'file' => new ReplacingFile($randomFile) + 'file' => new ReplacingFile($randomFile), + 'createdBy' => UserFactory::random() ]; } diff --git a/src/Factory/MediaObjectUserFactory.php b/src/Factory/MediaObjectUserFactory.php index 1b3bbff..2f37ff5 100644 --- a/src/Factory/MediaObjectUserFactory.php +++ b/src/Factory/MediaObjectUserFactory.php @@ -62,7 +62,8 @@ final class MediaObjectUserFactory extends ModelFactory } return [ - 'file' => new ReplacingFile($randomFile) + 'file' => new ReplacingFile($randomFile), + 'createdBy' => UserFactory::random() ]; } diff --git a/src/Factory/PartnerFactory.php b/src/Factory/PartnerFactory.php index 8a95d81..e557b29 100644 --- a/src/Factory/PartnerFactory.php +++ b/src/Factory/PartnerFactory.php @@ -60,6 +60,7 @@ final class PartnerFactory extends ModelFactory 'country' => self::faker()->randomElement(FakeValues::COUNTRIES), 'logo' => MediaObjectLogoFactory::random(), 'website' => 'https://spawntree.de/', + 'createdBy' => UserFactory::random() ]; } diff --git a/src/Factory/ProductFactory.php b/src/Factory/ProductFactory.php index f52fe58..912a7e4 100644 --- a/src/Factory/ProductFactory.php +++ b/src/Factory/ProductFactory.php @@ -51,6 +51,7 @@ final class ProductFactory extends ModelFactory 'name' => self::faker()->randomElement(FakeValues::PRODUCTS), 'description' => self::faker()->text(), 'image' => MediaObjectProductFactory::createOne(), + 'createdBy' => UserFactory::random() ]; } diff --git a/src/Factory/TaskFactory.php b/src/Factory/TaskFactory.php index 28c6131..4a3338e 100644 --- a/src/Factory/TaskFactory.php +++ b/src/Factory/TaskFactory.php @@ -52,7 +52,7 @@ final class TaskFactory extends ModelFactory return [ 'headline' => self::faker()->randomElement(FakeValues::TASKS), 'description' => self::faker()->sentence(), - 'createdBy' => UserFactory::new(), + 'createdBy' => UserFactory::random(), 'assignedTo' => UserFactory::random(), 'partner' => PartnerFactory::random(), 'contact' => $randomBoolean ? ContactFactory::random() : null, diff --git a/src/Factory/UserFactory.php b/src/Factory/UserFactory.php index fe8b75b..845be9d 100644 --- a/src/Factory/UserFactory.php +++ b/src/Factory/UserFactory.php @@ -52,7 +52,6 @@ final class UserFactory extends ModelFactory 'email' => self::faker()->email(), 'firstName' => self::faker()->firstName(), 'lastName' => self::faker()->lastName(), - 'image' => MediaObjectUserFactory::random(), 'password' => "test", 'roles' => [], ]; diff --git a/src/Mapper/ContactApiToEntityMapper.php b/src/Mapper/ContactApiToEntityMapper.php index 931cef0..68b7b60 100644 --- a/src/Mapper/ContactApiToEntityMapper.php +++ b/src/Mapper/ContactApiToEntityMapper.php @@ -5,6 +5,7 @@ namespace App\Mapper; use App\ApiResource\ContactApi; use App\Entity\Contact; use App\Entity\Partner; +use App\Entity\User; use App\Repository\ContactRepository; use App\Repository\PartnerRepository; use Symfony\Bundle\SecurityBundle\Security; @@ -17,6 +18,7 @@ class ContactApiToEntityMapper implements MapperInterface { public function __construct( private ContactRepository $repository, + private Security $security, private MicroMapperInterface $microMapper, ) { @@ -31,13 +33,15 @@ class ContactApiToEntityMapper implements MapperInterface if ($dto->id) { $entity = $this->repository->find($dto->id); } else { + $user = $this->security->getUser(); + assert($user instanceof User); if ($dto->partner === null) { throw new \Exception('Partner missing'); } $partner = $this->microMapper->map($dto->partner, Partner::class, [ MicroMapperInterface::MAX_DEPTH => 1, ]); - $entity = new Contact($partner); + $entity = new Contact($partner, $user); } diff --git a/src/Mapper/ContactEntityToApiMapper.php b/src/Mapper/ContactEntityToApiMapper.php index b6ed827..e357afb 100644 --- a/src/Mapper/ContactEntityToApiMapper.php +++ b/src/Mapper/ContactEntityToApiMapper.php @@ -5,6 +5,7 @@ namespace App\Mapper; use App\ApiResource\ContactApi; use App\ApiResource\PartnerApi; use App\ApiResource\PostingApi; +use App\ApiResource\UserApi; use App\Entity\Contact; use App\Entity\Posting; use App\Service\FileUrlService; @@ -53,6 +54,10 @@ class ContactEntityToApiMapper implements MapperInterface $dto->position = $entity->getPosition(); $dto->phone = $entity->getPhone(); $dto->email = $entity->getEmail(); + $dto->createdBy = $this->microMapper->map($entity->getCreatedBy(), UserApi::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); + $dto->createdByName = $entity->getCreatedBy()?->getFirstName() . ' ' . $entity->getCreatedBy()?->getLastName(); $dto->createdAt = $entity->getCreatedAt(); $dto->posts = array_map(function(Posting $posting) { diff --git a/src/Mapper/DocumentEntityToApiMapper.php b/src/Mapper/DocumentEntityToApiMapper.php index b7b0c62..9d5e5b9 100644 --- a/src/Mapper/DocumentEntityToApiMapper.php +++ b/src/Mapper/DocumentEntityToApiMapper.php @@ -43,10 +43,6 @@ class DocumentEntityToApiMapper implements MapperInterface assert($entity instanceof Document); assert($dto instanceof DocumentApi); - $dto->createdBy = $this->microMapper->map($entity->getCreatedBy(), UserApi::class, [ - MicroMapperInterface::MAX_DEPTH => 1, - ]); - $dto->createdByName = $entity->getCreatedBy()?->getFirstName() . ' ' . $entity->getCreatedBy()?->getLastName(); $dto->name = $entity->getName(); $dto->description = $entity->getDescription(); @@ -66,6 +62,11 @@ class DocumentEntityToApiMapper implements MapperInterface $dto->documentObject = $entity->getDocumentObject(); $dto->documentUrl = $this->fileUrlService->getFileUrl($entity->getDocumentObject()); + + $dto->createdBy = $this->microMapper->map($entity->getCreatedBy(), UserApi::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); + $dto->createdByName = $entity->getCreatedBy()?->getFirstName() . ' ' . $entity->getCreatedBy()?->getLastName(); $dto->createdAt = $entity->getCreatedAt(); return $dto; diff --git a/src/Mapper/PartnerApiToEntityMapper.php b/src/Mapper/PartnerApiToEntityMapper.php index 2144ccd..af388f0 100644 --- a/src/Mapper/PartnerApiToEntityMapper.php +++ b/src/Mapper/PartnerApiToEntityMapper.php @@ -19,7 +19,8 @@ use Symfonycasts\MicroMapper\MicroMapperInterface; class PartnerApiToEntityMapper implements MapperInterface { public function __construct( - private PartnerRepository $repository + private PartnerRepository $repository, + private Security $security, ) { @@ -30,7 +31,14 @@ class PartnerApiToEntityMapper implements MapperInterface $dto = $from; assert($dto instanceof PartnerApi); - $entity = $dto->id ? $this->repository->find($dto->id) : new Partner(); + if ($dto->id) { + $entity = $this->repository->find($dto->id); + } else { + $user = $this->security->getUser(); + assert($user instanceof User); + $entity = new Partner($user); + } + if (!$entity) { throw new \Exception('Partner not found'); } diff --git a/src/Mapper/PartnerEntityToApiMapper.php b/src/Mapper/PartnerEntityToApiMapper.php index a2d3403..3a7b7e2 100644 --- a/src/Mapper/PartnerEntityToApiMapper.php +++ b/src/Mapper/PartnerEntityToApiMapper.php @@ -5,6 +5,7 @@ namespace App\Mapper; use App\ApiResource\ContactApi; use App\ApiResource\PartnerApi; use App\ApiResource\PostingApi; +use App\ApiResource\UserApi; use App\Entity\Contact; use App\Entity\Partner; use App\Entity\Posting; @@ -52,6 +53,10 @@ class PartnerEntityToApiMapper implements MapperInterface $dto->website = $entity->getWebsite(); $dto->logo = $entity->getLogo(); $dto->logoUrl = $this->fileUrlService->getFileUrl($entity->getLogo()); + $dto->createdBy = $this->microMapper->map($entity->getCreatedBy(), UserApi::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); + $dto->createdByName = $entity->getCreatedBy()?->getFirstName() . ' ' . $entity->getCreatedBy()?->getLastName(); $dto->createdAt = $entity->getCreatedAt(); $dto->posts = array_map(function(Posting $posting) { return $this->microMapper->map($posting, PostingApi::class, [ diff --git a/src/Mapper/ProductApiToEntityMapper.php b/src/Mapper/ProductApiToEntityMapper.php index f09fdf9..855b0c2 100644 --- a/src/Mapper/ProductApiToEntityMapper.php +++ b/src/Mapper/ProductApiToEntityMapper.php @@ -4,15 +4,19 @@ namespace App\Mapper; use App\ApiResource\ProductApi; use App\Entity\Product; +use App\Entity\User; use App\Repository\ProductRepository; +use Symfony\Bundle\SecurityBundle\Security; use Symfonycasts\MicroMapper\AsMapper; use Symfonycasts\MicroMapper\MapperInterface; +use Symfonycasts\MicroMapper\MicroMapperInterface; #[AsMapper(from: ProductApi::class, to: Product::class)] class ProductApiToEntityMapper implements MapperInterface { public function __construct( - private ProductRepository $repository + private ProductRepository $repository, + private Security $security, ) { @@ -23,9 +27,16 @@ class ProductApiToEntityMapper implements MapperInterface $dto = $from; assert($dto instanceof ProductApi); - $entity = $dto->id ? $this->repository->find($dto->id) : new Product(); + if ($dto->id) { + $entity = $this->repository->find($dto->id); + } else { + $user = $this->security->getUser(); + assert($user instanceof User); + $entity = new Product($user); + } + if (!$entity) { - throw new \Exception('Product not found'); + throw new \Exception('Partner not found'); } return $entity; diff --git a/src/Mapper/ProductEntityToApiMapper.php b/src/Mapper/ProductEntityToApiMapper.php index 356521c..f5dc4f1 100644 --- a/src/Mapper/ProductEntityToApiMapper.php +++ b/src/Mapper/ProductEntityToApiMapper.php @@ -3,15 +3,18 @@ namespace App\Mapper; use App\ApiResource\ProductApi; +use App\ApiResource\UserApi; use App\Entity\Product; use App\Service\FileUrlService; use Symfonycasts\MicroMapper\AsMapper; use Symfonycasts\MicroMapper\MapperInterface; +use Symfonycasts\MicroMapper\MicroMapperInterface; #[AsMapper(from: Product::class, to: ProductApi::class)] class ProductEntityToApiMapper implements MapperInterface { public function __construct( + private MicroMapperInterface $microMapper, private FileUrlService $fileUrlService ) { } @@ -38,6 +41,10 @@ class ProductEntityToApiMapper implements MapperInterface $dto->description = $entity->getDescription(); $dto->image = $entity->getImage(); $dto->imageUrl = $this->fileUrlService->getFileUrl($entity->getImage()); + $dto->createdBy = $this->microMapper->map($entity->getCreatedBy(), UserApi::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); + $dto->createdByName = $entity->getCreatedBy()?->getFirstName() . ' ' . $entity->getCreatedBy()?->getLastName(); return $dto; } } diff --git a/tests/fixtures/1176_upload.png b/tests/fixtures/1176_upload.png new file mode 100644 index 0000000000000000000000000000000000000000..54a8ef6d64ec964458d7f3b493346842901e721f GIT binary patch literal 3989 zcmeH~`8O1b7r+OZ%uM!?EjvA9Dv~8T!!Rbx7-C}bNMoC48+#$qWZyC*MYge%Y}qPf zOJ1R|Hre+*B~%RG-uJ!tM|{uuo^$WH+i&;YbMEKHVJu8I!6IM)0KjRAGQt7?EWcb9 z00-MIL#)Kv{4#9*2vZvl4vtT=mNUOXkT2?*KLC(%@Rzd4Visb5C4~c!xBzP(*MMMW z5&;k#9DKpUo9OT2>`S=dLvnk(r7Z#gur^@KY>>yt$80Po{xARU!2eJOfME}QEjeH* z0Bd0lU}0rD!45ph0Rlr#adL6<@bd9P1x^bIi-?{P6PJ*bl9rJ@`FQlrKF}kNT+0EW^3JUC93G z{>=MAF8M0s9|Lz+nt_OjUL-^?`S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