From 294a78cd95cc92d253a1419615894f79c161a189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sun, 10 Nov 2024 03:26:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/biz/database_server.go | 16 ---------------- internal/data/database_server.go | 13 +++++++++++++ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/internal/biz/database_server.go b/internal/biz/database_server.go index f854f98d05..6453adfc5e 100644 --- a/internal/biz/database_server.go +++ b/internal/biz/database_server.go @@ -1,7 +1,6 @@ package biz import ( - "errors" "time" "github.com/go-rat/utils/crypt" @@ -63,21 +62,6 @@ func (r *DatabaseServer) AfterFind(tx *gorm.DB) error { return nil } -// TODO 检查放到业务层 -func (r *DatabaseServer) BeforeDelete(tx *gorm.DB) error { - if r.Name == "local_mysql" && !app.IsCli { - return errors.New("can't delete local_mysql, if you must delete it, please uninstall mysql") - } - if r.Name == "local_postgresql" && !app.IsCli { - return errors.New("can't delete local_postgresql, if you must delete it, please uninstall postgresql") - } - if r.Name == "local_redis" && !app.IsCli { - return errors.New("can't delete local_redis, if you must delete it, please uninstall redis") - } - - return nil -} - type DatabaseServerRepo interface { Count() (int64, error) List(page, limit uint) ([]*DatabaseServer, int64, error) diff --git a/internal/data/database_server.go b/internal/data/database_server.go index b405b20211..98bf6cbb62 100644 --- a/internal/data/database_server.go +++ b/internal/data/database_server.go @@ -1,6 +1,10 @@ package data import ( + "errors" + "slices" + "strings" + "github.com/TheTNB/panel/internal/app" "github.com/TheTNB/panel/internal/biz" "github.com/TheTNB/panel/internal/http/request" @@ -66,5 +70,14 @@ func (d databaseServerRepo) Update(req *request.DatabaseServerCreate) error { } func (d databaseServerRepo) Delete(id uint) error { + ds := new(biz.DatabaseServer) + if err := app.Orm.Where("id = ?", id).First(ds).Error; err != nil { + return err + } + + if slices.Contains([]string{"local_mysql", "local_postgresql", "local_redis"}, ds.Name) && !app.IsCli { + return errors.New("can't delete " + ds.Name + ", if you must delete it, please uninstall " + strings.TrimPrefix(ds.Name, "local_")) + } + return app.Orm.Delete(&biz.DatabaseServer{}, id).Error }