Skip to content

Commit

Permalink
feat: 修改 app 信息时检查 app name 是否存在
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Nov 25, 2023
1 parent 784fc15 commit b93adc9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions internal/api/callback/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var (
ErrAppNameExist = &Msg{
Code: cErrAppNameExist,
Msg: "名称已被占用",
HttpStatus: 400,
HttpStatus: 409,
}
ErrGroupNotFound = &Msg{
Code: cErrGroupNotFound,
Expand All @@ -112,7 +112,7 @@ var (
ErrMfaAlreadyExist = &Msg{
Code: cErrMfaAlreadyExist,
Msg: "双因素校验已开启",
HttpStatus: 400,
HttpStatus: 409,
}
ErrMfaAddExpired = &Msg{
Code: cErrMfaAddExpired,
Expand Down
18 changes: 13 additions & 5 deletions internal/api/controllers/user/dev/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,23 @@ func ApplyApp(c *gin.Context) {
return
}

appSrc, err := service.App.Begin()
appSrv, err := service.App.Begin()
if err != nil {
callback.Error(c, callback.ErrForm, err)
return
}
defer appSrc.Rollback()
defer appSrv.Rollback()

uid := tools.GetUserInfo(c).ID
newApp, err := appSrc.New(uid, f.Name, f.Callback, f.PermitAll)
newApp, err := appSrv.New(uid, f.Name, f.Callback, f.PermitAll)
if err != nil {
callback.Error(c, callback.ErrDBOperation, err)
return
}

var groups = make([]dto.Group, 0)
if !f.PermitAll && len(f.PermitGroups) != 0 {
appGroupSrv := service.AppGroupSrv{DB: appSrc.DB}
appGroupSrv := service.AppGroupSrv{DB: appSrv.DB}

if groups, err = appGroupSrv.BindForApp(newApp.ID, f.PermitGroups); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
Expand All @@ -94,7 +94,7 @@ func ApplyApp(c *gin.Context) {
}
}

if err = redis.AppCode.Add(newApp.AppCode); err != nil || appSrc.Commit().Error != nil {
if err = redis.AppCode.Add(newApp.AppCode); err != nil || appSrv.Commit().Error != nil {
callback.Error(c, callback.ErrUnexpected, err)
return
}
Expand Down Expand Up @@ -204,6 +204,14 @@ func ModifyApp(c *gin.Context) {
appInfoChanged := false

if f.Name != app.Name {
if exist, err := appSrv.NameExist(f.Name); err != nil {
callback.Error(c, callback.ErrDBOperation, err)
return
} else if exist {
callback.Error(c, callback.ErrAppNameExist)
return
}

appInfoChanged = true
}

Expand Down
5 changes: 5 additions & 0 deletions internal/db/dao/App.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (a *App) sqlOderForNav(tx *gorm.DB) *gorm.DB {
return tx.Order("views DESC,name,id DESC")
}

func (a *App) Exist(tx *gorm.DB) (bool, error) {
var t bool
return t, tx.Model(a).Select("1").Where(a).Limit(1).First(&t).Error
}

func (a *App) Insert(tx *gorm.DB) error {
return tx.Create(a).Error
}
Expand Down
6 changes: 6 additions & 0 deletions internal/service/App.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func (a AppSrv) This(host string) *dao.App {
}
}

func (a AppSrv) NameExist(name string, opts ...daoUtil.ServiceOpt) (bool, error) {
return (&dao.App{
Name: name,
}).Exist(daoUtil.TxOpts(a.DB, opts...))
}

func (a AppSrv) New(uid uint, name, callback string, permitAll bool) (*dao.App, error) {
randSrc := rand.NewSource(time.Now().UnixNano())
var t = dao.App{
Expand Down

0 comments on commit b93adc9

Please sign in to comment.