Skip to content

Commit

Permalink
[Item] CAES-1512: Refactoring meta, implemented endpoint to check exi…
Browse files Browse the repository at this point in the history
…sts items. (#546)

Co-authored-by: Aleksandr Beshkenadze <[email protected]>
  • Loading branch information
aburov-dev and beshkenadze authored Nov 19, 2020
1 parent 6928c51 commit 60e581a
Show file tree
Hide file tree
Showing 30 changed files with 354 additions and 148 deletions.
36 changes: 36 additions & 0 deletions src/Controller/Api/Item/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Form\Type\Request\Item\CreateBatchKeypairsRequestType;
use App\Form\Type\Request\Item\CreateItemRequestType;
use App\Form\Type\Request\Item\ItemsCollectionRequestType;
use App\Form\Type\Request\Item\ItemsIdCollectionRequestType;
use App\Limiter\Inspector\ItemCountInspector;
use App\Limiter\LimiterInterface;
use App\Limiter\Model\LimitCheck;
Expand All @@ -27,6 +28,7 @@
use App\Request\Item\CreateBatchKeypairsRequest;
use App\Request\Item\CreateItemRequest;
use App\Request\Item\ItemsCollectionRequest;
use App\Request\Item\ItemsIdCollectionRequest;
use App\Security\Voter\ItemVoter;
use App\Security\Voter\TeamItemVoter;
use Fourxxi\RestRequestError\Exception\FormInvalidRequestException;
Expand Down Expand Up @@ -87,6 +89,40 @@ public function items(Request $request, ItemRepository $repository, BatchItemVie
);
}

/**
* Get all items information.
*
* @SWG\Tag(name="Item")
* @SWG\Parameter(
* name="body",
* in="body",
* @Model(type=ItemsIdCollectionRequestType::class)
* )
* @SWG\Response(
* response=200,
* description="Success item created",
* @SWG\Schema(type="array", @SWG\Items(type="string"))
* )
*
* @Route(
* path="/unexists",
* name="api_items_unexists",
* methods={"POST"}
* )
*/
public function unexists(Request $request, ItemRepository $repository): array
{
$collectionRequest = new ItemsIdCollectionRequest();

$form = $this->createForm(ItemsIdCollectionRequestType::class, $collectionRequest);
$form->submit($request->request->all());
if (!$form->isValid()) {
throw new FormInvalidRequestException($form);
}

return $repository->getDiffItems($collectionRequest->getItems());
}

/**
* Get single raws item.
*
Expand Down
42 changes: 29 additions & 13 deletions src/Entity/Embedded/ItemMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,52 @@ class ItemMeta
/**
* @ORM\Column(type="integer", options={"default"=0})
*/
private int $attachCount;
private int $attachmentsCount;

/**
* @ORM\Column(nullable=true)
*/
private ?string $webSite;
private ?string $website;

public function __construct(int $attachCount = 0, ?string $webSite = null)
/**
* @ORM\Column(nullable=true)
*/
private ?string $title;

public function __construct(int $attachmentsCount = 0, ?string $website = null, ?string $title = null)
{
$this->attachmentsCount = $attachmentsCount;
$this->website = $website;
$this->title = $title;
}

public function getAttachmentsCount(): int
{
return $this->attachmentsCount;
}

public function setAttachmentsCount(int $attachmentsCount): void
{
$this->attachCount = $attachCount;
$this->webSite = $webSite;
$this->attachmentsCount = $attachmentsCount;
}

public function getAttachCount(): int
public function getWebsite(): ?string
{
return $this->attachCount;
return $this->website;
}

public function setAttachCount(int $attachCount): void
public function setWebsite(?string $website): void
{
$this->attachCount = $attachCount;
$this->website = $website;
}

public function getWebSite(): ?string
public function getTitle(): ?string
{
return $this->webSite;
return $this->title;
}

public function setWebSite(?string $webSite): void
public function setTitle(?string $title): void
{
$this->webSite = $webSite;
$this->title = $title;
}
}
17 changes: 0 additions & 17 deletions src/Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ class Item implements ChildItemAwareInterface
*/
protected $previousList;

/**
* @var string|null
*
* @ORM\Column(type="text", nullable=true)
*/
protected $title;

/**
* @var string|null
*
Expand Down Expand Up @@ -581,16 +574,6 @@ public function moveTo(Directory $directory): void
$this->setTeam($directory->getTeam());
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(?string $title): void
{
$this->title = $title;
}

public function getTeamKeypairGroupKey(): string
{
$groups = [];
Expand Down
11 changes: 7 additions & 4 deletions src/Factory/Entity/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ public function createTeamKeypairFromRequest(CreateKeypairRequest $request): Ite
{
$item = new Item($request->getOwner() ?: $request->getUser());
$item->setParentList($request->getTeam()->getDefaultDirectory());
$item->setTitle(NodeEnumType::TYPE_KEYPAIR);
$item->setType(NodeEnumType::TYPE_KEYPAIR);
$item->setSecret($request->getSecret());
$item->setRelatedItem($request->getRelatedItem());
$item->setTeam($request->getTeam());

$meta = new ItemMeta();
$meta->setTitle(NodeEnumType::TYPE_KEYPAIR);
$item->setMeta($meta);

return $item;
}

Expand All @@ -45,13 +48,13 @@ public function createFromRequest(CreateItemRequest $request): Item
$item->setParentList($parentList);
$item->setType($request->getType());
$item->setSecret($request->getSecret());
$item->setTitle($request->getTitle());
$item->setFavorite($request->isFavorite());
$item->setTags(new ArrayCollection($this->transformer->transform($request->getTags())));
$item->setTeam($request->getTeam());
$item->setMeta(new ItemMeta(
$request->getMeta()->getAttachCount() ?: 0,
$request->getMeta()->getWebSite()
$request->getMeta()->getAttachmentsCount() ?: 0,
$request->getMeta()->getWebsite(),
$request->getMeta()->getTitle()
));
$item->setRaws($request->getRaws());

Expand Down
5 changes: 4 additions & 1 deletion src/Factory/Entity/MemberFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Factory\Entity;

use App\DBAL\Types\Enum\NodeEnumType;
use App\Entity\Embedded\ItemMeta;
use App\Model\DTO\Member;
use App\Request\Team\CreateMemberRequest;

Expand Down Expand Up @@ -33,10 +34,12 @@ public function createFromRequest(CreateMemberRequest $request): Member
$keypair = $this->itemFactory->create();
$keypair->setOwner($user);
$keypair->setTeam($team);
$keypair->setTitle(NodeEnumType::TYPE_KEYPAIR);
$keypair->setType(NodeEnumType::TYPE_KEYPAIR);
$keypair->setParentList($team->getDefaultDirectory());
$keypair->setSecret($request->getSecret());
$meta = new ItemMeta();
$meta->setTitle(NodeEnumType::TYPE_KEYPAIR);
$keypair->setMeta($meta);

return new Member($userTeam, $keypair);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Factory/Entity/ShareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Factory\Entity;

use App\DBAL\Types\Enum\NodeEnumType;
use App\Entity\Embedded\ItemMeta;
use App\Model\DTO\Share;
use App\Request\Item\ShareBatchItemRequest;

Expand Down Expand Up @@ -33,11 +34,13 @@ public function createFromRequest(ShareBatchItemRequest $request): array

$item = $this->itemFactory->create();
$item->setOwner($user);
$item->setTitle(NodeEnumType::TYPE_KEYPAIR);
$item->setType(NodeEnumType::TYPE_KEYPAIR);
$item->setParentList($user->getInbox());
$item->setSecret($userRequest->getSecret());
$item->setRelatedItem($relatedItem);
$meta = new ItemMeta();
$meta->setTitle(NodeEnumType::TYPE_KEYPAIR);
$item->setMeta($meta);

$result[$user->getId()->toString()] = new Share($user, $item);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Factory/Entity/VaultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Factory\Entity;

use App\DBAL\Types\Enum\NodeEnumType;
use App\Entity\Embedded\ItemMeta;
use App\Entity\UserTeam;
use App\Model\DTO\Vault;
use App\Request\Team\CreateVaultRequest;
Expand Down Expand Up @@ -43,10 +44,12 @@ public function createFromRequest(CreateVaultRequest $request): Vault
$item = $this->itemFactory->create();
$item->setOwner($user);
$item->setTeam($team);
$item->setTitle(NodeEnumType::TYPE_KEYPAIR);
$item->setType(NodeEnumType::TYPE_KEYPAIR);
$item->setParentList($team->getDefaultDirectory());
$item->setSecret($keypairRequest->getSecret());
$meta = new ItemMeta();
$meta->setTitle(NodeEnumType::TYPE_KEYPAIR);
$item->setMeta($meta);

return new Vault($team, $item);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/View/Item/ItemMetaViewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class ItemMetaViewFactory
public function createSingle(ItemMeta $meta): ItemMetaView
{
$view = new ItemMetaView();
$view->setAttachCount($meta->getAttachCount());
$view->setWebSite($meta->getWebSite());
$view->setAttachmentsCount($meta->getAttachmentsCount());
$view->setWebsite($meta->getWebsite());
$view->setTitle($meta->getTitle());

return $view;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Factory/View/Item/ItemViewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public function createSingle(Item $item): ItemView
$view->setListId($item->getParentList()->getId()->toString());
$view->setPreviousListId($item->getPreviousListId());
$view->setSecret($item->getSecret());
$view->setTitle($item->getTitle());

$view->setInvited($this->inviteItemViewFactory->createCollection($item->getKeyPairItemsWithoutRoot()));
$view->setOwnerId($item->getOwner()->getId()->toString());
if (null === $item->getTeam()) {
Expand Down
1 change: 0 additions & 1 deletion src/Form/Type/Request/Item/CreateItemRequestType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
NodeEnumType::TYPE_SYSTEM,
],
])
->add('title', TextType::class)
->add('secret', TextType::class)
->add('raws', TextType::class)
->add('meta', ItemMetaType::class)
Expand Down
1 change: 0 additions & 1 deletion src/Form/Type/Request/Item/EditItemRequestType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'choice_value' => 'id',
'property_path' => 'owner',
])
->add('title', TextType::class)
->add('secret', TextType::class)
->add('raws', TextType::class)
->add('meta', ItemMetaType::class)
Expand Down
5 changes: 3 additions & 2 deletions src/Form/Type/Request/Item/ItemMetaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class ItemMetaType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('attachCount', IntegerType::class, [
->add('attachmentsCount', IntegerType::class, [
'required' => false,
])
->add('webSite', TextType::class, [
->add('website', TextType::class, [
'required' => false,
])
->add('title', TextType::class)
;
}

Expand Down
33 changes: 33 additions & 0 deletions src/Form/Type/Request/Item/ItemsIdCollectionRequestType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Form\Type\Request\Item;

use App\Request\Item\ItemsIdCollectionRequest;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ItemsIdCollectionRequestType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('items', CollectionType::class, [
'entry_type' => TextType::class,
'allow_add' => true,
])
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ItemsIdCollectionRequest::class,
'csrf_protection' => false,
]);
}
}
35 changes: 35 additions & 0 deletions src/Migrations/2020/11/Version20201119102626.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20201119102626 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('ALTER TABLE item RENAME COLUMN meta_attach_count TO meta_attachments_count');
$this->addSql('ALTER TABLE item RENAME COLUMN meta_web_site TO meta_website');

$this->addSql('ALTER TABLE item ADD meta_title VARCHAR(255) DEFAULT NULL');
$this->addSql('UPDATE item SET meta_title = title');
$this->addSql('ALTER TABLE item DROP title');
}

public function down(Schema $schema): void
{
$this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('ALTER TABLE item RENAME COLUMN meta_attachments_count TO meta_attach_count');
$this->addSql('ALTER TABLE item RENAME COLUMN meta_website TO meta_web_site');

$this->addSql('ALTER TABLE item ADD title TEXT DEFAULT NULL');
$this->addSql('UPDATE item SET title = meta_title');
$this->addSql('ALTER TABLE item DROP meta_title');
}
}
Loading

0 comments on commit 60e581a

Please sign in to comment.