From 603bd4ad67775236387a4ec9c25ef6ba0d5074c6 Mon Sep 17 00:00:00 2001 From: Michael Hierweck Date: Wed, 3 Apr 2019 14:29:12 +0200 Subject: [PATCH] PoC: data set based input validation (backend) --- .../org/hostsharing/hsadminng/service/ShareService.java | 8 ++++++++ src/main/webapp/i18n/de/custom-error.json | 6 ++++++ src/main/webapp/i18n/en/custom-error.json | 6 ++++++ 3 files changed, 20 insertions(+) create mode 100644 src/main/webapp/i18n/de/custom-error.json create mode 100644 src/main/webapp/i18n/en/custom-error.json diff --git a/src/main/java/org/hostsharing/hsadminng/service/ShareService.java b/src/main/java/org/hostsharing/hsadminng/service/ShareService.java index 925deb7e..8bb7878d 100644 --- a/src/main/java/org/hostsharing/hsadminng/service/ShareService.java +++ b/src/main/java/org/hostsharing/hsadminng/service/ShareService.java @@ -1,9 +1,11 @@ package org.hostsharing.hsadminng.service; import org.hostsharing.hsadminng.domain.Share; +import org.hostsharing.hsadminng.domain.enumeration.ShareAction; import org.hostsharing.hsadminng.repository.ShareRepository; import org.hostsharing.hsadminng.service.dto.ShareDTO; import org.hostsharing.hsadminng.service.mapper.ShareMapper; +import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,6 +42,12 @@ public class ShareService { */ public ShareDTO save(ShareDTO shareDTO) { log.debug("Request to save Share : {}", shareDTO); + if((shareDTO.getAction() == ShareAction.SUBSCRIPTION) && (shareDTO.getQuantity() <= 0)) { + throw new BadRequestAlertException("Share subscriptions require a positive quantity", "share", "sharesubscriptionpositivquantity"); + } + if((shareDTO.getAction() == ShareAction.CANCELLATION) && (shareDTO.getQuantity() >= 0)) { + throw new BadRequestAlertException("Share cancellations require a negative quantity", "share", "sharecancellationnegativequantity"); + } Share share = shareMapper.toEntity(shareDTO); share = shareRepository.save(share); return shareMapper.toDto(share); diff --git a/src/main/webapp/i18n/de/custom-error.json b/src/main/webapp/i18n/de/custom-error.json new file mode 100644 index 00000000..169f2b09 --- /dev/null +++ b/src/main/webapp/i18n/de/custom-error.json @@ -0,0 +1,6 @@ +{ + "error": { + "sharesubscriptionpositivquantity": "Zeichnungen von Geschäftsanteilen erfordern eine positive Stückzahl", + "sharecancellationnegativequantity": "Kündigungen von Geschäftsanteilen erfordern eine negative Stückzahl" + } +} diff --git a/src/main/webapp/i18n/en/custom-error.json b/src/main/webapp/i18n/en/custom-error.json new file mode 100644 index 00000000..6c6a14d4 --- /dev/null +++ b/src/main/webapp/i18n/en/custom-error.json @@ -0,0 +1,6 @@ +{ + "error": { + "sharesubscriptionpositivquantity": "Share subscriptions require a positive quantity", + "sharecancellationnegativequantity": "Share cancellations require a negative quantity" + } +}