From cd8a10d799b2b5251abbfdfe8e7d283bc00613ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Wed, 27 Nov 2024 01:08:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=95=B0=E6=8D=AE=E5=BA=93=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E4=BC=98=E5=8C=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/biz/database_server.go | 1 + internal/biz/database_user.go | 1 + internal/data/database_server.go | 4 + internal/data/database_user.go | 22 ++++- internal/http/request/database_server.go | 5 + internal/http/request/database_user.go | 5 + internal/route/http.go | 2 + internal/service/database_server.go | 15 +++ internal/service/database_user.go | 15 +++ web/src/api/panel/database/index.ts | 7 +- web/src/i18n/zh_CN.json | 2 +- .../views/database/CreateDatabaseModal.vue | 2 +- web/src/views/database/CreateServerModal.vue | 13 ++- web/src/views/database/CreateUserModal.vue | 99 ++++++++++--------- web/src/views/database/IndexView.vue | 4 +- web/src/views/database/ServerList.vue | 35 +++++++ web/src/views/database/UserList.vue | 24 ++++- web/src/views/website/IndexView.vue | 2 +- 18 files changed, 196 insertions(+), 62 deletions(-) diff --git a/internal/biz/database_server.go b/internal/biz/database_server.go index 1e928e8b52..a98bfbf57c 100644 --- a/internal/biz/database_server.go +++ b/internal/biz/database_server.go @@ -56,6 +56,7 @@ type DatabaseServerRepo interface { Get(id uint) (*DatabaseServer, error) Create(req *request.DatabaseServerCreate) error Update(req *request.DatabaseServerUpdate) error + UpdateRemark(req *request.DatabaseServerUpdateRemark) error Delete(id uint) error Sync(id uint) error } diff --git a/internal/biz/database_user.go b/internal/biz/database_user.go index 4a37af8e36..8f87c0b423 100644 --- a/internal/biz/database_user.go +++ b/internal/biz/database_user.go @@ -57,6 +57,7 @@ type DatabaseUserRepo interface { Get(id uint) (*DatabaseUser, error) Create(req *request.DatabaseUserCreate) error Update(req *request.DatabaseUserUpdate) error + UpdateRemark(req *request.DatabaseUserUpdateRemark) error Delete(id uint) error DeleteByServerID(serverID uint) error } diff --git a/internal/data/database_server.go b/internal/data/database_server.go index 52234096fc..e9d41abcdd 100644 --- a/internal/data/database_server.go +++ b/internal/data/database_server.go @@ -89,6 +89,10 @@ func (r databaseServerRepo) Update(req *request.DatabaseServerUpdate) error { return app.Orm.Save(server).Error } +func (r databaseServerRepo) UpdateRemark(req *request.DatabaseServerUpdateRemark) error { + return app.Orm.Model(&biz.DatabaseServer{}).Where("id = ?", req.ID).Update("remark", req.Remark).Error +} + func (r databaseServerRepo) Delete(id uint) error { // 删除服务器下的所有用户 if err := NewDatabaseUserRepo().DeleteByServerID(id); err != nil { diff --git a/internal/data/database_user.go b/internal/data/database_user.go index e1a3e3de3b..e58d03e457 100644 --- a/internal/data/database_user.go +++ b/internal/data/database_user.go @@ -88,12 +88,17 @@ func (r databaseUserRepo) Create(req *request.DatabaseUserCreate) error { user := &biz.DatabaseUser{ ServerID: req.ServerID, Username: req.Username, - Password: req.Password, Host: req.Host, - Remark: req.Remark, } - return app.Orm.Create(user).Error + if err = app.Orm.FirstOrInit(user, user).Error; err != nil { + return err + } + + user.Password = req.Password + user.Remark = req.Remark + + return app.Orm.Save(user).Error } func (r databaseUserRepo) Update(req *request.DatabaseUserUpdate) error { @@ -146,6 +151,17 @@ func (r databaseUserRepo) Update(req *request.DatabaseUserUpdate) error { return app.Orm.Save(user).Error } +func (r databaseUserRepo) UpdateRemark(req *request.DatabaseUserUpdateRemark) error { + user, err := r.Get(req.ID) + if err != nil { + return err + } + + user.Remark = req.Remark + + return app.Orm.Save(user).Error +} + func (r databaseUserRepo) Delete(id uint) error { user, err := r.Get(id) if err != nil { diff --git a/internal/http/request/database_server.go b/internal/http/request/database_server.go index 6b35401f2f..53c9316240 100644 --- a/internal/http/request/database_server.go +++ b/internal/http/request/database_server.go @@ -19,3 +19,8 @@ type DatabaseServerUpdate struct { Password string `form:"password" json:"password"` Remark string `form:"remark" json:"remark"` } + +type DatabaseServerUpdateRemark struct { + ID uint `form:"id" json:"id" validate:"required,exists=database_servers id"` + Remark string `form:"remark" json:"remark"` +} diff --git a/internal/http/request/database_user.go b/internal/http/request/database_user.go index a168052cc6..660b6cba18 100644 --- a/internal/http/request/database_user.go +++ b/internal/http/request/database_user.go @@ -15,3 +15,8 @@ type DatabaseUserUpdate struct { Privileges []string `form:"privileges" json:"privileges"` Remark string `form:"remark" json:"remark"` } + +type DatabaseUserUpdateRemark struct { + ID uint `form:"id" json:"id" validate:"required,exists=database_users id"` + Remark string `form:"remark" json:"remark"` +} diff --git a/internal/route/http.go b/internal/route/http.go index d107574507..481494a99d 100644 --- a/internal/route/http.go +++ b/internal/route/http.go @@ -74,6 +74,7 @@ func Http(r chi.Router) { r.Get("/", server.List) r.Post("/", server.Create) r.Put("/{id}", server.Update) + r.Put("/{id}/remark", server.UpdateRemark) r.Delete("/{id}", server.Delete) r.Post("/{id}/sync", server.Sync) }) @@ -83,6 +84,7 @@ func Http(r chi.Router) { r.Get("/", user.List) r.Post("/", user.Create) r.Put("/{id}", user.Update) + r.Put("/{id}/remark", user.UpdateRemark) r.Delete("/{id}", user.Delete) }) diff --git a/internal/service/database_server.go b/internal/service/database_server.go index a6931aaa94..0869ccdfdd 100644 --- a/internal/service/database_server.go +++ b/internal/service/database_server.go @@ -69,6 +69,21 @@ func (s *DatabaseServer) Update(w http.ResponseWriter, r *http.Request) { Success(w, nil) } +func (s *DatabaseServer) UpdateRemark(w http.ResponseWriter, r *http.Request) { + req, err := Bind[request.DatabaseServerUpdateRemark](r) + if err != nil { + Error(w, http.StatusUnprocessableEntity, "%v", err) + return + } + + if err = s.databaseServerRepo.UpdateRemark(req); err != nil { + Error(w, http.StatusInternalServerError, "%v", err) + return + } + + Success(w, nil) +} + func (s *DatabaseServer) Delete(w http.ResponseWriter, r *http.Request) { req, err := Bind[request.ID](r) if err != nil { diff --git a/internal/service/database_user.go b/internal/service/database_user.go index 2200b641ce..1ec01d03ab 100644 --- a/internal/service/database_user.go +++ b/internal/service/database_user.go @@ -69,6 +69,21 @@ func (s *DatabaseUser) Update(w http.ResponseWriter, r *http.Request) { Success(w, nil) } +func (s *DatabaseUser) UpdateRemark(w http.ResponseWriter, r *http.Request) { + req, err := Bind[request.DatabaseUserUpdateRemark](r) + if err != nil { + Error(w, http.StatusUnprocessableEntity, "%v", err) + return + } + + if err = s.databaseUserRepo.UpdateRemark(req); err != nil { + Error(w, http.StatusInternalServerError, "%v", err) + return + } + + Success(w, nil) +} + func (s *DatabaseUser) Delete(w http.ResponseWriter, r *http.Request) { req, err := Bind[request.ID](r) if err != nil { diff --git a/web/src/api/panel/database/index.ts b/web/src/api/panel/database/index.ts index ca296969eb..67c856ee51 100644 --- a/web/src/api/panel/database/index.ts +++ b/web/src/api/panel/database/index.ts @@ -18,6 +18,9 @@ export default { serverDelete: (id: number) => http.Delete(`/databaseServer/${id}`), // 同步数据库 serverSync: (id: number) => http.Post(`/databaseServer/${id}/sync`), + // 更新服务器备注 + serverRemark: (id: number, remark: string) => + http.Put(`/databaseServer/${id}/remark`, { remark }), // 获取数据库用户列表 userList: (page: number, limit: number) => http.Get('/databaseUser', { params: { page, limit } }), // 创建数据库用户 @@ -25,5 +28,7 @@ export default { // 更新数据库用户 userUpdate: (id: number, data: any) => http.Put(`/databaseUser/${id}`, data), // 删除数据库用户 - userDelete: (id: number) => http.Delete(`/databaseUser/${id}`) + userDelete: (id: number) => http.Delete(`/databaseUser/${id}`), + // 更新用户备注 + userRemark: (id: number, remark: string) => http.Put(`/databaseUser/${id}/remark`, { remark }) } diff --git a/web/src/i18n/zh_CN.json b/web/src/i18n/zh_CN.json index 28cf916270..42b8b26501 100644 --- a/web/src/i18n/zh_CN.json +++ b/web/src/i18n/zh_CN.json @@ -39,7 +39,7 @@ "title": "应用", "alerts": { "cache": "缓存更新成功", - "warning": "更新应用前强烈建议先备份/快照,以免出现问题时无法回滚!", + "warning": "更新应用前强烈建议先备份/快照,以免出现问题时无法第一时间回滚!", "setup": "设置成功", "install": "任务已提交,请前往后台任务查看任务进度", "update": "任务已提交,请前往后台任务查看任务进度", diff --git a/web/src/views/database/CreateDatabaseModal.vue b/web/src/views/database/CreateDatabaseModal.vue index d59dca7830..0d431741b4 100644 --- a/web/src/views/database/CreateDatabaseModal.vue +++ b/web/src/views/database/CreateDatabaseModal.vue @@ -76,7 +76,7 @@ onMounted(() => { v-model:value="createModel.username" type="text" @keydown.enter.prevent - placeholder="输入授权用户名" + placeholder="输入授权用户名(留空不授权)" /> diff --git a/web/src/views/database/CreateServerModal.vue b/web/src/views/database/CreateServerModal.vue index 31027dec3d..773d241433 100644 --- a/web/src/views/database/CreateServerModal.vue +++ b/web/src/views/database/CreateServerModal.vue @@ -18,6 +18,17 @@ const databaseType = [ { label: 'PostgreSQL', value: 'postgresql' } ] +watch( + () => createModel.value.type, + (value) => { + if (value === 'mysql') { + createModel.value.port = 3306 + } else if (value === 'postgresql') { + createModel.value.port = 5432 + } + } +) + const handleCreate = () => { useRequest(() => database.serverCreate(createModel.value)).onSuccess(() => { show.value = false @@ -31,7 +42,7 @@ const handleCreate = () => { ('show', { type: Boolean, required: true }) const createModel = ref({ - name: '', - type: 'mysql', - host: '127.0.0.1', - port: 3306, + server_id: null, username: '', password: '', + host: 'localhost', + privileges: [], remark: '' }) -const databaseType = [ - { label: 'MySQL', value: 'mysql' }, - { label: 'PostgreSQL', value: 'postgresql' } +const servers = ref<{ label: string; value: string }[]>([]) + +const hostType = [ + { label: '本地(localhost)', value: 'localhost' }, + { label: '所有(%)', value: '%' }, + { label: '指定', value: '' } ] const handleCreate = () => { - useRequest(() => database.serverCreate(createModel.value)).onSuccess(() => { + useRequest(() => database.userCreate(createModel.value)).onSuccess(() => { show.value = false - window.$message.success('添加成功') - window.$bus.emit('database:refresh') + window.$message.success('创建成功') + window.$bus.emit('database-user:refresh') }) } + +onMounted(() => { + database.serverList(1, 10000).then((data: any) => { + for (const server of data.items) { + servers.value.push({ + label: server.name, + value: server.id + }) + } + }) +})