feat: 优化网站删除的逻辑

This commit is contained in:
zhengkunwang223 2023-03-02 11:38:56 +08:00 committed by zhengkunwang223
parent a9aa1d386f
commit 025b98b4f1
2 changed files with 6 additions and 8 deletions

View file

@ -127,11 +127,14 @@ func (b *BaseApi) DeleteWebsite(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
} }
err := websiteService.DeleteWebsite(req) tx, ctx := helper.GetTxAndContext()
err := websiteService.DeleteWebsite(ctx, req)
if err != nil { if err != nil {
tx.Rollback()
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return return
} }
tx.Commit()
helper.SuccessWithData(c, nil) helper.SuccessWithData(c, nil)
} }

View file

@ -34,7 +34,7 @@ type IWebsiteService interface {
OpWebsite(req request.WebsiteOp) error OpWebsite(req request.WebsiteOp) error
GetWebsiteOptions() ([]string, error) GetWebsiteOptions() ([]string, error)
UpdateWebsite(req request.WebsiteUpdate) error UpdateWebsite(req request.WebsiteUpdate) error
DeleteWebsite(req request.WebsiteDelete) error DeleteWebsite(ctx context.Context, req request.WebsiteDelete) error
GetWebsite(id uint) (response.WebsiteDTO, error) GetWebsite(id uint) (response.WebsiteDTO, error)
CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error) CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error)
GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error) GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error)
@ -233,7 +233,7 @@ func (w WebsiteService) GetWebsite(id uint) (response.WebsiteDTO, error) {
return res, nil return res, nil
} }
func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error { func (w WebsiteService) DeleteWebsite(ctx context.Context, req request.WebsiteDelete) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID)) website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil { if err != nil {
return err return err
@ -241,7 +241,6 @@ func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error {
if err := delNginxConfig(website, req.ForceDelete); err != nil { if err := delNginxConfig(website, req.ForceDelete); err != nil {
return err return err
} }
tx, ctx := getTxAndContext()
if req.DeleteApp { if req.DeleteApp {
websites, _ := websiteRepo.GetBy(websiteRepo.WithAppInstallId(website.AppInstallID)) websites, _ := websiteRepo.GetBy(websiteRepo.WithAppInstallId(website.AppInstallID))
@ -266,21 +265,17 @@ func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error {
_ = fileOp.DeleteDir(b.FileDir) _ = fileOp.DeleteDir(b.FileDir)
} }
if err := backupRepo.DeleteRecord(ctx, backupRepo.WithByType("website-"+website.Type), commonRepo.WithByName(website.PrimaryDomain)); err != nil { if err := backupRepo.DeleteRecord(ctx, backupRepo.WithByType("website-"+website.Type), commonRepo.WithByName(website.PrimaryDomain)); err != nil {
tx.Rollback()
return err return err
} }
} }
} }
if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil { if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil {
tx.Rollback()
return err return err
} }
if err := websiteDomainRepo.DeleteBy(ctx, websiteDomainRepo.WithWebsiteId(req.ID)); err != nil { if err := websiteDomainRepo.DeleteBy(ctx, websiteDomainRepo.WithWebsiteId(req.ID)); err != nil {
tx.Rollback()
return err return err
} }
tx.Commit()
return nil return nil
} }