api
This commit is contained in:
24
frontend/modules/api/Api.php
Normal file
24
frontend/modules/api/Api.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api;
|
||||
|
||||
/**
|
||||
* api module definition class
|
||||
*/
|
||||
class Api extends \yii\base\Module
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $controllerNamespace = 'frontend\modules\api\controllers';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
// custom initialization code goes here
|
||||
}
|
||||
}
|
80
frontend/modules/api/controllers/ApiController.php
Normal file
80
frontend/modules/api/controllers/ApiController.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\behaviors\GsCors;
|
||||
use yii\filters\auth\CompositeAuth;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\filters\ContentNegotiator;
|
||||
use yii\rest\Controller;
|
||||
use yii\web\Response;
|
||||
|
||||
|
||||
/**
|
||||
* @OA\Info(
|
||||
* version="1.0.0",
|
||||
* title="Документация Сервис для создания чеков",
|
||||
* description="Документация для работы с API",
|
||||
*
|
||||
* ),
|
||||
* @OA\PathItem(
|
||||
* path="/api"
|
||||
* ),
|
||||
* @OA\Server(
|
||||
* url="https://check.itguild.info/api",
|
||||
* description="Основной сервер",
|
||||
* ),
|
||||
*
|
||||
* @OA\Server(
|
||||
* url="http://check-back.loc/api",
|
||||
* description="Локальный сервер",
|
||||
* ),
|
||||
*
|
||||
* @OA\SecurityScheme(
|
||||
* securityScheme="bearerAuth",
|
||||
* in="header",
|
||||
* name="Authorization",
|
||||
* type="http",
|
||||
* scheme="bearer",
|
||||
* bearerFormat="JWT",
|
||||
* ),
|
||||
*/
|
||||
class ApiController extends Controller
|
||||
{
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
'corsFilter' => [
|
||||
'class' => GsCors::class,
|
||||
'cors' => [
|
||||
'Origin' => ['*'],
|
||||
//'Access-Control-Allow-Credentials' => true,
|
||||
'Access-Control-Allow-Headers' => [
|
||||
'Access-Control-Allow-Origin',
|
||||
'Access-Control-Allow-Methods',
|
||||
'Content-Type',
|
||||
'Access-Control-Allow-Headers',
|
||||
'Authorization',
|
||||
'X-Requested-With'
|
||||
],
|
||||
'Access-Control-Allow-Methods' => ['POST', 'GET', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
||||
'Access-Control-Request-Method' => ['POST', 'GET', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
||||
]
|
||||
],
|
||||
'authenticator' => [
|
||||
'class' => CompositeAuth::class,
|
||||
'authMethods' => [
|
||||
HttpBearerAuth::class,
|
||||
],
|
||||
],
|
||||
[
|
||||
'class' => ContentNegotiator::className(),
|
||||
'formats' => [
|
||||
'application/json' => Response::FORMAT_JSON,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
82
frontend/modules/api/controllers/CategoryController.php
Normal file
82
frontend/modules/api/controllers/CategoryController.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\classes\Debug;
|
||||
use common\services\CompanyService;
|
||||
|
||||
class CategoryController extends ApiController
|
||||
{
|
||||
public CompanyService $companyService;
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
$this->companyService = new CompanyService();
|
||||
}
|
||||
|
||||
public function behaviors(): array
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
unset($behaviors['authenticator']);
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @OA\Get(path="/category",
|
||||
* summary="Список категорий компании",
|
||||
* description="Получить список всех категорий компании",
|
||||
* tags={"Category"},
|
||||
* @OA\Parameter(
|
||||
* name="company_id",
|
||||
* in="query",
|
||||
* required=true,
|
||||
* @OA\Schema(
|
||||
* type="integer",
|
||||
* default=null
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Возвращает массив",
|
||||
* @OA\MediaType(
|
||||
* mediaType="application/json",
|
||||
* @OA\Schema(
|
||||
* type="array",
|
||||
* @OA\Items(
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="integer",
|
||||
* example="1",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="title",
|
||||
* type="string",
|
||||
* example="Кальянный клуб LA",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="parent_id",
|
||||
* type="integer",
|
||||
* example="33",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="company_id",
|
||||
* type="integer",
|
||||
* example="23",
|
||||
* ),
|
||||
* )
|
||||
* ),
|
||||
* ),
|
||||
*
|
||||
* ),
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function actionIndex(int $company_id): array
|
||||
{
|
||||
return $this->companyService->getCategoryByCompanyId($company_id);
|
||||
}
|
||||
|
||||
}
|
20
frontend/modules/api/controllers/DefaultController.php
Normal file
20
frontend/modules/api/controllers/DefaultController.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use yii\web\Controller;
|
||||
|
||||
/**
|
||||
* Default controller for the `api` module
|
||||
*/
|
||||
class DefaultController extends Controller
|
||||
{
|
||||
/**
|
||||
* Renders the index view for the module
|
||||
* @return string
|
||||
*/
|
||||
public function actionIndex()
|
||||
{
|
||||
return $this->render('index');
|
||||
}
|
||||
}
|
114
frontend/modules/api/controllers/ProductController.php
Normal file
114
frontend/modules/api/controllers/ProductController.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\services\CompanyService;
|
||||
use common\services\ProductService;
|
||||
|
||||
class ProductController extends ApiController
|
||||
{
|
||||
|
||||
public CompanyService $companyService;
|
||||
public ProductService $productService;
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
$this->companyService = new CompanyService();
|
||||
$this->productService = new ProductService();
|
||||
}
|
||||
|
||||
public function behaviors(): array
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
unset($behaviors['authenticator']);
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @OA\Get(path="/product",
|
||||
* summary="Список товаров компании",
|
||||
* description="Получить список всех товаров компании",
|
||||
* tags={"Product"},
|
||||
* @OA\Parameter(
|
||||
* name="company_id",
|
||||
* in="query",
|
||||
* required=true,
|
||||
* @OA\Schema(
|
||||
* type="integer",
|
||||
* default=null
|
||||
* )
|
||||
* ),
|
||||
* @OA\Parameter(
|
||||
* name="category_id",
|
||||
* in="query",
|
||||
* required=false,
|
||||
* @OA\Schema(
|
||||
* type="integer",
|
||||
* default=null
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Возвращает массив",
|
||||
* @OA\MediaType(
|
||||
* mediaType="application/json",
|
||||
* @OA\Schema(
|
||||
* type="array",
|
||||
* @OA\Items(
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="integer",
|
||||
* example="1",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="title",
|
||||
* type="string",
|
||||
* example="Кальянный клуб LA",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="article",
|
||||
* type="string",
|
||||
* example="005",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="type",
|
||||
* type="integer",
|
||||
* example="1",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="price",
|
||||
* type="integer",
|
||||
* example="250",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="status",
|
||||
* type="integer",
|
||||
* example="1",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="product_category_id",
|
||||
* type="integer",
|
||||
* example="5",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="company_id",
|
||||
* type="integer",
|
||||
* example="23",
|
||||
* ),
|
||||
* )
|
||||
* ),
|
||||
* ),
|
||||
*
|
||||
* ),
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function actionIndex(int $company_id, int $category_id = null): array
|
||||
{
|
||||
return $this->productService->findBy($company_id, $category_id);
|
||||
}
|
||||
|
||||
}
|
12
frontend/modules/api/views/default/index.php
Normal file
12
frontend/modules/api/views/default/index.php
Normal file
@ -0,0 +1,12 @@
|
||||
<div class="api-default-index">
|
||||
<h1><?= $this->context->action->uniqueId ?></h1>
|
||||
<p>
|
||||
This is the view content for action "<?= $this->context->action->id ?>".
|
||||
The action belongs to the controller "<?= get_class($this->context) ?>"
|
||||
in the "<?= $this->context->module->id ?>" module.
|
||||
</p>
|
||||
<p>
|
||||
You may customize this page by editing the following file:<br>
|
||||
<code><?= __FILE__ ?></code>
|
||||
</p>
|
||||
</div>
|
@ -14,10 +14,10 @@ class ProductSearch extends Product
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
[['id', 'company_id', 'type', 'price', 'status'], 'integer'],
|
||||
[['id', 'company_id', 'type', 'price', 'status', 'product_category_id'], 'integer'],
|
||||
[['title', 'article'], 'safe'],
|
||||
];
|
||||
}
|
||||
|
@ -17,7 +17,9 @@ use yii\bootstrap4\ActiveForm;
|
||||
|
||||
<?= $form->field($model, 'article')->textInput(['maxlength' => true]) ?>
|
||||
|
||||
<?= $form->field($model, 'company_id')->dropDownList($companies) ?>
|
||||
<?= $form->field($model, 'company_id')->dropDownList($companies, ['id' => 'company_select']) ?>
|
||||
|
||||
<div class="form-group" id="category_select_container"></div>
|
||||
|
||||
<?= $form->field($model, 'type')->dropDownList(\common\models\Product::getType()) ?>
|
||||
|
||||
|
@ -40,6 +40,10 @@ $this->params['breadcrumbs'][] = $this->title;
|
||||
'attribute' => 'company_id',
|
||||
'value' => $companyService->getCompany($model->company_id)->name ?? null
|
||||
],
|
||||
[
|
||||
'attribute' => 'product_category_id',
|
||||
'value' => $model->getProductCategory()->one()->title ?? null
|
||||
],
|
||||
[
|
||||
'attribute' => 'type',
|
||||
'value' => \common\models\Product::getType()[$model->type]
|
||||
|
@ -4,6 +4,7 @@ namespace frontend\modules\product_category\controllers;
|
||||
|
||||
use common\classes\Debug;
|
||||
use common\models\User;
|
||||
use common\services\CompanyService;
|
||||
use Yii;
|
||||
use frontend\modules\product_category\models\ProductCategory;
|
||||
use frontend\modules\product_category\models\ProductCategorySearch;
|
||||
@ -17,6 +18,14 @@ use yii\filters\VerbFilter;
|
||||
*/
|
||||
class ProductCategoryController extends Controller
|
||||
{
|
||||
public CompanyService $companyService;
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
$this->companyService = new CompanyService();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -114,6 +123,16 @@ class ProductCategoryController extends Controller
|
||||
return $this->redirect(['index']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $company_id
|
||||
* @return string
|
||||
*/
|
||||
public function actionGetCategorySelectByCompanyId($company_id): string
|
||||
{
|
||||
$category = $this->companyService->getCategoryByCompanyId($company_id);
|
||||
return $this->renderAjax('_category_select', ['category' => $category]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the ProductCategory model based on its primary key value.
|
||||
* If the model is not found, a 404 HTTP exception will be thrown.
|
||||
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var array $category
|
||||
*/
|
||||
echo \yii\helpers\Html::label("Категории", "product_category");
|
||||
echo \yii\helpers\Html::dropDownList(
|
||||
'Product[product_category_id]',
|
||||
null,
|
||||
\yii\helpers\ArrayHelper::map($category, 'id', 'title'),
|
||||
[
|
||||
'class' => 'form-control',
|
||||
'id' => 'product_category',
|
||||
]
|
||||
);
|
@ -18,8 +18,9 @@ $this->params['breadcrumbs'][] = $this->title;
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<p>
|
||||
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
|
||||
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
|
||||
<?= Html::a('Список', ['index'], ['class' => 'btn btn-primary']) ?>
|
||||
<?= Html::a('Редактировать', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
|
||||
<?= Html::a('Удалить', ['delete', 'id' => $model->id], [
|
||||
'class' => 'btn btn-danger',
|
||||
'data' => [
|
||||
'confirm' => 'Are you sure you want to delete this item?',
|
||||
|
Reference in New Issue
Block a user