From ff138100ee67eb47237c18e90fdb2c2928a4edb5 Mon Sep 17 00:00:00 2001 From: oceanlvr <657531018@qq.com> Date: Thu, 12 Aug 2021 22:09:30 +0800 Subject: [PATCH] fix(service): fix collection service set get API --- src/api/connection.ts | 2 +- src/components/ExportData.vue | 7 ++++--- src/database/models/ConnectionEntity.ts | 6 +++--- src/database/services/CollectionService.ts | 24 ++++++++++++---------- src/types/global.d.ts | 2 +- src/views/connections/ConnectionForm.vue | 9 +++++--- src/views/connections/ConnectionsList.vue | 6 +++--- src/views/connections/index.vue | 2 +- src/views/window/Window.vue | 2 +- 9 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/api/connection.ts b/src/api/connection.ts index e51306d9c..922e9638f 100644 --- a/src/api/connection.ts +++ b/src/api/connection.ts @@ -3,7 +3,7 @@ import db from '@/database/index' export const updateConnectionCollectionId = (id: string, collectionId: string | null): ConnectionModel => { const connection: ConnectionModel = loadConnection(id) if (connection) { - connection.collectionId = collectionId + connection.parentId = collectionId } return db.update('connections', id, connection) } diff --git a/src/components/ExportData.vue b/src/components/ExportData.vue index d79dd43f7..4bc1a1095 100644 --- a/src/components/ExportData.vue +++ b/src/components/ExportData.vue @@ -44,6 +44,7 @@ import { Component, Vue, Prop, Watch } from 'vue-property-decorator' import { Getter } from 'vuex-class' import { ipcRenderer } from 'electron' import { loadConnections } from '@/api/connection' +import useService from '@/database/useServices' import MyDialog from './MyDialog.vue' import XMLConvert from 'xml-js' import { parse as CSVConvert } from 'json2csv' @@ -119,9 +120,9 @@ export default class ExportData extends Vue { const content = JSON.stringify(data[0], null, 2) return content } else { - const connections: ConnectionModel[] | [] = await loadConnections() - const data = connections - const content = JSON.stringify(data, null, 2) + const { connectionService } = useService() + const connections: ConnectionModel[] | undefined = await connectionService.getAll() + const content = JSON.stringify(connections, null, 2) return content } } diff --git a/src/database/models/ConnectionEntity.ts b/src/database/models/ConnectionEntity.ts index 71e7aaf8f..74c265681 100644 --- a/src/database/models/ConnectionEntity.ts +++ b/src/database/models/ConnectionEntity.ts @@ -68,10 +68,10 @@ export default class ConnectionEntity { // ManyToOne entities @ManyToOne(() => CollectionEntity, (collection) => collection.connections, { onDelete: 'CASCADE', nullable: true }) @JoinColumn({ name: 'parent_id', referencedColumnName: 'id' }) - parent?: CollectionEntity + parent?: CollectionEntity | null - @Column({ name: 'parent_id', nullable: true, default: '' }) - parentId?: string + @Column({ name: 'parent_id', nullable: true, default: null }) + parentId?: string | null // ManyToOne entities ends @Column({ type: 'integer', nullable: true, comment: 'order in the collection' }) diff --git a/src/database/services/CollectionService.ts b/src/database/services/CollectionService.ts index b0ee09691..d78fab52b 100644 --- a/src/database/services/CollectionService.ts +++ b/src/database/services/CollectionService.ts @@ -56,6 +56,10 @@ export default class ConnectionService { ): Promise<{ collection: CollectionEntity[]; connection: ConnectionEntity[] }> { let collection: CollectionEntity[] = [] let connection: ConnectionEntity[] = [] + let parent: CollectionEntity | undefined = undefined + if (parentId) { + parent = await this.collectionRepository.findOne(parentId) + } await Promise.all( children.map(async (treeNode: ConnectionModelTree) => { if (treeNode.isCollection) { @@ -69,14 +73,11 @@ export default class ConnectionService { connections: topConnection, } as CollectionEntity) } else if (!treeNode.isCollection) { - if (parentId) { - const parent: CollectionEntity | undefined = await this.collectionRepository.findOne(parentId) - if (parent) { - connection.push({ - ...treeNode, - parent, - } as ConnectionEntity) - } + if (parent) { + connection.push({ + ...treeNode, + parent, + } as ConnectionEntity) } else { connection.push(treeNode as ConnectionEntity) } @@ -108,16 +109,17 @@ export default class ConnectionService { } const { collection, connection } = await this.travelModel(data) if (collection && connection) { - // maybe not this condition - return data + return [...collection, ...connection] } return } public async getAll(): Promise { + // get top collections const topConnections: ConnectionEntity[] = await this.connectionRepository.find({ - parentId: '', + parentId: null, }) + // get top collections const query: CollectionEntity[] = await this.collectionRepository.manager .getTreeRepository(CollectionEntity) .findTrees() diff --git a/src/types/global.d.ts b/src/types/global.d.ts index 1d90ac715..6d4a7a789 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -230,7 +230,7 @@ declare global { requestProblemInformation?: boolean will?: WillModel clientIdWithTime?: boolean - collectionId?: string | null + parentId?: string | null isCollection: false orderId?: number } diff --git a/src/views/connections/ConnectionForm.vue b/src/views/connections/ConnectionForm.vue index ff105c90b..5c4d500c7 100644 --- a/src/views/connections/ConnectionForm.vue +++ b/src/views/connections/ConnectionForm.vue @@ -552,7 +552,7 @@ export default class ConnectionCreate extends Vue { topicAliasMaximum: undefined, clientIdWithTime: false, isCollection: false, - collectionId: null, + parentId: null, } private record: ConnectionModel = _.cloneDeep(this.defaultRecord) @@ -597,17 +597,19 @@ export default class ConnectionCreate extends Vue { } } - private save() { + private async save() { this.vueForm.validate(async (valid: boolean) => { if (!valid) { return false } const data = { ...this.record } this.trimString(data) - let res: ConnectionModel | null = null + let res: ConnectionModel | undefined = undefined let msgError = '' if (this.oper === 'create') { res = await createConnection(data) + const { connectionService } = useServices() + await connectionService.create(data) msgError = this.$t('common.createfailed') as string } else { if (data.id) { @@ -629,6 +631,7 @@ export default class ConnectionCreate extends Vue { } } else { this.$message.error(msgError) + this.$log.error(msgError.toString()) } }) } diff --git a/src/views/connections/ConnectionsList.vue b/src/views/connections/ConnectionsList.vue index 3abb46557..9da5cd90b 100644 --- a/src/views/connections/ConnectionsList.vue +++ b/src/views/connections/ConnectionsList.vue @@ -262,13 +262,13 @@ export default class ConnectionsList extends Vue { const { connectionService } = useServices() switch (position) { case 'inner': - draggingNode.data.collectionId = dropNode.data.id + draggingNode.data.parentId = dropNode.data.id updateConnectionCollectionId(draggingNode.data.id, dropNode.data.id) await connectionService.updateCollectionId(draggingNode.data.id, dropNode.data.id) break default: if (!dropNode.parent) return - draggingNode.data.collectionId = Array.isArray(dropNode.parent.data) ? null : dropNode.parent.data.id + draggingNode.data.parentId = Array.isArray(dropNode.parent.data) ? null : dropNode.parent.data.id updateConnectionCollectionId(draggingNode.data.id, dropNode.parent.data.id) await connectionService.updateCollectionId(draggingNode.data.id, dropNode.parent.data.id) break @@ -484,7 +484,7 @@ export default class ConnectionsList extends Vue { treeTravel(treeNode) }) } else { - treeNode.collectionId = null + treeNode.parentId = null this.$emit('delete', treeNode) } } diff --git a/src/views/connections/index.vue b/src/views/connections/index.vue index 603289ec2..b2fa5665f 100644 --- a/src/views/connections/index.vue +++ b/src/views/connections/index.vue @@ -72,7 +72,7 @@ export default class Connections extends Vue { cert: '', key: '', isCollection: false, - collectionId: null, + parentId: null, } @Watch('$route.params.id') diff --git a/src/views/window/Window.vue b/src/views/window/Window.vue index 7d2f0067f..f4338d6bc 100644 --- a/src/views/window/Window.vue +++ b/src/views/window/Window.vue @@ -45,7 +45,7 @@ export default class Window extends Vue { cert: '', key: '', isCollection: false, - collectionId: null, + parentId: null, } get connectionId(): string {