changeset 1297:1c0c9190fcf2

Import sounding result: If uploading a zip with a meta.json which has a dataset for bottleneck which already has a scan for a given date issue a message.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 22 Nov 2018 18:16:47 +0100
parents 99c039e86624
children 6590208e3ee1
files pkg/controllers/srimports.go pkg/models/sr.go
diffstat 2 files changed, 19 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/srimports.go	Thu Nov 22 17:00:26 2018 +0100
+++ b/pkg/controllers/srimports.go	Thu Nov 22 18:16:47 2018 +0100
@@ -276,7 +276,7 @@
 	result := struct {
 		Message string `json:"message"`
 	}{
-		Message: fmt.Sprintf("Token %s deletes.", token),
+		Message: fmt.Sprintf("Token %s deleted.", token),
 	}
 	SendJSON(rw, http.StatusOK, &result)
 }
--- a/pkg/models/sr.go	Thu Nov 22 17:00:26 2018 +0100
+++ b/pkg/models/sr.go	Thu Nov 22 18:16:47 2018 +0100
@@ -46,6 +46,11 @@
 
 	checkBottleneckSQL = `
 SELECT true FROM waterway.bottlenecks WHERE objnam = $1`
+
+	checkBottleneckDateUniqueSQL = `
+SELECT true FROM waterway.sounding_results sr JOIN
+  waterway.bottlenecks bn ON sr.bottleneck_id = bn.bottleneck_id
+WHERE bn.objnam = $1 AND sr.date_info = $2`
 )
 
 func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error {
@@ -85,7 +90,7 @@
 		errs = append(errs, errors.New("Unexpected depth reference"))
 	}
 
-	err = conn.QueryRowContext(context.Background(),
+	err = conn.QueryRowContext(ctx,
 		checkBottleneckSQL,
 		m.Bottleneck).Scan(&b)
 	switch {
@@ -97,5 +102,17 @@
 		errs = append(errs, errors.New("Unexpected bottleneck"))
 	}
 
+	err = conn.QueryRowContext(ctx,
+		checkBottleneckDateUniqueSQL,
+		m.Bottleneck, m.Date.Time).Scan(&b)
+	switch {
+	case err == sql.ErrNoRows: // good! -> unique.
+	case err != nil:
+		errs = append(errs, err)
+	case b:
+		errs = append(errs,
+			errors.New("Sounding result for this date already exists."))
+	}
+
 	return errs
 }