From e52498b9383ebf704f6eaf8ced4103771b7cbbec Mon Sep 17 00:00:00 2001 From: zjc <1569294420@qq.com> Date: Tue, 15 Apr 2025 05:26:40 -0700 Subject: [PATCH 1/3] feat:add component and componentLibrary --- api/component-library/config/routes.json | 36 +++++++++ .../controllers/component-library.js | 34 ++++++++ .../models/component-library.js | 12 +++ .../models/component-library.settings.json | 78 +++++++++++++++++++ .../services/component-library.js | 12 +++ .../material-category-relation.settings.json | 6 ++ api/materials/models/materials.settings.json | 6 +- api/tenant/models/tenant.settings.json | 4 + .../controllers/user-components.js | 39 +--------- 9 files changed, 187 insertions(+), 40 deletions(-) create mode 100644 api/component-library/config/routes.json create mode 100644 api/component-library/controllers/component-library.js create mode 100644 api/component-library/models/component-library.js create mode 100644 api/component-library/models/component-library.settings.json create mode 100644 api/component-library/services/component-library.js diff --git a/api/component-library/config/routes.json b/api/component-library/config/routes.json new file mode 100644 index 0000000..7d0e8c2 --- /dev/null +++ b/api/component-library/config/routes.json @@ -0,0 +1,36 @@ +{ + "routes": [ + { + "method": "GET", + "path": "/component-library", + "handler": "component-library.find", + "config": { + "policies": [] + } + }, + { + "method": "POST", + "path": "/component-library", + "handler": "component-library.create", + "config": { + "policies": [] + } + }, + { + "method": "PUT", + "path": "/component-library/:id", + "handler": "component-library.update", + "config": { + "policies": [] + } + }, + { + "method": "DELETE", + "path": "/component-library/:id", + "handler": "component-library.delete", + "config": { + "policies": [] + } + } + ] +} diff --git a/api/component-library/controllers/component-library.js b/api/component-library/controllers/component-library.js new file mode 100644 index 0000000..841b521 --- /dev/null +++ b/api/component-library/controllers/component-library.js @@ -0,0 +1,34 @@ +/** +* Copyright (c) 2023 - present TinyEngine Authors. +* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. +* +* Use of this source code is governed by an MIT-style license. +* +* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, +* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR +* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. +* +*/ +const _ = require('lodash'); +const { sanitizeEntity } = require('strapi-utils'); +const { ERROR_TYPE, UNIT_TYPE, AUTH_TYPE } = require('../../../config/constants'); +const { throwErrors, getPublicSuperAdmin, isTenantAdmin } = require('../../../config/toolkits'); + +const { isTruthy, + findAllMaterial, + handlePublicScope +} = require('../../../config/material'); + +module.exports = { + async delete(ctx) { + const { id } = ctx.params; + const entity = await strapi.services['component-library'].delete({ id }); + try{ + await strapi.services['user-components'].delete({ library: id }); + } catch (error) { + strapi.log.error('user-component delete failed', error); + return res; + } + return res; + } +}; diff --git a/api/component-library/models/component-library.js b/api/component-library/models/component-library.js new file mode 100644 index 0000000..738e48c --- /dev/null +++ b/api/component-library/models/component-library.js @@ -0,0 +1,12 @@ +/** +* Copyright (c) 2023 - present TinyEngine Authors. +* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. +* +* Use of this source code is governed by an MIT-style license. +* +* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, +* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR +* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. +* +*/ +module.exports = {}; diff --git a/api/component-library/models/component-library.settings.json b/api/component-library/models/component-library.settings.json new file mode 100644 index 0000000..45410f2 --- /dev/null +++ b/api/component-library/models/component-library.settings.json @@ -0,0 +1,78 @@ +{ + "kind": "collectionType", + "collectionName": "component_library", + "info": { + "name": "Component-Library", + "description": "" + }, + "options": { + "increments": true, + "timestamps": true, + "comment": "", + "draftAndPublish": true + }, + "attributes": { + "name": { + "type": "string" + }, + "packageName": { + "type": "string" + }, + "version": { + "type": "string" + }, + "framework": { + "type": "enumeration", + "enum": [ + "Html", + "Angular", + "React", + "Vue" + ], + "required": true + }, + "script": { + "type": "string" + }, + "css": { + "type": "json" + }, + "description": { + "type": "string" + }, + "thumbnail": { + "type": "string" + }, + "isDefault": { + "type": "boolean", + "default": false + }, + "isOfficial": { + "type": "boolean", + "default": false + }, + "public": { + "type": "integer", + "default": 0, + "required": false + }, + "createdBy": { + "plugin": "users-permissions", + "model": "user" + }, + "updatedBy": { + "plugin": "users-permissions", + "model": "user" + }, + "public_scope_tenants": { + "collection": "tenant", + "via": "component_library", + "collectionName": "component_library_tenant", + "dominant": true + }, + "materials": { + "via": "component_library", + "collection": "materials" + } + } +} diff --git a/api/component-library/services/component-library.js b/api/component-library/services/component-library.js new file mode 100644 index 0000000..738e48c --- /dev/null +++ b/api/component-library/services/component-library.js @@ -0,0 +1,12 @@ +/** +* Copyright (c) 2023 - present TinyEngine Authors. +* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. +* +* Use of this source code is governed by an MIT-style license. +* +* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, +* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR +* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. +* +*/ +module.exports = {}; diff --git a/api/material-category-relation/models/material-category-relation.settings.json b/api/material-category-relation/models/material-category-relation.settings.json index 21d5b87..64a8006 100644 --- a/api/material-category-relation/models/material-category-relation.settings.json +++ b/api/material-category-relation/models/material-category-relation.settings.json @@ -17,6 +17,12 @@ "material": { "model": "materials" }, + "materials": { + "collection": "materials", + "via": "material_category_relations", + "collectionName": "materials__material_category_relations", + "dominant": true + }, "category_code": { "type": "string", "required": true diff --git a/api/materials/models/materials.settings.json b/api/materials/models/materials.settings.json index 50cb21e..12e6a08 100644 --- a/api/materials/models/materials.settings.json +++ b/api/materials/models/materials.settings.json @@ -92,10 +92,12 @@ }, "material_category_relations": { "collection": "material-category-relation", - "via": "material" + "via": "materials" }, "component_library": { - "type": "string" + "collection": "component-library", + "via": "materials", + "dominant": true } } } diff --git a/api/tenant/models/tenant.settings.json b/api/tenant/models/tenant.settings.json index cc0a53f..1ea5848 100644 --- a/api/tenant/models/tenant.settings.json +++ b/api/tenant/models/tenant.settings.json @@ -42,6 +42,10 @@ "user_components": { "via": "public_scope_tenants", "collection": "user-components" + }, + "component_library": { + "via": "public_scope_tenants", + "collection": "component-library" } } } diff --git a/api/user-components/controllers/user-components.js b/api/user-components/controllers/user-components.js index 15c1a03..cd08cd8 100644 --- a/api/user-components/controllers/user-components.js +++ b/api/user-components/controllers/user-components.js @@ -23,17 +23,6 @@ const { const idRegExp = /^[0-9]+$/; // Tips: 非必要情况下正则不要用g参数;如果必须使用,需要尽量限制使用范围,不要设为全局变量。 module.exports = { - async find(ctx) { - const { list } = await findAllMaterial(ctx.session.user, ctx.request.query, 'user-components', 'user_components', [ - 'createdBy' - ]); - return list.map((item) => - sanitizeEntity( - { ...item, createdBy: filterUserField(item.createdBy) }, - { model: strapi.models['user-components'] } - ) - ); - }, async pagination(ctx) { const { list, total } = await findAllMaterial( @@ -59,33 +48,7 @@ module.exports = { const { list } = await findAllMaterial(ctx.session.user, ctx.request.query, 'user-components', 'user_components'); return list.length; }, - - async update(ctx) { - const { id } = ctx.params; - const newData = { ...ctx.request.body }; - - let currentPublicScope = newData.public; - if (!isTruthy(currentPublicScope)) { - currentPublicScope = (await strapi.services['user-components'].findOne({ id })).public; - } - - handlePublicScope(currentPublicScope, newData); - handleTinyReserved(ctx.session.user, newData); - - const component = await strapi.services['user-components'].update({ id }, newData); - return sanitizeEntity(component, { model: strapi.models['user-components'] }); - }, - - async create(ctx) { - const data = { ...ctx.request.body }; - - handlePublicScope(data.public, data); - handleTinyReserved(ctx.session.user, data, true); - - const component = await strapi.services['user-components'].create(data); - return sanitizeEntity(component, { model: strapi.models['user-components'] }); - }, - + async associated(ctx) { const { id } = ctx.params; if (!idRegExp.test(id)) { From 34e8046a1d0e91ce9778beba23b364516894b038 Mon Sep 17 00:00:00 2001 From: zjc <1569294420@qq.com> Date: Tue, 15 Apr 2025 18:51:09 -0700 Subject: [PATCH 2/3] feat:modify issues --- .../controllers/component-library.js | 31 ++++++++++--------- .../models/component-library.settings.json | 9 ++++-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/api/component-library/controllers/component-library.js b/api/component-library/controllers/component-library.js index 841b521..912faae 100644 --- a/api/component-library/controllers/component-library.js +++ b/api/component-library/controllers/component-library.js @@ -9,26 +9,29 @@ * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. * */ -const _ = require('lodash'); -const { sanitizeEntity } = require('strapi-utils'); -const { ERROR_TYPE, UNIT_TYPE, AUTH_TYPE } = require('../../../config/constants'); -const { throwErrors, getPublicSuperAdmin, isTenantAdmin } = require('../../../config/toolkits'); -const { isTruthy, - findAllMaterial, - handlePublicScope -} = require('../../../config/material'); +const { sanitizeEntity } = require("strapi-utils"); +const { throwErrors } = require('../../../config/toolkits'); +const { ERROR_TYPE } = require('../../../config/constants'); module.exports = { async delete(ctx) { const { id } = ctx.params; - const entity = await strapi.services['component-library'].delete({ id }); try{ - await strapi.services['user-components'].delete({ library: id }); - } catch (error) { - strapi.log.error('user-component delete failed', error); - return res; + const res = await strapi.services['component-library'].delete({ id }); + try{ + await strapi.services['user-components'].delete({ library: id }); + } catch (error) { + strapi.log.error('user-component delete failed', error); + throwErrors('user-component delete failed.', ERROR_TYPE.badRequest); + } + return sanitizeEntity(res, {model: strapi.models['component-library']}); + + }catch(error) { + strapi.log.error('component-library delete failed', error); + throwErrors('component-library delete failed.', ERROR_TYPE.badRequest); } - return res; + + return sanitizeEntity(res, {model: strapi.models['component-library']});; } }; diff --git a/api/component-library/models/component-library.settings.json b/api/component-library/models/component-library.settings.json index 45410f2..24f9431 100644 --- a/api/component-library/models/component-library.settings.json +++ b/api/component-library/models/component-library.settings.json @@ -13,13 +13,16 @@ }, "attributes": { "name": { - "type": "string" + "type": "string", + "required": true }, "packageName": { - "type": "string" + "type": "string", + "required": true }, "version": { - "type": "string" + "type": "string", + "required": true }, "framework": { "type": "enumeration", From 8544a2a40876f415b8e56d6dbf5c9d9b19738b34 Mon Sep 17 00:00:00 2001 From: zjc <1569294420@qq.com> Date: Thu, 24 Apr 2025 18:42:47 -0700 Subject: [PATCH 3/3] feat:modify material --- api/materials/models/materials.settings.json | 4 +++- api/user-components/models/user-components.settings.json | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/api/materials/models/materials.settings.json b/api/materials/models/materials.settings.json index 12e6a08..8b77d92 100644 --- a/api/materials/models/materials.settings.json +++ b/api/materials/models/materials.settings.json @@ -53,7 +53,9 @@ "type": "json" }, "user_components": { - "type": "string" + "via": "materials", + "collection": "user-components", + "dominant": true }, "latest": { "model": "material-histories" diff --git a/api/user-components/models/user-components.settings.json b/api/user-components/models/user-components.settings.json index 8d0aab8..2bce163 100644 --- a/api/user-components/models/user-components.settings.json +++ b/api/user-components/models/user-components.settings.json @@ -114,6 +114,10 @@ }, "library": { "type": "integer" + }, + "materials": { + "via": "user_components", + "collection": "materials" } } }