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' => [
'request' => [
// !!! 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' => [
'class' => 'yii\caching\FileCache',
@ -42,14 +45,13 @@ $config = [
],
],
'db' => $db,
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[ 'class' => 'yii\rest\UrlRule', 'controller' => 'Api' ],
],
],
*/
],
'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;
/**
* This is the model class for table "parameters".
* This is the model class for table "image".
*
* @property int $id
* @property string $title
* @property int $type
* @property string $sha256
* @property string $original_name
*/
class Parameters extends \yii\db\ActiveRecord
class Image extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'parameters';
return 'image';
}
/**
@ -27,9 +27,8 @@ class Parameters extends \yii\db\ActiveRecord
public function rules()
{
return [
[['title', 'type'], 'required'],
[['type'], 'integer'],
[['title'], 'string', 'max' => 255],
[['sha256', 'original_name'], 'required'],
[['sha256', 'original_name'], 'string', 'max' => 255],
];
}
@ -40,8 +39,8 @@ class Parameters extends \yii\db\ActiveRecord
{
return [
'id' => 'ID',
'title' => 'Title',
'type' => 'Type',
'sha256' => 'Sha256',
'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:
- name: Routes
paths:
/api/list:
/api:
get:
tags:
- Routes
@ -25,13 +25,13 @@ paths:
"id": 0,
"title": "A thing happening somewhere",
"type": 1,
"icons": null
"icon": null,
"icon_gray": null
},
{
"id": 2,
"title": "The fox jumping over a cat",
"type": 1,
"icons": {
"icon": {
"original_name": "fox_jump.jpeg",
"url": "/images/SHA256_HASH.jpeg",
@ -43,7 +43,6 @@ paths:
"sha256": "SHA256"
}
}
}
]
components:
schemas:
@ -74,9 +73,6 @@ components:
type:
type: number
example: 0
icons:
type: object
properties:
icon:
$ref: '#/components/schemas/Image'
icon_gray: