From 58252caa20b165c90061420fa58af510b9058b79 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Wed, 8 Feb 2023 18:12:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E7=BD=91=E7=AB=99=E4=B8=AD=E6=8A=A5=E9=94=99=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E9=94=81=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/website.go | 5 ++++- backend/app/service/website.go | 16 +++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index 0a19035b5..90e6c8d80 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -79,11 +79,14 @@ func (b *BaseApi) CreateWebsite(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - err := websiteService.CreateWebsite(req) + tx, ctx := helper.GetTxAndContext() + err := websiteService.CreateWebsite(ctx, req) if err != nil { + tx.Rollback() helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } + tx.Commit() helper.SuccessWithData(c, nil) } diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 95364a62b..4f2c18215 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -30,7 +30,7 @@ type WebsiteService struct { type IWebsiteService interface { PageWebsite(req request.WebsiteSearch) (int64, []response.WebsiteDTO, error) GetWebsites() ([]response.WebsiteDTO, error) - CreateWebsite(create request.WebsiteCreate) error + CreateWebsite(ctx context.Context, create request.WebsiteCreate) error OpWebsite(req request.WebsiteOp) error GetWebsiteOptions() ([]string, error) Backup(id uint) error @@ -96,7 +96,7 @@ func (w WebsiteService) GetWebsites() ([]response.WebsiteDTO, error) { return websiteDTOs, nil } -func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error { +func (w WebsiteService) CreateWebsite(ctx context.Context, create request.WebsiteCreate) error { if exist, _ := websiteRepo.GetBy(websiteRepo.WithDomain(create.PrimaryDomain)); len(exist) > 0 { return buserr.New(constant.ErrNameIsExist) } @@ -123,7 +123,6 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error { ErrorLog: true, } - tx, ctx := getTxAndContext() var appInstall *model.AppInstall switch create.Type { case constant.Deployment: @@ -161,7 +160,6 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error { } domainModel, err := getDomain(domain, website.ID) if err != nil { - tx.Rollback() return err } if reflect.DeepEqual(domainModel, model.WebsiteDomain{}) { @@ -171,18 +169,10 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error { } if len(domains) > 0 { if err := websiteDomainRepo.BatchCreate(ctx, domains); err != nil { - tx.Rollback() return err } } - - if err := configDefaultNginx(website, domains, appInstall); err != nil { - tx.Rollback() - return err - } - - tx.Commit() - return nil + return configDefaultNginx(website, domains, appInstall) } func (w WebsiteService) OpWebsite(req request.WebsiteOp) error {