feat: add image model and api controller

This commit is contained in:
b1ek 2024-08-19 22:44:16 +10:00
parent a5eb1d859b
commit 949006b68d
Signed by: blek
GPG Key ID: 14546221E3595D0C
6 changed files with 175 additions and 33 deletions

View File

@ -14,7 +14,10 @@ $config = [
'components' => [ 'components' => [
'request' => [ 'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'lMAfMLborfQnIGmHY25_bz_WLAwK0Cx_', 'cookieValidationKey' => 'never mind this, it is not used at all',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
], ],
'cache' => [ 'cache' => [
'class' => 'yii\caching\FileCache', 'class' => 'yii\caching\FileCache',
@ -42,14 +45,13 @@ $config = [
], ],
], ],
'db' => $db, 'db' => $db,
/*
'urlManager' => [ 'urlManager' => [
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'showScriptName' => false, 'showScriptName' => false,
'rules' => [ 'rules' => [
[ 'class' => 'yii\rest\UrlRule', 'controller' => 'Api' ],
], ],
], ],
*/
], ],
'params' => $params, 'params' => $params,
]; ];

View File

@ -0,0 +1,21 @@
<?php
namespace app\controllers;
use app\models\Parameter;
use yii\data\ActiveDataProvider;
use yii\rest\IndexAction;
class ApiController extends \yii\rest\Controller
{
public function actionIndex()
{
return Parameter::find()->with(['icon', 'iconGray'])->all();
}
public function verbs()
{
return [
'index' => [ 'GET' ],
];
}
}

View File

@ -0,0 +1,49 @@
<?php
use yii\db\cubrid\Schema;
use yii\db\Migration;
/**
* Class m240819_102301_image
*/
class m240819_102301_image extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('image', [
'id' => Schema::TYPE_PK,
'sha256' => Schema::TYPE_STRING . ' NOT NULL',
'original_name' => Schema::TYPE_STRING . ' NOT NULL'
]);
$this->addColumn('parameters', 'icon', 'INT(11) DEFAULT NULL');
$this->addColumn('parameters', 'icon_gray', 'INT(11) DEFAULT NULL');
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('image');
$this->dropColumn('parameters', 'icon');
$this->dropColumn('parameters', 'icon_gray');
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m240819_102301_image cannot be reverted.\n";
return false;
}
*/
}

View File

@ -5,20 +5,20 @@ namespace app\models;
use Yii; use Yii;
/** /**
* This is the model class for table "parameters". * This is the model class for table "image".
* *
* @property int $id * @property int $id
* @property string $title * @property string $sha256
* @property int $type * @property string $original_name
*/ */
class Parameters extends \yii\db\ActiveRecord class Image extends \yii\db\ActiveRecord
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function tableName() public static function tableName()
{ {
return 'parameters'; return 'image';
} }
/** /**
@ -27,9 +27,8 @@ class Parameters extends \yii\db\ActiveRecord
public function rules() public function rules()
{ {
return [ return [
[['title', 'type'], 'required'], [['sha256', 'original_name'], 'required'],
[['type'], 'integer'], [['sha256', 'original_name'], 'string', 'max' => 255],
[['title'], 'string', 'max' => 255],
]; ];
} }
@ -40,8 +39,8 @@ class Parameters extends \yii\db\ActiveRecord
{ {
return [ return [
'id' => 'ID', 'id' => 'ID',
'title' => 'Title', 'sha256' => 'Sha256',
'type' => 'Type', 'original_name' => 'Original Name',
]; ];
} }
} }

75
models/Parameter.php Normal file
View File

@ -0,0 +1,75 @@
<?php
namespace app\models;
use Yii;
use yii\db\ActiveQuery;
/**
* This is the model class for table "parameters".
*
* @property int $id
* @property string $title
* @property int $type
*/
class Parameter extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'parameters';
}
public function getIcon()
{
return $this->hasOne(Image::class, [ 'id' => 'icon' ]);
}
public function getIconGray()
{
return $this->hasOne(Image::class, [ 'id' => 'icon_gray' ]);
}
public function fields()
{
$fields = parent::fields();
$fields['icon'] = function (Parameter $parameter) {
return $parameter->relatedRecords['icon'];
};
$fields['icon_gray'] = function (Parameter $parameter) {
return $parameter->relatedRecords['iconGray'];
};
return $fields;
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['title', 'type'], 'required'],
[['type'], 'integer'],
[['title'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
'type' => 'Type (either 1 or 2)',
'icon' => 'Icon',
'icon_gray' => 'Gray icon',
];
}
}

View File

@ -5,7 +5,7 @@ openapi: '3.1.0'
tags: tags:
- name: Routes - name: Routes
paths: paths:
/api/list: /api:
get: get:
tags: tags:
- Routes - Routes
@ -25,23 +25,22 @@ paths:
"id": 0, "id": 0,
"title": "A thing happening somewhere", "title": "A thing happening somewhere",
"type": 1, "type": 1,
"icons": null "icon": null,
"icon_gray": null
}, },
{ {
"id": 2, "id": 2,
"title": "The fox jumping over a cat", "title": "The fox jumping over a cat",
"type": 1, "type": 1,
"icons": { "icon": {
"icon": { "original_name": "fox_jump.jpeg",
"original_name": "fox_jump.jpeg", "url": "/images/SHA256_HASH.jpeg",
"url": "/images/SHA256_HASH.jpeg", "sha256": "SHA256"
"sha256": "SHA256" },
}, "icon_gray": {
"icon_gray": { "original_name": "fox_jump.gray.jpeg",
"original_name": "fox_jump.gray.jpeg", "url": "/images/SHA256_HASH.jpeg",
"url": "/images/SHA256_HASH.jpeg", "sha256": "SHA256"
"sha256": "SHA256"
}
} }
} }
] ]
@ -74,13 +73,10 @@ components:
type: type:
type: number type: number
example: 0 example: 0
icons: icon:
type: object $ref: '#/components/schemas/Image'
properties: icon_gray:
icon: $ref: '#/components/schemas/Image'
$ref: '#/components/schemas/Image'
icon_gray:
$ref: '#/components/schemas/Image'
required: required:
- id - id
- title - title