Skip to content

Commit

Permalink
feat: 优化删除检查
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 9, 2024
1 parent 8294a1a commit 294a78c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
16 changes: 0 additions & 16 deletions internal/biz/database_server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package biz

import (
"errors"
"time"

"github.com/go-rat/utils/crypt"
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions internal/data/database_server.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 294a78c

Please sign in to comment.