Last active
November 20, 2019 03:26
-
-
Save dersonsena/8f9f734c1be73c6016b7b79014ef0010 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CreateAction extends Action | |
{ | |
private function save(Product $model) | |
{ | |
$bodyParams = Yii::$app->getRequest()->getBodyParams(); | |
if (!isset($bodyParams['colors']) || empty($bodyParams['colors'])) { | |
Yii::$app->getResponse()->setStatusCode(422, 'Data Validation Failed.'); | |
return []; // bla bla bla | |
} | |
$transaction = Yii::$app->getDb()->beginTransaction(); | |
$isNewRecord = $model->isNewRecord; | |
$model->load($bodyParams, ''); | |
try { | |
if (!$model->save()) { | |
throw new DbException('There was an error creating product', $model->getFormattedErrors(), 3); | |
} | |
if (!$isNewRecord) { | |
ProductColor::deleteAll('product_id = :id', [':id' => $model->id]); | |
} | |
foreach ($bodyParams['colors'] as $color) { | |
$colorRecord = Color::findOne($color['uuid']); | |
if (!$colorRecord) { | |
throw new DbException('There was an error creating product', [ | |
['field' => 'colors', 'message' => "Cor '{$color['uuid']}' é inválido."] | |
], 4); | |
} | |
$colorRecord->link('products', $model); | |
} | |
$transaction->commit(); | |
Yii::$app->getResponse()->setStatusCode(200); | |
return $model; | |
} catch (DbException $e) { | |
$transaction->rollBack(); | |
Yii::$app->getResponse()->setStatusCode(422, 'Data Validation Failed.'); | |
return ['code' => $e->getCode(), 'message' => $e->getMessage(), 'errors' => $e->errorInfo]; | |
} catch (Exception $e) { | |
$transaction->rollBack(); | |
Yii::$app->getResponse()->setStatusCode(422, 'Data Validation Failed.'); | |
return ['code' => $e->getCode(), 'message' => $e->getMessage()]; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Domain; | |
use App\Domain\Product\Product; | |
use GoSale\YiiCore\ActiveRecord\ActiveRecordAbstract; | |
/** | |
* This is the model class for table "{{%colors}}". | |
* | |
* @property string $id (DC2Type:uuid_binary_ordered_time) | |
* @property string $uuid (DC2Type:uuid) | |
* @property array $client | |
* @property string $name | |
* @property string $abbreviation | |
* @property int $status | |
* @property int $deleted | |
* @property string $created_at | |
* @property string $updated_at | |
* @property string $deleted_at | |
* @property string $created_by | |
* @property string $updated_by | |
* @property string $deleted_by | |
* | |
* @property ProductColor[] $productsColors | |
* @property Product[] $products | |
*/ | |
class Color extends ActiveRecordAbstract | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function tableName() | |
{ | |
return '{{%colors}}'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['id', 'uuid', 'client', 'name', 'abbreviation'], 'required'], | |
[['client', 'created_at', 'updated_at', 'deleted_at'], 'safe'], | |
[['status', 'deleted'], 'integer'], | |
['id', 'string', 'max' => 16], | |
['uuid', 'string', 'max' => 36], | |
['name', 'string', 'max' => 60], | |
['abbreviation', 'string', 'max' => 10], | |
[['created_by', 'updated_by', 'deleted_by'], 'string', 'max' => 100], | |
['uuid', 'unique'], | |
['id', 'unique'], | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function attributeLabels() | |
{ | |
return $this->buildAttributeLabels([ | |
'client' => 'Cliente', | |
'name' => 'Nome', | |
'abbreviation' => 'Sigla', | |
]); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getProductsColors() | |
{ | |
return $this->hasMany(ProductColor::class, ['color_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getProducts() | |
{ | |
return $this->hasMany(Product::class, ['id' => 'product_id']) | |
->viaTable('{{%products_colors}}', ['color_id' => 'id']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Domain\Product; | |
use App\Domain\Color; | |
use App\Domain\Grid; | |
use App\Domain\Group; | |
use App\Domain\Product\Validators\EAN13BarcodeValidator; | |
use GoSale\YiiCore\ActiveRecord\ActiveRecordAbstract; | |
/** | |
* This is the model class for table "{{%products}}". | |
* | |
* @property string $id (DC2Type:uuid_binary_ordered_time) | |
* @property string $uuid (DC2Type:uuid) | |
* @property array $client | |
* @property string $group_id (DC2Type:uuid_binary_ordered_time) | |
* @property string $name | |
* @property string $reference | |
* @property string $reference_manufacturer | |
* @property string $price_cost | |
* @property string $price_sale | |
* @property string $barcode | |
* @property int $control_stock | |
* @property string $observation | |
* @property int $status | |
* @property int $deleted | |
* @property string $created_at | |
* @property string $updated_at | |
* @property string $deleted_at | |
* @property string $created_by | |
* @property string $updated_by | |
* @property string $deleted_by | |
* | |
* @property Group $group | |
* @property Grid $grid | |
* @property Color[] $colors | |
*/ | |
class Product extends ActiveRecordAbstract | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function tableName() | |
{ | |
return '{{%products}}'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function rules() | |
{ | |
return [ | |
[ | |
[ | |
'id', | |
'uuid', | |
'client', | |
'group_id', | |
'grid_id', | |
'name', | |
'reference', | |
'price_cost', | |
'price_sale', | |
'control_stock', | |
'barcode' | |
], | |
'required' | |
], | |
[['client', 'created_at', 'updated_at', 'deleted_at'], 'safe'], | |
[['price_cost', 'price_sale'], 'number'], | |
[['control_stock', 'status', 'deleted'], 'integer'], | |
['control_stock', 'boolean'], | |
['observation', 'string'], | |
[['id', 'group_id'], 'string', 'max' => 16], | |
['uuid', 'string', 'max' => 36], | |
[['name', 'reference', 'reference_manufacturer'], 'string', 'max' => 60], | |
['barcode', 'string', 'max' => 13], | |
['barcode', EAN13BarcodeValidator::class], | |
[['created_by', 'updated_by', 'deleted_by'], 'string', 'max' => 100], | |
['uuid', 'unique'], | |
['id', 'unique'], | |
[ | |
'group_id', | |
'exist', | |
'skipOnError' => true, | |
'targetClass' => Group::class, | |
'targetAttribute' => ['group_id' => 'id'] | |
], | |
[ | |
'grid_id', | |
'exist', | |
'skipOnError' => true, | |
'targetClass' => Grid::class, | |
'targetAttribute' => ['grid_id' => 'id'] | |
], | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function attributeLabels() | |
{ | |
return $this->buildAttributeLabels([ | |
'client' => 'Cliente', | |
'group_id' => 'Grupo', | |
'grid_id' => 'Grade', | |
'name' => 'Descrição', | |
'reference' => 'Referência', | |
'reference_manufacturer' => 'Referência Fabricante', | |
'price_cost' => 'Preço Custo', | |
'price_sale' => 'Preço Venda', | |
'barcode' => 'Cod. Barras', | |
'control_stock' => 'Controla Estoque', | |
'observation' => 'Observações', | |
]); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getGroup() | |
{ | |
return $this->hasOne(Group::class, ['id' => 'group_id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getGrid() | |
{ | |
return $this->hasOne(Grid::class, ['id' => 'grid_id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getColors() | |
{ | |
return $this->hasMany(Color::class, ['id' => 'color_id']) | |
->viaTable('{{%products_colors}}', ['product_id' => 'id']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Domain; | |
use App\Domain\Product\Product; | |
use yii\db\ActiveRecord; | |
/** | |
* This is the model class for table "{{%products_colors}}". | |
* | |
* @property string $product_id (DC2Type:uuid_binary_ordered_time) | |
* @property string $color_id (DC2Type:uuid_binary_ordered_time) | |
* | |
* @property Color $color | |
* @property Product $product | |
*/ | |
class ProductColor extends ActiveRecord | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function tableName() | |
{ | |
return '{{%products_colors}}'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['product_id', 'color_id'], 'required'], | |
[['product_id', 'color_id'], 'string', 'max' => 16], | |
[ | |
['color_id'], | |
'exist', | |
'skipOnError' => true, | |
'targetClass' => Color::class, | |
'targetAttribute' => ['color_id' => 'id'] | |
], | |
[ | |
['product_id'], | |
'exist', | |
'skipOnError' => true, | |
'targetClass' => Product::class, | |
'targetAttribute' => ['product_id' => 'id'] | |
], | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'product_id' => 'Product ID', | |
'color_id' => 'Color ID', | |
]; | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getColor() | |
{ | |
return $this->hasOne(Color::class, ['id' => 'color_id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getProduct() | |
{ | |
return $this->hasOne(Product::class, ['id' => 'product_id']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment