Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add grouping for interfaces #193

Merged
merged 18 commits into from Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions app/controller/api/group.js
@@ -0,0 +1,50 @@
'use strict';

const { Controller } = require('egg');

class GroupController extends Controller {

async create() {
const ctx = this.ctx;
const { belongedUniqId, groupName, groupType } = ctx.request.body;
ctx.assertParam({ belongedUniqId, groupName, groupType });
const res = await ctx.service.group.createGroup({
belongedUniqId,
groupName,
groupType,
});
ctx.success(res);
}

async update() {
const ctx = this.ctx;
const uniqId = ctx.params.uniqId;
const { groupName } = ctx.request.body;
const res = await ctx.service.group.updateGroupName({
uniqId,
groupName,
});
ctx.success(res);
}

async delete() {
const ctx = this.ctx;
const { uniqId } = ctx.params;
const res = await ctx.service.group.deleteGroupByUniqId({ uniqId });
if (res) {
ctx.success(res);
return;
}
ctx.fail(ctx.gettext('common.delete.fail'));
}

async showAll() {
const ctx = this.ctx;
const { belongedUniqId, groupType } = ctx.query;
ctx.assertParam({ belongedUniqId, groupType });
const res = await ctx.service.group.queryGroupByBelongedUniqId({ belongedUniqId, groupType });
ctx.success(res);
}
}

module.exports = GroupController;
13 changes: 9 additions & 4 deletions app/controller/api/interface.js
Expand Up @@ -11,7 +11,7 @@ class InterfaceController extends Controller {
const ctx = this.ctx;
const { projectUniqId } = ctx.query;
ctx.assertParam({ projectUniqId });
const res = await ctx.service.interface.queryInterfaceByProjectUniqId({ projectUniqId });
const res = await ctx.service.interface.queryInterfaceDataByProjectUniqId({ projectUniqId });
ctx.success(res);
}

Expand All @@ -24,10 +24,14 @@ class InterfaceController extends Controller {

async create() {
const ctx = this.ctx;
const { projectUniqId, pathname, method, description } = ctx.request.body;
ctx.assertParam({ projectUniqId, pathname, method, description });
const { projectUniqId, pathname, method, description, groupUniqId } = ctx.request.body;
ctx.assertParam({ projectUniqId, pathname, method, description, groupUniqId });
const res = await ctx.service.interface.createInterface({
projectUniqId, pathname, method: method.toUpperCase(), description,
projectUniqId,
pathname,
method: method.toUpperCase(),
description,
groupUniqId,
});
ctx.success(res);
}
Expand All @@ -40,6 +44,7 @@ class InterfaceController extends Controller {
'pathname',
'method',
'description',
'groupUniqId',
'currentScene',
'proxyConfig',
].forEach(i => {
Expand Down
10 changes: 5 additions & 5 deletions app/controller/api/project.js
Expand Up @@ -26,22 +26,22 @@ class ProjectController extends Controller {
const _res = await ctx.service.project.queryAllProject();
for (const _item of _res) {
const item = _item.get({ plain: true });
const iterfaceList = await ctx.service.interface.queryInterfaceByProjectUniqId({
const interfaceList = await ctx.service.interface.queryInterfaceByProjectUniqId({
projectUniqId: item.uniqId,
});
const allSceneList = await Promise.all(iterfaceList.map(({ uniqId: interfaceUniqId }) => {
const allSceneList = await Promise.all(interfaceList.map(({ uniqId: interfaceUniqId }) => {
return ctx.service.scene.querySceneByInterfaceUniqId({ interfaceUniqId });
}));
let bufSize = 0;
for (const sceneList of allSceneList) {
for (const scene of sceneList) {
const buf = new Buffer(JSON.stringify(scene.data));
const buf = new Buffer.from(JSON.stringify(scene.data));
bufSize += buf.length;
}
}

item.capacity = {
count: iterfaceList.length,
count: interfaceList.length,
size: filesize(bufSize),
};
res.push(item);
Expand Down Expand Up @@ -98,7 +98,7 @@ class ProjectController extends Controller {
const { uniqId } = ctx.params;
const res = await ctx.service.transfer.downloadProject({ uniqId });

ctx.body = JSON.stringify(res.data, null, 2);
ctx.body = JSON.stringify(res.dataGroupList, null, 2);
ctx.attachment(res.fileName);
}

Expand Down
42 changes: 42 additions & 0 deletions app/model/Group.js
@@ -0,0 +1,42 @@
'use strict';

module.exports = app => {
const {
STRING,
UUID,
UUIDV4,
} = app.Sequelize;

const Group = app.model.define('group', {
uniqId: {
type: UUID,
defaultValue: UUIDV4,
primaryKey: true,
allowNull: false,
},
groupName: {
type: STRING,
allowNull: false,
},
groupType: {
type: STRING,
allowNull: false,
},
belongedUniqId: {
type: STRING,
allowNull: false,
},
}, {
...app.config.modelCommonOption,
indexes: [
{
fields: [
'uniqId',
],
unique: true,
},
],
});

return Group;
};
4 changes: 4 additions & 0 deletions app/model/Interface.js
Expand Up @@ -47,6 +47,10 @@ module.exports = app => {
primaryKey: true,
allowNull: false,
},
groupUniqId: {
type: STRING,
allowNull: true,
},
}, {
...app.config.modelCommonOption,
indexes: [
Expand Down
5 changes: 5 additions & 0 deletions app/router.js
Expand Up @@ -46,6 +46,11 @@ module.exports = app => {
router.put('/api/scene/:uniqId', controller.api.scene.update);
router.delete('/api/scene/:uniqId', controller.api.scene.delete);

router.get('/api/group', controller.api.group.showAll);
router.post('/api/group', controller.api.group.create);
router.put('/api/group/:uniqId', controller.api.group.update);
router.delete('/api/group/:uniqId', controller.api.group.delete);

router.get('/api/preview/scene', controller.api.preview.scene);

router.get('/api/schema', controller.api.schema.showAll);
Expand Down
61 changes: 61 additions & 0 deletions app/service/group.js
@@ -0,0 +1,61 @@
'use strict';

const { Service } = require('egg');

class GroupService extends Service {
async queryGroupByBelongedUniqId({
belongedUniqId,
groupType,
}) {
return await this.ctx.model.Group.findAll({
where: {
belongedUniqId,
groupType,
},
order: [
[
'createdAt',
'ASC',
],
],
});
}

async createGroup({
belongedUniqId,
groupName,
groupType,
}) {
return await this.ctx.model.Group.create({
belongedUniqId,
groupName,
groupType,
});
}

async updateGroupName({
uniqId,
groupName,
}) {
return await this.ctx.model.Group.update(
{ groupName },
{
where: {
uniqId,
},
}
);
}

async deleteGroupByUniqId({
uniqId,
}) {
return await this.ctx.model.Group.destroy({
where: {
uniqId,
},
});
}
}

module.exports = GroupService;