Skip to content

Commit

Permalink
feat(sys): impl delete app api & page
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 9, 2021
1 parent b4bef78 commit 7dd294c
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 13 deletions.
13 changes: 13 additions & 0 deletions packages/system-client/src/api/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ export async function updateApplication(appid, { name }) {
return res
}

/**
* 删除(释放)应用
* @param {*} appid
* @returns
*/
export async function removeApplication(appid) {
const res = await request({
url: `/apps/${appid}`,
method: 'delete'
})
return res
}

/**
* 添加协作成员
* @param {member_id, roles}
Expand Down
25 changes: 18 additions & 7 deletions packages/system-client/src/views/application/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@
</template>
</el-table-column>
<el-table-column fixed="right" label="操作" align="center" width="280" class-name="small-padding">
<template slot-scope="{row,$index}">
<template slot-scope="{row}">
<el-button type="success" size="mini" @click="toDetail(row)">
开发管理
</el-button>
<el-button plain type="primary" size="mini" @click="showUpdateForm(row)">
编辑
</el-button>
<el-button plain size="mini" type="default" @click="deleteApp(row,$index)">
<el-button plain size="mini" type="default" @click="deleteApp(row)">
释放
</el-button>
</template>
Expand Down Expand Up @@ -109,14 +109,14 @@
</template>
</el-table-column>
<el-table-column fixed="right" label="操作" align="center" width="280" class-name="small-padding">
<template slot-scope="{row,$index}">
<template slot-scope="{row}">
<el-button type="success" size="mini" @click="toDetail(row)">
开发管理
</el-button>
<el-button plain type="primary" size="mini" @click="showUpdateForm(row)">
编辑
</el-button>
<el-button plain size="mini" type="default" @click="deleteApp(row,$index)">
<el-button plain size="mini" type="default" @click="deleteApp(row)">
释放
</el-button>
</template>
Expand Down Expand Up @@ -151,7 +151,8 @@
</template>

<script>
import { createApplication, getMyApplications, startApplicationService, stopApplicationService, removeApplicationService, updateApplication } from '@/api/application'
import { createApplication, getMyApplications, startApplicationService, stopApplicationService, removeApplicationService, updateApplication, removeApplication } from '@/api/application'
import { showError, showSuccess } from '@/utils/show'
// 默认化创建表单的值
function getDefaultFormValue() {
return {
Expand Down Expand Up @@ -276,8 +277,18 @@ export default {
this.dialogFormVisible = false
})
},
async deleteApp() {
this.$message('尚未实现此功能')
async deleteApp(row) {
await this.$confirm('应用被删除后,暂不可恢复,确定释放?', '确认释放应用?')
console.log(row)
if (row.status !== 'cleared' && row.status !== 'created') { return showError('请先停止并清除该应用的服务') }
this.loading = true
const res = await removeApplication(row.appid)
.finally(() => { this.loading = false })
if (res.error) showError(res.error)
showSuccess('应用已释放: ' + row.name)
this.loadApps()
},
onCopy() {
this.$message.success('已复制')
Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/api/application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-28 22:00:45
* @LastEditTime: 2021-09-08 18:39:01
* @LastEditTime: 2021-09-09 10:43:23
* @Description: Application APIs
*/

Expand All @@ -22,7 +22,7 @@ export interface ApplicationStruct {
name: string
created_by: string
appid: string
status: 'created' | 'running' | 'stopped'
status: 'created' | 'running' | 'stopped' | 'cleared'
config: {
db_name: string
db_user: string
Expand Down
45 changes: 45 additions & 0 deletions packages/system-server/src/api/recycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as assert from "assert"
import { Constants } from "../constants"
import { DatabaseAgent } from "../lib/db-agent"

/**
* Recycle collector
*/
export class RecycleCollector {
collection: string

constructor(collection: string) {
assert.ok(collection, 'empty collection got')
this.collection = collection
}

async insert(data: any) {
const db = DatabaseAgent.sys_db
const r = await db.collection(Constants.cn.recycles)
.add({
collection: this.collection,
data,
created_at: Date.now()
})

return r.id
}

async retrieve(_id: string) {
const db = DatabaseAgent.sys_db
const r = await db.collection(Constants.cn.recycles)
.where({ _id: _id, collection: this.collection })
.getOne()

return r.data
}

async remove(_id: string) {
const db = DatabaseAgent.sys_db
const r = await db.collection(Constants.cn.recycles)
.where({ _id: _id, collection: this.collection })
.remove()

return r.deleted
}
}
1 change: 1 addition & 0 deletions packages/system-server/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const Constants = {
deploy_targets: coll_prefix + 'deploy_targets',
deploy_requests: coll_prefix + 'deploy_requests',
applications: coll_prefix + 'applications',
recycles: coll_prefix + 'recycles',
},

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/system-server/src/router/application/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-29 11:35:11
* @LastEditTime: 2021-09-09 00:43:39
* @LastEditTime: 2021-09-09 10:49:48
* @Description:
*/

Expand All @@ -10,6 +10,7 @@ import { handleNotImplemented } from '../common'
import { handleGetCollaborators, handleGetRoles, handleInviteCollaborator, handleRemoveCollaborator, handleSearchCollaborator } from './collaborator'
import { handleCreateApplication } from './create'
import { handleGetApplicationByAppid, handleMyApplications } from './get'
import { handleRemoveApplication } from './remove'
import { handleStopApplicationService, handleStartApplicationService, handleRemoveApplicationService } from './service'
import { handleUpdateApplication } from './update'

Expand Down Expand Up @@ -53,7 +54,7 @@ ApplicationRouter.post('/:appid/service/remove', handleRemoveApplicationService)
/**
* Remove an application by appid
*/
ApplicationRouter.delete('/:appid', handleNotImplemented)
ApplicationRouter.delete('/:appid', handleRemoveApplication)

/**
* Get collaborators of application
Expand Down
53 changes: 53 additions & 0 deletions packages/system-server/src/router/application/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 15:22:34
* @LastEditTime: 2021-09-09 11:00:29
* @Description:
*/

import * as assert from 'assert'
import { Request, Response } from 'express'
import { RecycleCollector } from '../../api/recycle'
import { getApplicationByAppid } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { Constants } from '../../constants'
import { DatabaseAgent } from '../../lib/db-agent'

const { APPLICATION_REMOVE } = Constants.permissions

/**
* The handler of removing application
* @param req
* @param res
* @returns
*/
export async function handleRemoveApplication(req: Request, res: Response) {
const uid = req['auth']?.uid
const appid = req.params.appid
const app = await getApplicationByAppid(appid)
if (!app)
return res.status(422).send('invalid appid')

if (app.status !== 'cleared' && app.status !== 'created') {
return res.status(422).send('app status must be cleared or created')
}

// check permission
const code = await checkPermission(uid, APPLICATION_REMOVE.name, app)
if (code) {
return res.status(code).send()
}

// save app to recycle collection
const recycle = new RecycleCollector(Constants.cn.applications)
const saved = await recycle.insert(app)
assert.ok(saved, 'recycle insert got empty return value')

// remove app
const db = DatabaseAgent.sys_db
const ret = await db.collection(Constants.cn.applications)
.where({ appid })
.remove()

return res.send({ data: ret })
}
4 changes: 2 additions & 2 deletions packages/system-server/src/router/application/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-31 15:00:04
* @LastEditTime: 2021-09-08 23:52:11
* @LastEditTime: 2021-09-09 10:42:32
* @Description:
*/

Expand Down Expand Up @@ -105,7 +105,7 @@ export async function handleRemoveApplicationService(req: Request, res: Response

await db.collection(Constants.cn.applications)
.where({ appid: app.appid })
.update({ status: 'removed' })
.update({ status: 'cleared' })

return res.send({
data: {
Expand Down

0 comments on commit 7dd294c

Please sign in to comment.