Skip to content

Commit

Permalink
Merge pull request #543 from caesar-team/feature/CAES-1508
Browse files Browse the repository at this point in the history
[Item] CAES-1508: Impelmented raws field to item.
  • Loading branch information
aburov-dev authored Nov 18, 2020
2 parents 40e4f3c + a63170a commit aa7b86f
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Controller/Api/Item/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Entity\Item;
use App\Factory\Entity\ItemFactory;
use App\Factory\View\Item\BatchItemViewFactory;
use App\Factory\View\Item\ItemRawsViewFactory;
use App\Factory\View\Item\ItemViewFactory;
use App\Form\Type\Request\Item\CreateBatchItemsRequestType;
use App\Form\Type\Request\Item\CreateBatchKeypairsRequestType;
Expand All @@ -19,6 +20,7 @@
use App\Model\DTO\GroupedUserItems;
use App\Model\Query\ItemsAllQuery;
use App\Model\View\Item\BatchItemsView;
use App\Model\View\Item\ItemRawsView;
use App\Model\View\Item\ItemView;
use App\Repository\ItemRepository;
use App\Request\Item\CreateBatchItemsRequest;
Expand Down Expand Up @@ -85,6 +87,32 @@ public function items(Request $request, ItemRepository $repository, BatchItemVie
);
}

/**
* Get single raws item.
*
* @SWG\Tag(name="Item")
* @SWG\Response(
* response=200,
* description="Item data",
* @Model(type=ItemRawsView::class)
* )
*
* @SWG\Response(
* response=404,
* description="No such item"
* )
*
* @Route(
* path="/{id}/raws",
* name="api_show_item_raws",
* methods={"GET"}
* )
*/
public function raws(Item $item, ItemRawsViewFactory $factory): ItemRawsView
{
return $factory->createSingle($item);
}

/**
* Get batch items information.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class Item implements ChildItemAwareInterface
*/
protected $meta;

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

/**
* @var string
*
Expand Down Expand Up @@ -239,6 +246,16 @@ public function setMeta(ItemMeta $meta): void
$this->meta = $meta;
}

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

public function getRaws(): ?string
{
return $this->raws;
}

public function getLastUpdated(): DateTime
{
return $this->lastUpdated;
Expand Down
1 change: 1 addition & 0 deletions src/Factory/Entity/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function createFromRequest(CreateItemRequest $request): Item
$request->getMeta()->getAttachCount() ?: 0,
$request->getMeta()->getWebSite()
));
$item->setRaws($request->getRaws());

return $item;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Factory/View/Item/ItemRawsViewFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Factory\View\Item;

use App\Entity\Item;
use App\Model\View\Item\ItemRawsView;

class ItemRawsViewFactory
{
public function createSingle(Item $item): ItemRawsView
{
$view = new ItemRawsView();
$view->setId($item->getId()->toString());
$view->setRaws($item->getRaws());

return $view;
}
}
1 change: 1 addition & 0 deletions src/Form/Type/Request/Item/CreateItemRequestType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
])
->add('title', TextType::class)
->add('secret', TextType::class)
->add('raws', TextType::class)
->add('meta', ItemMetaType::class)
->add('favorite', CheckboxType::class)
->add('tags', CollectionType::class, [
Expand Down
1 change: 1 addition & 0 deletions src/Form/Type/Request/Item/EditItemRequestType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
])
->add('title', TextType::class)
->add('secret', TextType::class)
->add('raws', TextType::class)
->add('meta', ItemMetaType::class)
->add('tags', CollectionType::class, [
'entry_type' => TextType::class,
Expand Down
25 changes: 25 additions & 0 deletions src/Migrations/2020/11/Version20201118094040.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20201118094040 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 ADD raws TEXT DEFAULT NULL');
}

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 DROP raws');
}
}
40 changes: 40 additions & 0 deletions src/Model/View/Item/ItemRawsView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Model\View\Item;

use Swagger\Annotations as SWG;

final class ItemRawsView
{
/**
* @SWG\Property(type="string", example="4fcc6aef-3fd6-4c16-9e4b-5c37486c7d46")
*/
private string $id;

/**
* @SWG\Property(type="string")
*/
private ?string $raws;

public function getId(): string
{
return $this->id;
}

public function setId(string $id): void
{
$this->id = $id;
}

public function getRaws(): ?string
{
return $this->raws;
}

public function setRaws(?string $raws): void
{
$this->raws = $raws;
}
}
1 change: 1 addition & 0 deletions src/Modifier/ItemModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function modifyByRequest(EditItemRequest $request): Item
$request->getMeta()->getAttachCount() ?: 0,
$request->getMeta()->getWebSite()
));
$item->setRaws($request->getRaws());
$item->setTitle($request->getTitle());
$item->setTags(new ArrayCollection($this->transformer->transform($request->getTags())));
$this->repository->save($item);
Expand Down
13 changes: 13 additions & 0 deletions src/Request/Item/CreateItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ final class CreateItemRequest
*/
private ?string $secret;

private ?string $raws;

private bool $favorite;

private array $tags;
Expand All @@ -50,6 +52,7 @@ public function __construct(User $user)
$this->secret = null;
$this->favorite = false;
$this->meta = new ItemMetaRequest();
$this->raws = null;
$this->tags = [];
}

Expand Down Expand Up @@ -142,4 +145,14 @@ public function setMeta(ItemMetaRequest $meta): void
{
$this->meta = $meta;
}

public function getRaws(): ?string
{
return $this->raws;
}

public function setRaws(?string $raws): void
{
$this->raws = $raws;
}
}
13 changes: 13 additions & 0 deletions src/Request/Item/EditItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class EditItemRequest
*/
private ItemMetaRequest $meta;

private ?string $raws;

private array $tags;

private Item $item;
Expand All @@ -41,6 +43,7 @@ public function __construct(Item $item)
$this->meta = new ItemMetaRequest();
$this->meta->setAttachCount($item->getMeta()->getAttachCount());
$this->meta->setWebSite($item->getMeta()->getWebSite());
$this->raws = $item->getRaws();
}

public function getOwner(): ?User
Expand Down Expand Up @@ -97,4 +100,14 @@ public function setMeta(ItemMetaRequest $meta): void
{
$this->meta = $meta;
}

public function getRaws(): ?string
{
return $this->raws;
}

public function setRaws(?string $raws): void
{
$this->raws = $raws;
}
}
1 change: 1 addition & 0 deletions tests/_support/factories/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'parent_list' => 'entity|'.Directory::class,
'secret' => Faker::word(),
'title' => Faker::word(),
'raws' => Faker::word(),
'original_item_id' => null,
'favorite' => false,
'type' => NodeEnumType::TYPE_CRED,
Expand Down
15 changes: 15 additions & 0 deletions tests/_support/schemas/item/definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@
"previousListId"
]
},
"item_raw": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"raws": {
"type": ["null", "string"]
}
},
"required": [
"id",
"raws"
]
},
"list": {
"type": "object",
"properties": {
Expand Down
4 changes: 4 additions & 0 deletions tests/_support/schemas/item/item_raw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "schemas://item/definitions.json#/definitions/item_raw"
}
7 changes: 7 additions & 0 deletions tests/api/Item/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function getItem()
$I->seeResponseCodeIs(HttpCode::OK);

$I->seeResponseIsValidOnJsonSchemaString($I->getSchema('item/item.json'));

$I->sendGET(sprintf('/items/%s/raws', $item->getId()->toString()));
$I->seeResponseCodeIs(HttpCode::OK);

$I->seeResponseIsValidOnJsonSchemaString($I->getSchema('item/item_raw.json'));
}

/** @test */
Expand Down Expand Up @@ -210,6 +215,7 @@ public function createCredItem()
'meta' => [
'attachCount' => 2,
],
'raws' => uniqid(),
'tags' => ['tag'],
]);
$I->seeResponseCodeIs(HttpCode::OK);
Expand Down Expand Up @@ -247,6 +253,7 @@ public function editItem()
'attachCount' => 3,
'webSite' => 'http://examle.com/login',
],
'raws' => uniqid(),
]);
$I->seeResponseCodeIs(HttpCode::OK);

Expand Down

0 comments on commit aa7b86f

Please sign in to comment.