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 {