api
This commit is contained in:
22
common/behaviors/GsCors.php
Executable file
22
common/behaviors/GsCors.php
Executable file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace common\behaviors;
|
||||
|
||||
|
||||
use yii\filters\Cors;
|
||||
|
||||
class GsCors extends Cors
|
||||
{
|
||||
|
||||
public function prepareHeaders($requestHeaders){
|
||||
$responseHeaders = parent::prepareHeaders($requestHeaders);
|
||||
|
||||
if (isset($this->cors['Access-Control-Allow-Headers'])) {
|
||||
$responseHeaders['Access-Control-Allow-Headers'] = implode(', ', $this->cors['Access-Control-Allow-Headers']);
|
||||
}
|
||||
|
||||
return $responseHeaders;
|
||||
}
|
||||
|
||||
}
|
@@ -15,6 +15,7 @@ use function Symfony\Component\String\s;
|
||||
* @property int|null $type
|
||||
* @property int|null $price
|
||||
* @property int|null $status
|
||||
* @property int $product_category_id
|
||||
*
|
||||
* @property Company $company
|
||||
*/
|
||||
@@ -51,7 +52,7 @@ class Product extends \yii\db\ActiveRecord
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
public static function tableName(): string
|
||||
{
|
||||
return 'product';
|
||||
}
|
||||
@@ -59,11 +60,11 @@ class Product extends \yii\db\ActiveRecord
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
[['title', 'article', 'company_id'], 'required'],
|
||||
[['company_id', 'type', 'price', 'status'], 'integer'],
|
||||
[['title', 'article', 'company_id', 'product_category_id'], 'required'],
|
||||
[['company_id', 'type', 'price', 'status', 'product_category_id'], 'integer'],
|
||||
[['title', 'article'], 'string', 'max' => 255],
|
||||
[['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::class, 'targetAttribute' => ['company_id' => 'id']],
|
||||
];
|
||||
@@ -72,7 +73,7 @@ class Product extends \yii\db\ActiveRecord
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
public function attributeLabels(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
@@ -82,6 +83,7 @@ class Product extends \yii\db\ActiveRecord
|
||||
'type' => 'Тип',
|
||||
'price' => 'Цена',
|
||||
'status' => 'Статус',
|
||||
'product_category_id' => 'Категория',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -90,8 +92,16 @@ class Product extends \yii\db\ActiveRecord
|
||||
*
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getCompany()
|
||||
public function getCompany(): \yii\db\ActiveQuery
|
||||
{
|
||||
return $this->hasOne(Company::class, ['id' => 'company_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getProductCategory(): \yii\db\ActiveQuery
|
||||
{
|
||||
return $this->hasOne(ProductCategory::class, ['id' => 'product_category_id']);
|
||||
}
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@
|
||||
namespace common\services;
|
||||
|
||||
use common\models\Company;
|
||||
use common\models\ProductCategory;
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
@@ -61,4 +62,14 @@ class CompanyService
|
||||
return ArrayHelper::map($this->getAddressesByUser($id), 'id', 'address');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoryByCompanyId(int $id): array
|
||||
{
|
||||
return ProductCategory::find()->where(['company_id' => $id])->all();
|
||||
}
|
||||
|
||||
|
||||
}
|
30
common/services/ProductService.php
Normal file
30
common/services/ProductService.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use common\models\Product;
|
||||
|
||||
class ProductService
|
||||
{
|
||||
|
||||
public Product $model;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new Product();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $company_id
|
||||
* @param int|null $category_id
|
||||
* @return array
|
||||
*/
|
||||
public function findBy(int $company_id, int $category_id = null): array
|
||||
{
|
||||
return $this->model->find()->where(['company_id' => $company_id])
|
||||
->andWhere(['status' => Product::STATUS_ACTIVE])
|
||||
->andFilterWhere(['product_category_id' => $category_id])
|
||||
->all();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user