Skip to content

Commit 6ba323a

Browse files
authored
feat: add component and componentLibrary (#5)
1 parent dcec056 commit 6ba323a

10 files changed

Lines changed: 200 additions & 41 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"routes": [
3+
{
4+
"method": "GET",
5+
"path": "/component-library",
6+
"handler": "component-library.find",
7+
"config": {
8+
"policies": []
9+
}
10+
},
11+
{
12+
"method": "POST",
13+
"path": "/component-library",
14+
"handler": "component-library.create",
15+
"config": {
16+
"policies": []
17+
}
18+
},
19+
{
20+
"method": "PUT",
21+
"path": "/component-library/:id",
22+
"handler": "component-library.update",
23+
"config": {
24+
"policies": []
25+
}
26+
},
27+
{
28+
"method": "DELETE",
29+
"path": "/component-library/:id",
30+
"handler": "component-library.delete",
31+
"config": {
32+
"policies": []
33+
}
34+
}
35+
]
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2023 - present TinyEngine Authors.
3+
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4+
*
5+
* Use of this source code is governed by an MIT-style license.
6+
*
7+
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8+
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9+
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10+
*
11+
*/
12+
13+
const { sanitizeEntity } = require("strapi-utils");
14+
const { throwErrors } = require('../../../config/toolkits');
15+
const { ERROR_TYPE } = require('../../../config/constants');
16+
17+
module.exports = {
18+
async delete(ctx) {
19+
const { id } = ctx.params;
20+
try{
21+
const res = await strapi.services['component-library'].delete({ id });
22+
try{
23+
await strapi.services['user-components'].delete({ library: id });
24+
} catch (error) {
25+
strapi.log.error('user-component delete failed', error);
26+
throwErrors('user-component delete failed.', ERROR_TYPE.badRequest);
27+
}
28+
return sanitizeEntity(res, {model: strapi.models['component-library']});
29+
30+
}catch(error) {
31+
strapi.log.error('component-library delete failed', error);
32+
throwErrors('component-library delete failed.', ERROR_TYPE.badRequest);
33+
}
34+
35+
return sanitizeEntity(res, {model: strapi.models['component-library']});;
36+
}
37+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2023 - present TinyEngine Authors.
3+
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4+
*
5+
* Use of this source code is governed by an MIT-style license.
6+
*
7+
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8+
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9+
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10+
*
11+
*/
12+
module.exports = {};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"kind": "collectionType",
3+
"collectionName": "component_library",
4+
"info": {
5+
"name": "Component-Library",
6+
"description": ""
7+
},
8+
"options": {
9+
"increments": true,
10+
"timestamps": true,
11+
"comment": "",
12+
"draftAndPublish": true
13+
},
14+
"attributes": {
15+
"name": {
16+
"type": "string",
17+
"required": true
18+
},
19+
"packageName": {
20+
"type": "string",
21+
"required": true
22+
},
23+
"version": {
24+
"type": "string",
25+
"required": true
26+
},
27+
"framework": {
28+
"type": "enumeration",
29+
"enum": [
30+
"Html",
31+
"Angular",
32+
"React",
33+
"Vue"
34+
],
35+
"required": true
36+
},
37+
"script": {
38+
"type": "string"
39+
},
40+
"css": {
41+
"type": "json"
42+
},
43+
"description": {
44+
"type": "string"
45+
},
46+
"thumbnail": {
47+
"type": "string"
48+
},
49+
"isDefault": {
50+
"type": "boolean",
51+
"default": false
52+
},
53+
"isOfficial": {
54+
"type": "boolean",
55+
"default": false
56+
},
57+
"public": {
58+
"type": "integer",
59+
"default": 0,
60+
"required": false
61+
},
62+
"createdBy": {
63+
"plugin": "users-permissions",
64+
"model": "user"
65+
},
66+
"updatedBy": {
67+
"plugin": "users-permissions",
68+
"model": "user"
69+
},
70+
"public_scope_tenants": {
71+
"collection": "tenant",
72+
"via": "component_library",
73+
"collectionName": "component_library_tenant",
74+
"dominant": true
75+
},
76+
"materials": {
77+
"via": "component_library",
78+
"collection": "materials"
79+
}
80+
}
81+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2023 - present TinyEngine Authors.
3+
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4+
*
5+
* Use of this source code is governed by an MIT-style license.
6+
*
7+
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8+
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9+
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10+
*
11+
*/
12+
module.exports = {};

api/material-category-relation/models/material-category-relation.settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
"material": {
1818
"model": "materials"
1919
},
20+
"materials": {
21+
"collection": "materials",
22+
"via": "material_category_relations",
23+
"collectionName": "materials__material_category_relations",
24+
"dominant": true
25+
},
2026
"category_code": {
2127
"type": "string",
2228
"required": true

api/materials/models/materials.settings.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
"type": "json"
5454
},
5555
"user_components": {
56-
"type": "string"
56+
"via": "materials",
57+
"collection": "user-components",
58+
"dominant": true
5759
},
5860
"latest": {
5961
"model": "material-histories"
@@ -92,10 +94,12 @@
9294
},
9395
"material_category_relations": {
9496
"collection": "material-category-relation",
95-
"via": "material"
97+
"via": "materials"
9698
},
9799
"component_library": {
98-
"type": "string"
100+
"collection": "component-library",
101+
"via": "materials",
102+
"dominant": true
99103
}
100104
}
101105
}

api/tenant/models/tenant.settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
"user_components": {
4343
"via": "public_scope_tenants",
4444
"collection": "user-components"
45+
},
46+
"component_library": {
47+
"via": "public_scope_tenants",
48+
"collection": "component-library"
4549
}
4650
}
4751
}

api/user-components/controllers/user-components.js

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ const {
2323
const idRegExp = /^[0-9]+$/; // Tips: 非必要情况下正则不要用g参数;如果必须使用,需要尽量限制使用范围,不要设为全局变量。
2424

2525
module.exports = {
26-
async find(ctx) {
27-
const { list } = await findAllMaterial(ctx.session.user, ctx.request.query, 'user-components', 'user_components', [
28-
'createdBy'
29-
]);
30-
return list.map((item) =>
31-
sanitizeEntity(
32-
{ ...item, createdBy: filterUserField(item.createdBy) },
33-
{ model: strapi.models['user-components'] }
34-
)
35-
);
36-
},
3726

3827
async pagination(ctx) {
3928
const { list, total } = await findAllMaterial(
@@ -59,33 +48,7 @@ module.exports = {
5948
const { list } = await findAllMaterial(ctx.session.user, ctx.request.query, 'user-components', 'user_components');
6049
return list.length;
6150
},
62-
63-
async update(ctx) {
64-
const { id } = ctx.params;
65-
const newData = { ...ctx.request.body };
66-
67-
let currentPublicScope = newData.public;
68-
if (!isTruthy(currentPublicScope)) {
69-
currentPublicScope = (await strapi.services['user-components'].findOne({ id })).public;
70-
}
71-
72-
handlePublicScope(currentPublicScope, newData);
73-
handleTinyReserved(ctx.session.user, newData);
74-
75-
const component = await strapi.services['user-components'].update({ id }, newData);
76-
return sanitizeEntity(component, { model: strapi.models['user-components'] });
77-
},
78-
79-
async create(ctx) {
80-
const data = { ...ctx.request.body };
81-
82-
handlePublicScope(data.public, data);
83-
handleTinyReserved(ctx.session.user, data, true);
84-
85-
const component = await strapi.services['user-components'].create(data);
86-
return sanitizeEntity(component, { model: strapi.models['user-components'] });
87-
},
88-
51+
8952
async associated(ctx) {
9053
const { id } = ctx.params;
9154
if (!idRegExp.test(id)) {

api/user-components/models/user-components.settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@
114114
},
115115
"library": {
116116
"type": "integer"
117+
},
118+
"materials": {
119+
"via": "user_components",
120+
"collection": "materials"
117121
}
118122
}
119123
}

0 commit comments

Comments
 (0)