From 949006b68d2b41ec95d4cd10e3096dce69edc9b3 Mon Sep 17 00:00:00 2001 From: b1ek Date: Mon, 19 Aug 2024 22:44:16 +1000 Subject: [PATCH] feat: add image model and api controller --- config/web.php | 8 +-- controllers/ApiController.php | 21 ++++++++ migrations/m240819_102301_image.php | 49 ++++++++++++++++++ models/{Parameters.php => Image.php} | 19 ++++--- models/Parameter.php | 75 ++++++++++++++++++++++++++++ web/openapi.yml | 36 ++++++------- 6 files changed, 175 insertions(+), 33 deletions(-) create mode 100644 controllers/ApiController.php create mode 100644 migrations/m240819_102301_image.php rename models/{Parameters.php => Image.php} (52%) create mode 100644 models/Parameter.php diff --git a/config/web.php b/config/web.php index fac4e62..0d41066 100644 --- a/config/web.php +++ b/config/web.php @@ -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, ]; diff --git a/controllers/ApiController.php b/controllers/ApiController.php new file mode 100644 index 0000000..08fe36f --- /dev/null +++ b/controllers/ApiController.php @@ -0,0 +1,21 @@ +with(['icon', 'iconGray'])->all(); + } + + public function verbs() + { + return [ + 'index' => [ 'GET' ], + ]; + } +} diff --git a/migrations/m240819_102301_image.php b/migrations/m240819_102301_image.php new file mode 100644 index 0000000..8260cc1 --- /dev/null +++ b/migrations/m240819_102301_image.php @@ -0,0 +1,49 @@ +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; + } + */ +} diff --git a/models/Parameters.php b/models/Image.php similarity index 52% rename from models/Parameters.php rename to models/Image.php index 2e7f211..c5c3b23 100644 --- a/models/Parameters.php +++ b/models/Image.php @@ -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', ]; } } diff --git a/models/Parameter.php b/models/Parameter.php new file mode 100644 index 0000000..c7c52e3 --- /dev/null +++ b/models/Parameter.php @@ -0,0 +1,75 @@ +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', + ]; + } +} diff --git a/web/openapi.yml b/web/openapi.yml index 9a0c288..6a13be3 100644 --- a/web/openapi.yml +++ b/web/openapi.yml @@ -5,7 +5,7 @@ openapi: '3.1.0' tags: - name: Routes paths: - /api/list: + /api: get: tags: - Routes @@ -25,23 +25,22 @@ 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", - "sha256": "SHA256" - }, - "icon_gray": { - "original_name": "fox_jump.gray.jpeg", - "url": "/images/SHA256_HASH.jpeg", - "sha256": "SHA256" - } + "icon": { + "original_name": "fox_jump.jpeg", + "url": "/images/SHA256_HASH.jpeg", + "sha256": "SHA256" + }, + "icon_gray": { + "original_name": "fox_jump.gray.jpeg", + "url": "/images/SHA256_HASH.jpeg", + "sha256": "SHA256" } } ] @@ -74,13 +73,10 @@ components: type: type: number example: 0 - icons: - type: object - properties: - icon: - $ref: '#/components/schemas/Image' - icon_gray: - $ref: '#/components/schemas/Image' + icon: + $ref: '#/components/schemas/Image' + icon_gray: + $ref: '#/components/schemas/Image' required: - id - title