diff --git a/migrations/Version20240305164344.php b/migrations/Version20240308161707.php similarity index 98% rename from migrations/Version20240305164344.php rename to migrations/Version20240308161707.php index 3a1f3be..cd91d1e 100644 --- a/migrations/Version20240305164344.php +++ b/migrations/Version20240308161707.php @@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240305164344 extends AbstractMigration +final class Version20240308161707 extends AbstractMigration { public function getDescription(): string { @@ -36,7 +36,7 @@ final class Version20240305164344 extends AbstractMigration $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D739393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id)'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D73E7A1254A FOREIGN KEY (contact_id) REFERENCES contact (id)'); $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 `user` ADD CONSTRAINT FK_8D93D6493DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id)'); + $this->addSql('ALTER TABLE `user` ADD CONSTRAINT FK_8D93D6493DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id) ON DELETE SET NULL'); } public function down(Schema $schema): void diff --git a/src/ApiResource/PartnerApi.php b/src/ApiResource/PartnerApi.php index c5f6313..cba0a4e 100644 --- a/src/ApiResource/PartnerApi.php +++ b/src/ApiResource/PartnerApi.php @@ -79,6 +79,7 @@ class PartnerApi #[ApiProperty(writable: false)] public ?string $logoUrl = null; + #[ApiProperty(writable: false)] public ?\DateTimeImmutable $createdAt = null; /** diff --git a/src/ApiResource/UserApi.php b/src/ApiResource/UserApi.php index c0db4f3..bf05def 100644 --- a/src/ApiResource/UserApi.php +++ b/src/ApiResource/UserApi.php @@ -14,6 +14,7 @@ use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\GetCollection; use ApiPlatform\Metadata\Patch; use ApiPlatform\Metadata\Post; +use App\Entity\MediaObject; use App\Entity\User; use App\State\EntityClassDtoStateProcessor; use App\State\EntityToDtoStateProvider; @@ -57,6 +58,8 @@ class UserApi #[Assert\NotBlank] public ?string $lastName = null; + public ?MediaObject $image = null; + /** * The plaintext password when being set or changed. */ diff --git a/src/Entity/User.php b/src/Entity/User.php index 1641bbe..534dd7c 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -27,7 +27,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column(length: 255)] private ?string $lastName = null; - #[ORM\ManyToOne] + #[ORM\ManyToOne(targetEntity: MediaObject::class)] + #[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")] private ?MediaObject $image = null; #[ORM\Column] diff --git a/src/Mapper/UserApiToEntityMapper.php b/src/Mapper/UserApiToEntityMapper.php index dfe8f84..1d0025f 100644 --- a/src/Mapper/UserApiToEntityMapper.php +++ b/src/Mapper/UserApiToEntityMapper.php @@ -17,10 +17,10 @@ use Symfonycasts\MicroMapper\MicroMapperInterface; class UserApiToEntityMapper implements MapperInterface { public function __construct( - private UserRepository $userRepository, + private UserRepository $repository, private UserPasswordHasherInterface $userPasswordHasher, - private MicroMapperInterface $microMapper, - private PropertyAccessorInterface $propertyAccessor, + private MicroMapperInterface $microMapper, + private PropertyAccessorInterface $propertyAccessor, ) { } @@ -30,12 +30,12 @@ class UserApiToEntityMapper implements MapperInterface $dto = $from; assert($dto instanceof UserApi); - $userEntity = $dto->id ? $this->userRepository->find($dto->id) : new User(); - if (!$userEntity) { + $entity = $dto->id ? $this->repository->find($dto->id) : new User(); + if (!$entity) { throw new \Exception('User not found'); } - return $userEntity; + return $entity; } public function populate(object $from, object $to, array $context): object @@ -53,11 +53,6 @@ class UserApiToEntityMapper implements MapperInterface } $userPostsEntities = new ArrayCollection(); - foreach ($dto->posts as $userPostApi) { - $userPostsEntities[] = $this->microMapper->map($userPostApi, Posting::class, [ - MicroMapperInterface::MAX_DEPTH => 1, - ]); - } $this->propertyAccessor->setValue($entity, 'postings', $userPostsEntities); return $entity; diff --git a/src/Mapper/UserEntityToApiMapper.php b/src/Mapper/UserEntityToApiMapper.php index e916509..ebc0527 100644 --- a/src/Mapper/UserEntityToApiMapper.php +++ b/src/Mapper/UserEntityToApiMapper.php @@ -40,6 +40,7 @@ class UserEntityToApiMapper implements MapperInterface $dto->email = $entity->getEmail(); $dto->firstName = $entity->getFirstName(); $dto->lastName = $entity->getLastName(); + $dto->image = $entity->getImage(); return $dto; } diff --git a/src/Serializer/MediaObjectNormalizer.php b/src/Serializer/MediaObjectNormalizer.php index 6b97d37..fe1ed5e 100644 --- a/src/Serializer/MediaObjectNormalizer.php +++ b/src/Serializer/MediaObjectNormalizer.php @@ -44,7 +44,7 @@ final class MediaObjectNormalizer implements NormalizerInterface, SerializerAwar if (isset($context[self::ALREADY_CALLED])) { //return false; } - + // return $data instanceof MediaObject; return $this->normalizer->supportsNormalization($data, $format, $context); } diff --git a/tests/Functional/CommentResourceTest.php b/tests/Functional/CommentResourceTest.php index 350492e..8cd0292 100644 --- a/tests/Functional/CommentResourceTest.php +++ b/tests/Functional/CommentResourceTest.php @@ -82,7 +82,6 @@ class CommentResourceTest extends KernelTestCase ], ]) ->assertSuccessful() - ->dd() ; } } \ No newline at end of file diff --git a/tests/Functional/ContactResourceTest.php b/tests/Functional/ContactResourceTest.php index fabae5e..8484b5d 100644 --- a/tests/Functional/ContactResourceTest.php +++ b/tests/Functional/ContactResourceTest.php @@ -56,7 +56,7 @@ class ContactResourceTest extends KernelTestCase 'lastName' => 'Test', 'partner' => '/api/partners/' . $partner->getId(), 'birthday' => '1984-02-10', - 'image' => '/api/media/' . $mediaObject->getId(), + 'image' => '/api/medias/' . $mediaObject->getId(), 'position' => 'CEO', 'phone' => '123456789', 'email' => 'peter@test2.de', @@ -80,7 +80,7 @@ class ContactResourceTest extends KernelTestCase ; $this->browser() - ->delete('/api/media/' . $mediaObject->getId(), [ + ->delete('/api/medias/' . $mediaObject->getId(), [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, ], diff --git a/tests/Functional/MediaObjectResourceTest.php b/tests/Functional/MediaObjectResourceTest.php index ab81ccc..d956cae 100644 --- a/tests/Functional/MediaObjectResourceTest.php +++ b/tests/Functional/MediaObjectResourceTest.php @@ -53,7 +53,16 @@ class MediaObjectResourceTest extends KernelTestCase $token = $this->JWTManager->create($user->object()); $this->browser() - ->post('/api/media', [ + ->get('/api/medias', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ], + ]) + ->assertSuccessful() + ; + + $this->browser() + ->post('/api/medias', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'multipart/form-data' @@ -67,22 +76,24 @@ class MediaObjectResourceTest extends KernelTestCase $partner = PartnerFactory::createOne(); $this->browser() - ->patch('/api/partner/' . $partner->getId(), [ + ->patch('/api/partners/' . $partner->getId(), + [ + 'json' => [ + 'logo' => '/api/medias/1' + ], 'headers' => [ 'Authorization' => 'Bearer ' . $token, + 'Content-Type' => 'application/merge-patch+json' ], - 'json' => [ - 'logo' => '/api/media/1' - ] ]) ->assertSuccessful() ; $this->browser() - ->delete('/api/media/1', [ + ->delete('/api/medias/1', + [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, - 'Content-Type' => 'multipart/form-data' ], ]) ->assertSuccessful() @@ -102,10 +113,9 @@ class MediaObjectResourceTest extends KernelTestCase ); $token = $this->JWTManager->create($user->object()); $this->browser() - ->delete('/api/media/' . $mediaObject->getId(), [ + ->delete('/api/medias/' . $mediaObject->getId(), [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, - 'Content-Type' => 'multipart/form-data' ], ]) ->assertSuccessful() diff --git a/tests/Functional/PartnerResourceTest.php b/tests/Functional/PartnerResourceTest.php index f728b5d..7abfea0 100644 --- a/tests/Functional/PartnerResourceTest.php +++ b/tests/Functional/PartnerResourceTest.php @@ -55,7 +55,7 @@ class PartnerResourceTest extends KernelTestCase 'city' => 'test city', 'country' => 'test country', 'website' => 'wwe.test.de', - 'logo' => '/api/media/' . $mediaObject->getId(), + 'logo' => '/api/medias/' . $mediaObject->getId(), ], 'headers' => [ 'Authorization' => 'Bearer ' . $token, @@ -79,7 +79,7 @@ class PartnerResourceTest extends KernelTestCase ; $this->browser() - ->delete('/api/media/' . $mediaObject->getId(), [ + ->delete('/api/medias/' . $mediaObject->getId(), [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, ], diff --git a/tests/Functional/ProductResourceTest.php b/tests/Functional/ProductResourceTest.php index bf988c1..45a1b6b 100644 --- a/tests/Functional/ProductResourceTest.php +++ b/tests/Functional/ProductResourceTest.php @@ -54,7 +54,7 @@ class ProductResourceTest extends KernelTestCase 'json' => [ 'name' => 'Test', 'description' => 'more test', - 'image' => '/api/media/' . $mediaObject->getId(), + 'image' => '/api/medias/' . $mediaObject->getId(), ], 'headers' => [ 'Authorization' => 'Bearer ' . $token, diff --git a/tests/fixtures/1176_upload.png b/tests/fixtures/1176_upload.png deleted file mode 100644 index 54a8ef6..0000000 Binary files a/tests/fixtures/1176_upload.png and /dev/null differ