Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variant #2

Merged
merged 26 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f9326e5
variant table has been set
NimaGhaedsharafi Nov 10, 2017
bd24ff5
WIP
NimaGhaedsharafi Nov 11, 2017
195a078
some phpDoc has been fixed
NimaGhaedsharafi Nov 21, 2017
ff0e32c
BaseEntity supports JsonSerializable
NimaGhaedsharafi Nov 21, 2017
4154c94
minor fix
NimaGhaedsharafi Nov 21, 2017
0008521
set variant a default value
NimaGhaedsharafi Nov 21, 2017
226beee
variant should be eager loaded
NimaGhaedsharafi Nov 21, 2017
9b4dd54
missing toArray has been added
NimaGhaedsharafi Nov 21, 2017
3bbdae2
debugging state has been deleted
NimaGhaedsharafi Nov 21, 2017
7c6af8e
missing phpDoc has been added
NimaGhaedsharafi Nov 21, 2017
1cf539b
extra commas have been deleted
NimaGhaedsharafi Nov 21, 2017
a05c892
extra qoutes have been deleted
NimaGhaedsharafi Nov 21, 2017
b56db5a
get the last product instead of first one
NimaGhaedsharafi Nov 21, 2017
c263f4c
product_id field has been added to Product entity
NimaGhaedsharafi Nov 21, 2017
a8cdba3
delete a variant has been implemented
NimaGhaedsharafi Nov 21, 2017
3b79d1c
extra group names have been deleted
NimaGhaedsharafi Nov 21, 2017
c5d66cc
update a variant has been implemented
NimaGhaedsharafi Nov 21, 2017
191b2b9
extra group name has been deleted
NimaGhaedsharafi Nov 21, 2017
871a767
taking care of not existant variant edition
NimaGhaedsharafi Nov 21, 2017
4ee543c
updating a variant from another product is forbidden
NimaGhaedsharafi Nov 21, 2017
ab5531f
minor change
NimaGhaedsharafi Nov 21, 2017
4e81ea6
validation check has been added
NimaGhaedsharafi Nov 21, 2017
3f5ac70
make sure every condition has test
NimaGhaedsharafi Nov 21, 2017
f9e416d
BaseEnum has been defined for further encapsulation
NimaGhaedsharafi Nov 21, 2017
264ac99
ColorEnum has been defined to use instead of some random integer
NimaGhaedsharafi Nov 21, 2017
b36baa4
use Enum instead of Integers
NimaGhaedsharafi Nov 21, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions app/Migrations/Version20171110195112.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Application\Migrations;

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

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20171110195112 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('CREATE TABLE variants (id INT AUTO_INCREMENT NOT NULL, product_id INT DEFAULT NULL, color INT NOT NULL, price INT NOT NULL, INDEX IDX_B39853E14584665A (product_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE variants ADD CONSTRAINT FK_B39853E14584665A FOREIGN KEY (product_id) REFERENCES products (id)');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('DROP TABLE variants');
}
}
4 changes: 2 additions & 2 deletions src/AppBundle/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class BaseController extends Controller
*/
protected function response($data = null, $status = 200, $headers = [])
{
if (is_array($data) && isset($data[0]) && $data[0] instanceof Renderable) {
if (is_array($data) && count($data) > 1) {
$data = json_encode(array_map(function ($value) {
return $value instanceof Arrayable ? $value->toArray() : $value;
return $value instanceof Collection ? $value->toArray() : $value;
}, $data));
} elseif ($data instanceof Renderable) {
$data = $data->render();
Expand Down
86 changes: 86 additions & 0 deletions src/AppBundle/Controller/VariantController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Product;
use AppBundle\Entity\Variant;
use AppBundle\Exception\NotFoundEntity;
use AppBundle\Exception\ValidationFailed;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class VariantController extends BaseController
{
/**
* @param $id
* @param Request $request
* @return Response
*/
public function addAction($id, Request $request)
{
/** @var Product $product */
$product = $this->getDoctrine()->getRepository(Product::class)->find($id);

$variant = new Variant();
$variant->setColor($request->get('color'));
$variant->setPrice($request->get('price'));
$variant->setProduct($product);

$manager = $this->getDoctrine()->getManager();
$manager->persist($variant);
$manager->flush();

$product->getVariants()->add($variant);

return $this->response($product, Response::HTTP_CREATED);
}

/**
* @param $pid
* @param $vid
* @return Response
*/
public function deleteAction($pid, $vid)
{
/** @var Variant $variant */
$variant = $this->getDoctrine()->getRepository(Variant::class)->find($vid);

if ($variant === null || $variant->getProductId() != $pid) {
throw new NotFoundEntity();
}

$manager = $this->getDoctrine()->getManager();
$manager->remove($variant);
$manager->flush();

return $this->ack();
}

/**
* @param $pid
* @param $vid
* @param Request $request
* @return Response
*/
public function updateAction($pid, $vid, Request $request)
{
// TODO: add a validation
// it needs some validation but I don't know how to do it in right way!
// but just for now let's check their existence
if ($request->get('price') === null || $request->get('color') === null) {
throw new ValidationFailed();
}

/** @var Variant $variant */
$variant = $this->getDoctrine()->getRepository(Variant::class)->find($vid);

if ($variant === null || $variant->getProductId() != $pid) {
throw new NotFoundEntity();
}
$variant->setColor($request->get('color'));
$variant->setPrice($request->get('price'));
$this->getDoctrine()->getManager()->flush();

return $this->response($variant);
}
}
10 changes: 9 additions & 1 deletion src/AppBundle/Entity/BaseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use AppBundle\Contracts\Collection;

abstract class BaseEntity extends Collection
abstract class BaseEntity extends Collection implements \JsonSerializable
{

/**
Expand All @@ -30,4 +30,12 @@ public function toArray(): array
{
// TODO: Implement toArray() method.
}

/**
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}
}
34 changes: 31 additions & 3 deletions src/AppBundle/Entity/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AppBundle\Entity;

use Carbon\Carbon;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
Expand Down Expand Up @@ -50,13 +51,22 @@ class Product extends BaseEntity
*/
private $updatedAt;

/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Variant", mappedBy="product", fetch="EAGER")
*/
private $variants;


/**
* Product constructor.
*/
public function __construct()
{
$this->setCreatedAt(Carbon::now());
$this->setUpdatedAt(Carbon::now());

$this->variants = new ArrayCollection();
}

/**
Expand Down Expand Up @@ -165,14 +175,32 @@ public function getUpdatedAt()
return $this->updatedAt;
}

/**
* @return ArrayCollection
*/
public function getVariants()
{
return $this->variants;
}

/**
* @param ArrayCollection $variants
*/
public function setVariants($variants)
{
$this->variants = $variants;
}

/**
* @return array
*/
public function toArray(): array
{
return [
'title' => $this->getTitle(),
'description' => $this->getDescription()
'description' => $this->getDescription(),
'variants' => $this->getVariants()->toArray()
];
}


}

157 changes: 157 additions & 0 deletions src/AppBundle/Entity/Variant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Variant
*
* @ORM\Table(name="variants")
* @ORM\Entity(repositoryClass="AppBundle\Repository\VariantRepository")
*/
class Variant extends BaseEntity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="Product", inversedBy="variants")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;

/**
* @var int
*
* @ORM\Column(name="color", type="integer")
*/
private $color;


/**
* @var int
*
* @ORM\Column(name="price", type="integer")
*/
private $price;


/**
* @var int
*
* @ORM\Column(name="product_id", type="integer")
*/
private $product_id;

/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Set color
*
* @param integer $color
*
* @return Variant
*/
public function setColor($color)
{
$this->color = $color;

return $this;
}

/**
* Get color
*
* @return int
*/
public function getColor()
{
return $this->color;
}

/**
* @return Product
*/
public function getProduct()
{
return $this->product;
}

/**
* @param Product $product
*/
public function setProduct($product)
{
$this->product = $product;
}

/**
* Set price
*
* @param integer $price
*
* @return Variant
*/
public function setPrice($price)
{
$this->price = $price;

return $this;
}

/**
* Get price
*
* @return int
*/
public function getPrice()
{
return $this->price;
}

/**
* @return int
*/
public function getProductId(): int
{
return $this->product_id;
}

/**
* @param int $product_id
*/
public function setProductId(int $product_id)
{
$this->product_id = $product_id;
}

/**
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->getId(),
'price' => $this->getPrice(),
'color' => $this->getColor()
];
}

}
Loading