From 18c20b97f67c87c36c61d3fe780c34939f049711 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Mar 2024 17:48:55 +0100 Subject: [PATCH] media objects delete --- ...16094550.php => Version20240304164555.php} | 6 +-- src/Controller/DeleteMediaObjectAction.php | 29 +++++++++++ src/Entity/Contact.php | 2 +- src/Entity/MediaObject.php | 7 ++- src/Entity/Partner.php | 2 +- tests/Functional/ContactResourceTest.php | 9 ++++ tests/Functional/MediaObjectResourceTest.php | 48 +++++++++++++++++++ tests/Functional/PartnerResourceTest.php | 13 ++++- 8 files changed, 109 insertions(+), 7 deletions(-) rename migrations/{Version20240216094550.php => Version20240304164555.php} (98%) create mode 100644 src/Controller/DeleteMediaObjectAction.php diff --git a/migrations/Version20240216094550.php b/migrations/Version20240304164555.php similarity index 98% rename from migrations/Version20240216094550.php rename to migrations/Version20240304164555.php index 8d154a4..98b4bf2 100644 --- a/migrations/Version20240216094550.php +++ b/migrations/Version20240304164555.php @@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240216094550 extends AbstractMigration +final class Version20240304164555 extends AbstractMigration { public function getDescription(): string { @@ -29,8 +29,8 @@ final class Version20240216094550 extends AbstractMigration $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C7E3C61F9 FOREIGN KEY (owner_id) REFERENCES `user` (id)'); $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C9AE985F6 FOREIGN KEY (posting_id) REFERENCES posting (id)'); $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E6389393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id)'); - $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E6383DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id)'); - $this->addSql('ALTER TABLE partner ADD CONSTRAINT FK_312B3E16F98F144A FOREIGN KEY (logo_id) REFERENCES media_object (id)'); + $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 partner ADD CONSTRAINT FK_312B3E16F98F144A FOREIGN KEY (logo_id) REFERENCES media_object (id) ON DELETE SET NULL'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D737E3C61F9 FOREIGN KEY (owner_id) REFERENCES `user` (id)'); $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)'); diff --git a/src/Controller/DeleteMediaObjectAction.php b/src/Controller/DeleteMediaObjectAction.php new file mode 100644 index 0000000..9d69880 --- /dev/null +++ b/src/Controller/DeleteMediaObjectAction.php @@ -0,0 +1,29 @@ + + * @date 25.01.24 + */ + + +namespace App\Controller; + + +use App\Entity\MediaObject; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpKernel\Attribute\AsController; + +#[AsController] +final class DeleteMediaObjectAction extends AbstractController +{ + + public function __invoke( + MediaObject $mediaObject, + EntityManagerInterface $em, + ): null + { + $em->remove($mediaObject); + $em->flush(); + return null; + } +} \ No newline at end of file diff --git a/src/Entity/Contact.php b/src/Entity/Contact.php index e014c32..1c3dc04 100644 --- a/src/Entity/Contact.php +++ b/src/Entity/Contact.php @@ -30,7 +30,7 @@ class Contact private ?\DateTimeInterface $birthday = null; #[ORM\ManyToOne(targetEntity: MediaObject::class)] - #[ORM\JoinColumn(nullable: true)] + #[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")] private ?MediaObject $image = null; #[ORM\Column(length: 255, nullable: true)] diff --git a/src/Entity/MediaObject.php b/src/Entity/MediaObject.php index 7cea724..6075d64 100644 --- a/src/Entity/MediaObject.php +++ b/src/Entity/MediaObject.php @@ -9,11 +9,13 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Delete; use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\GetCollection; use ApiPlatform\Metadata\Post; use ApiPlatform\OpenApi\Model; use App\Controller\CreateMediaObjectAction; +use App\Controller\DeleteMediaObjectAction; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\Serializer\Annotation\Groups; @@ -49,7 +51,10 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich; ), validationContext: ['groups' => ['Default', 'media_object_create']], deserialize: false - ) + ), + new Delete( + controller: DeleteMediaObjectAction::class + ), ], normalizationContext: ['groups' => ['media_object:read']] )] diff --git a/src/Entity/Partner.php b/src/Entity/Partner.php index 2ff54c9..aca57d9 100644 --- a/src/Entity/Partner.php +++ b/src/Entity/Partner.php @@ -47,7 +47,7 @@ class Partner private Collection $contacts; #[ORM\ManyToOne(targetEntity: MediaObject::class)] - #[ORM\JoinColumn(nullable: true)] + #[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")] private ?MediaObject $logo = null; #[ORM\OneToMany(mappedBy: 'partner', targetEntity: Posting::class, orphanRemoval: true)] diff --git a/tests/Functional/ContactResourceTest.php b/tests/Functional/ContactResourceTest.php index c6f3ce8..a71b35b 100644 --- a/tests/Functional/ContactResourceTest.php +++ b/tests/Functional/ContactResourceTest.php @@ -78,5 +78,14 @@ class ContactResourceTest extends KernelTestCase ->assertJsonMatches('"hydra:totalItems"', 1) ->assertJsonMatches('"hydra:member"[0].position', 'CEO') ; + + $this->browser() + ->delete('/api/medias/' . $mediaObject->getId(), [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ], + ]) + ->assertSuccessful() + ; } } \ No newline at end of file diff --git a/tests/Functional/MediaObjectResourceTest.php b/tests/Functional/MediaObjectResourceTest.php index b0c9ec8..23ac1c3 100644 --- a/tests/Functional/MediaObjectResourceTest.php +++ b/tests/Functional/MediaObjectResourceTest.php @@ -7,6 +7,8 @@ namespace App\Tests\Functional; +use App\Factory\MediaObjectLogoFactory; +use App\Factory\PartnerFactory; use App\Factory\UserFactory; use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -62,5 +64,51 @@ class MediaObjectResourceTest extends KernelTestCase ]) ->assertSuccessful() ; + + $partner = PartnerFactory::createOne(); + $this->browser() + ->patch('/api/partner/' . $partner->getId(), [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ], + 'json' => [ + 'logo' => '/api/medias/1' + ] + ]) + ->assertSuccessful() + ; + + $this->browser() + ->delete('/api/medias/1', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + 'Content-Type' => 'multipart/form-data' + ], + ]) + ->assertSuccessful() + ; + } + + public function testDeleteMediaObject() + { + $mediaObject = MediaObjectLogoFactory::createOne(); + $user = UserFactory::createOne( + [ + 'firstName' => 'Peter', + 'lastName' => 'Test', + 'password' => 'test', + 'email' => 'peter@test.de', + ] + ); + $token = $this->JWTManager->create($user->object()); + $this->browser() + ->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 8fd908a..7abfea0 100644 --- a/tests/Functional/PartnerResourceTest.php +++ b/tests/Functional/PartnerResourceTest.php @@ -44,7 +44,7 @@ class PartnerResourceTest extends KernelTestCase $mediaObject = MediaObjectLogoFactory::createOne(); $token = $this->JWTManager->create($user->object()); - $this->browser() + $response = $this->browser() ->post('/api/partners' , [ 'json' => [ 'name' => 'test customer', @@ -63,6 +63,7 @@ class PartnerResourceTest extends KernelTestCase ] ]) ->assertSuccessful() + ->content() ; $this->browser() @@ -74,6 +75,16 @@ class PartnerResourceTest extends KernelTestCase ->assertSuccessful() ->assertJsonMatches('"hydra:totalItems"', 1) ->assertJsonMatches('"hydra:member"[0].name', 'test customer') + ->content() + ; + + $this->browser() + ->delete('/api/medias/' . $mediaObject->getId(), [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ], + ]) + ->assertSuccessful() ; } } \ No newline at end of file