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

fix(service): fix collection service set get API #658

Merged
merged 1 commit into from
Aug 13, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConnectionModel>('connections', id, connection)
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/ExportData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/database/models/ConnectionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
24 changes: 13 additions & 11 deletions src/database/services/CollectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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<ConnectionModelTree[] | undefined> {
// 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()
Expand Down
2 changes: 1 addition & 1 deletion src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ declare global {
requestProblemInformation?: boolean
will?: WillModel
clientIdWithTime?: boolean
collectionId?: string | null
parentId?: string | null
isCollection: false
orderId?: number
}
Expand Down
9 changes: 6 additions & 3 deletions src/views/connections/ConnectionForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -629,6 +631,7 @@ export default class ConnectionCreate extends Vue {
}
} else {
this.$message.error(msgError)
this.$log.error(msgError.toString())
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/views/connections/ConnectionsList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -484,7 +484,7 @@ export default class ConnectionsList extends Vue {
treeTravel(treeNode)
})
} else {
treeNode.collectionId = null
treeNode.parentId = null
this.$emit('delete', treeNode)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/connections/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class Connections extends Vue {
cert: '',
key: '',
isCollection: false,
collectionId: null,
parentId: null,
}

@Watch('$route.params.id')
Expand Down
2 changes: 1 addition & 1 deletion src/views/window/Window.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class Window extends Vue {
cert: '',
key: '',
isCollection: false,
collectionId: null,
parentId: null,
}

get connectionId(): string {
Expand Down