소스 검색

fix disable endpoint not work (#17)

* fix disable endpoint not work

* fix disable endpoint

* refactor the disable endpoint

* fix comment
非法操作 6 달 전
부모
커밋
0f19a092ef
2개의 변경된 파일31개의 추가작업 그리고 30개의 파일을 삭제
  1. 2 20
      internal/service/endpoint.go
  2. 29 10
      internal/service/install_service/state.go

+ 2 - 20
internal/service/endpoint.go

@@ -194,17 +194,8 @@ func Endpoint(
 }
 
 func EnableEndpoint(endpoint_id string, tenant_id string) *entities.Response {
-	endpoint, err := db.GetOne[models.Endpoint](
-		db.Equal("id", endpoint_id),
-		db.Equal("tenant_id", tenant_id),
-	)
-	if err != nil {
-		return exception.NotFoundError(errors.New("endpoint not found")).ToResponse()
-	}
-
-	endpoint.Enabled = true
 
-	if err := install_service.EnabledEndpoint(&endpoint); err != nil {
+	if err := install_service.EnabledEndpoint(endpoint_id, tenant_id); err != nil {
 		return exception.InternalServerError(errors.New("failed to enable endpoint")).ToResponse()
 	}
 
@@ -212,17 +203,8 @@ func EnableEndpoint(endpoint_id string, tenant_id string) *entities.Response {
 }
 
 func DisableEndpoint(endpoint_id string, tenant_id string) *entities.Response {
-	endpoint, err := db.GetOne[models.Endpoint](
-		db.Equal("id", endpoint_id),
-		db.Equal("tenant_id", tenant_id),
-	)
-	if err != nil {
-		return exception.NotFoundError(errors.New("Endpoint not found")).ToResponse()
-	}
-
-	endpoint.Enabled = false
 
-	if err := install_service.DisabledEndpoint(&endpoint); err != nil {
+	if err := install_service.DisabledEndpoint(endpoint_id, tenant_id); err != nil {
 		return exception.InternalServerError(errors.New("failed to disable endpoint")).ToResponse()
 	}
 

+ 29 - 10
internal/service/install_service/state.go

@@ -144,12 +144,22 @@ func UninstallEndpoint(endpoint *models.Endpoint) error {
 	})
 }
 
-func EnabledEndpoint(endpoint *models.Endpoint) error {
-	if endpoint.Enabled {
-		return nil
-	}
-
+func EnabledEndpoint(endpoint_id string, tenant_id string) error {
 	return db.WithTransaction(func(tx *gorm.DB) error {
+		endpoint, err := db.GetOne[models.Endpoint](
+			db.WithTransactionContext(tx),
+			db.Equal("id", endpoint_id),
+			db.Equal("tenant_id", tenant_id),
+			db.WLock(),
+		)
+		if err != nil {
+			return err
+		}
+
+		if endpoint.Enabled {
+			return nil
+		}
+
 		endpoint.Enabled = true
 		if err := db.Update(endpoint, tx); err != nil {
 			return err
@@ -168,12 +178,21 @@ func EnabledEndpoint(endpoint *models.Endpoint) error {
 	})
 }
 
-func DisabledEndpoint(endpoint *models.Endpoint) error {
-	if !endpoint.Enabled {
-		return nil
-	}
-
+func DisabledEndpoint(endpoint_id string, tenant_id string) error {
 	return db.WithTransaction(func(tx *gorm.DB) error {
+		endpoint, err := db.GetOne[models.Endpoint](
+			db.WithTransactionContext(tx),
+			db.Equal("id", endpoint_id),
+			db.Equal("tenant_id", tenant_id),
+			db.WLock(),
+		)
+		if err != nil {
+			return err
+		}
+		if !endpoint.Enabled {
+			return nil
+		}
+
 		endpoint.Enabled = false
 		if err := db.Update(endpoint, tx); err != nil {
 			return err