changeset 4392:024b16a1c253

Implemented deletion of stretches.
author Sascha Wilde <wilde@intevation.de>
date Thu, 12 Sep 2019 20:08:29 +0200
parents d9088bb96ce1
children cdd332dbebbf
files client/src/components/stretches/Stretches.vue pkg/controllers/routes.go pkg/imports/dst.go pkg/imports/modelconvert.go pkg/models/imports.go
diffstat 5 files changed, 141 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/stretches/Stretches.vue	Thu Sep 12 19:14:10 2019 +0200
+++ b/client/src/components/stretches/Stretches.vue	Thu Sep 12 20:08:29 2019 +0200
@@ -207,29 +207,19 @@
         });
     },
     deleteStretch(stretch) {
-      this.$store.commit("application/popup", {
-        icon: "trash",
-        title: this.$gettext("Delete Stretch"),
-        content:
-          this.$gettext("Do you really want to delete this stretch:") +
-          `<br>
-        <b>${stretch.properties.name}, ${
-            stretch.properties.source_organization
-          } (${stretch.properties.countries})</b>`,
-        confirm: {
-          label: this.$gettext("Delete"),
-          icon: "trash",
-          callback: () => {
-            displayInfo({
-              title: this.$gettext("Not implemented"),
-              message: this.$gettext("Deleting ") + stretch.id
-            });
-          }
+      HTTP.post(
+        "/imports/dst",
+        {
+          id: stretch.id.split(".")[1] * 1
         },
-        cancel: {
-          label: this.$gettext("Cancel"),
-          icon: "times"
+        {
+          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
         }
+      ).then(() => {
+        displayInfo({
+          title: this.$gettext("Staging Area"),
+          message: this.$gettext("Deleting ") + stretch.properties.objnam
+        });
       });
     },
     moveMapToStretch(stretch) {
--- a/pkg/controllers/routes.go	Thu Sep 12 19:14:10 2019 +0200
+++ b/pkg/controllers/routes.go	Thu Sep 12 20:08:29 2019 +0200
@@ -241,7 +241,7 @@
 	kinds := strings.Join([]string{
 		"bn", "gm", "fa", "wx", "wa",
 		"wg", "dmv", "fd", "dma",
-		"sec", "dsec",
+		"sec", "dsec", "dst",
 	}, "|")
 
 	api.Handle("/imports/{kind:"+kinds+"}", waterwayAdmin(&mw.JSONHandler{
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/imports/dst.go	Thu Sep 12 20:08:29 2019 +0200
@@ -0,0 +1,117 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2019 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+//  * Sascha Wilde <wilde@intevation.de>
+
+package imports
+
+import (
+	"context"
+	"database/sql"
+	"fmt"
+)
+
+// DeleteStretch is a Job to delete a stretch from the database.
+type DeleteStretch struct {
+	ID int64 `json:"id"`
+}
+
+// DSTJobKind is the import queue type identifier.
+const DSTJobKind JobKind = "dst"
+
+type dstJobCreator struct{}
+
+func init() { RegisterJobCreator(DSTJobKind, dstJobCreator{}) }
+
+func (dstJobCreator) Description() string { return "delete stretch" }
+
+func (dstJobCreator) AutoAccept() bool { return false }
+
+func (dstJobCreator) Create() Job { return new(DeleteStretch) }
+
+func (dstJobCreator) Depends() [2][]string {
+	return [2][]string{
+		{"stretches"},
+		{},
+	}
+}
+
+const (
+	dstExistsSQL = `
+SELECT EXISTS (
+  SELECT 1 FROM users.stretches
+  WHERE id = $1 AND staging_done)
+`
+	dstStageDoneSQL = `
+DELETE FROM users.stretches
+WHERE id IN (
+  SELECT key from import.track_imports
+  WHERE import_id = $1 AND
+        deletion AND
+        relation = 'users.stretches'::regclass)`
+)
+
+// StageDone finally removes the stretch from the database.
+func (dstJobCreator) StageDone(
+	ctx context.Context,
+	tx *sql.Tx,
+	id int64,
+) error {
+	_, err := tx.ExecContext(ctx, dstStageDoneSQL, id)
+	return err
+}
+
+// CleanUp of a stretch delete import is a NOP.
+func (*DeleteStretch) CleanUp() error { return nil }
+
+// Do prepares the deletion of the stretch.
+func (dst *DeleteStretch) Do(
+	ctx context.Context,
+	importID int64,
+	conn *sql.Conn,
+	feedback Feedback,
+) (interface{}, error) {
+
+	tx, err := conn.BeginTx(ctx, nil)
+	if err != nil {
+		return nil, err
+	}
+	defer tx.Rollback()
+
+	var found bool
+	if err := tx.QueryRowContext(ctx, dstExistsSQL, dst.ID).Scan(&found); err != nil {
+		return nil, err
+	}
+
+	if !found {
+		return nil, fmt.Errorf("no stretch with id %d found", dst.ID)
+	}
+
+	feedback.Info("Prepare deletion of stretch with id %d", dst.ID)
+
+	if _, err := tx.ExecContext(
+		ctx,
+		trackImportDeletionSQL,
+		importID,
+		"users.stretches",
+		dst.ID,
+		true,
+	); err != nil {
+		return nil, err
+	}
+
+	if err := tx.Commit(); err != nil {
+		return nil, err
+	}
+
+	return dst, nil
+}
--- a/pkg/imports/modelconvert.go	Thu Sep 12 19:14:10 2019 +0200
+++ b/pkg/imports/modelconvert.go	Thu Sep 12 20:08:29 2019 +0200
@@ -30,6 +30,7 @@
 	STJobKind:   func() interface{} { return new(models.StretchImport) },
 	SECJobKind:  func() interface{} { return new(models.SectionImport) },
 	DSECJobKind: func() interface{} { return new(models.SectionDelete) },
+	DSTJobKind:  func() interface{} { return new(models.StretchDelete) },
 }
 
 // ImportModelForJobKind returns the constructor function to
@@ -164,8 +165,13 @@
 	},
 
 	DSECJobKind: func(input interface{}) interface{} {
-		ds := input.(*models.SectionDelete)
-		return &DeleteSection{ID: ds.ID}
+		dsec := input.(*models.SectionDelete)
+		return &DeleteSection{ID: dsec.ID}
+	},
+
+	DSTJobKind: func(input interface{}) interface{} {
+		dst := input.(*models.StretchDelete)
+		return &DeleteStretch{ID: dst.ID}
 	},
 }
 
--- a/pkg/models/imports.go	Thu Sep 12 19:14:10 2019 +0200
+++ b/pkg/models/imports.go	Thu Sep 12 20:08:29 2019 +0200
@@ -124,6 +124,10 @@
 	SectionDelete struct {
 		ID int64 `json:"id"`
 	}
+
+	StretchDelete struct {
+		ID int64 `json:"id"`
+	}
 )
 
 func (cui *ConfigurableURLImport) MarshalAttributes(attrs common.Attributes) error {