Explorar o código

handle delete on backend

Ivan Galakhov %!s(int64=4) %!d(string=hai) anos
pai
achega
dfe9ae9fe5

+ 12 - 1
internal/repository/gorm/slack.go

@@ -23,7 +23,7 @@ func NewSlackIntegrationRepository(
 	return &SlackIntegrationRepository{db, key}
 }
 
-// CreateKubeIntegration creates a new kube auth mechanism
+// CreateSlackIntegration creates a new kube auth mechanism
 func (repo *SlackIntegrationRepository) CreateSlackIntegration(
 	slackInt *ints.SlackIntegration,
 ) (*ints.SlackIntegration, error) {
@@ -58,6 +58,17 @@ func (repo *SlackIntegrationRepository) ListSlackIntegrationsByProjectID(
 	return slackInts, nil
 }
 
+// DeleteSlackIntegration deletes a slack integration by ID
+func (repo *SlackIntegrationRepository) DeleteSlackIntegration(
+	integrationID uint,
+) error {
+	if err := repo.db.Where("id = ?", integrationID).Delete(&ints.SlackIntegration{}).Error; err != nil {
+		return err
+	}
+
+	return nil
+}
+
 // EncryptSlackIntegrationData will encrypt the slack integration data before
 // writing to the DB
 func (repo *SlackIntegrationRepository) EncryptSlackIntegrationData(

+ 1 - 0
internal/repository/integrations.go

@@ -49,6 +49,7 @@ type GithubAppOAuthIntegrationRepository interface {
 type SlackIntegrationRepository interface {
 	CreateSlackIntegration(slackInt *ints.SlackIntegration) (*ints.SlackIntegration, error)
 	ListSlackIntegrationsByProjectID(projectID uint) ([]*ints.SlackIntegration, error)
+	DeleteSlackIntegration(integrationID uint) error
 }
 
 // AWSIntegrationRepository represents the set of queries on the AWS auth

+ 38 - 0
server/api/oauth_slack_handler.go

@@ -128,3 +128,41 @@ func (app *App) HandleListSlackIntegrations(w http.ResponseWriter, r *http.Reque
 		return
 	}
 }
+
+// HandleDeleteSlackIntegration deletes a slack integration for a project by ID
+func (app *App) HandleDeleteSlackIntegration(w http.ResponseWriter, r *http.Request) {
+	// check that slack integration belongs to given project
+	projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
+
+	if err != nil || projID == 0 {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	integrationID, err := strconv.ParseUint(chi.URLParam(r, "slack_integration_id"), 0, 64)
+
+	if err != nil || projID == 0 {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	slackInts, err := app.Repo.SlackIntegration.ListSlackIntegrationsByProjectID(uint(projID))
+
+	if err != nil {
+		app.handleErrorRead(err, ErrProjectDataRead, w)
+		return
+	}
+
+	for _, slackInt := range slackInts {
+		if slackInt.ID == uint(integrationID) {
+			err = app.Repo.SlackIntegration.DeleteSlackIntegration(slackInt.ID)
+			if err != nil {
+				app.handleErrorInternal(err, w)
+				return
+			}
+			w.WriteHeader(http.StatusOK)
+		}
+	}
+
+	w.WriteHeader(http.StatusNotFound)
+}

+ 10 - 0
server/router/router.go

@@ -868,6 +868,16 @@ func New(a *api.App) *chi.Mux {
 				),
 			)
 
+			r.Method(
+				"DELETE",
+				"/projects/{project_id}/slack_integrations/{slack_integration_id}",
+				auth.DoesUserHaveProjectAccess(
+					requestlog.NewHandler(a.HandleDeleteSlackIntegration, l),
+					mw.URLParam,
+					mw.WriteAccess,
+				),
+			)
+
 			// /api/projects/{project_id}/helmrepos routes
 			r.Method(
 				"POST",