| @@ -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)'); | |||
| @@ -0,0 +1,29 @@ | |||
| <?php | |||
| /** | |||
| * @author Daniel Knudsen <d.knudsen@spawntree.de> | |||
| * @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; | |||
| } | |||
| } | |||
| @@ -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)] | |||
| @@ -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']] | |||
| )] | |||
| @@ -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)] | |||
| @@ -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() | |||
| ; | |||
| } | |||
| } | |||
| @@ -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() | |||
| ; | |||
| } | |||
| } | |||
| @@ -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() | |||
| ; | |||
| } | |||
| } | |||