From 427fe9058efd82f0c70885ac0af7c2c0a21fb936 Mon Sep 17 00:00:00 2001 From: "art.dambrine" <2318827-art.dambrine@users.noreply.gitlab.com> Date: Wed, 24 Nov 2021 18:32:06 +0100 Subject: [PATCH] dealing with ISO-8601 UTC date time --- .../service/PartnerService.java | 24 +++++++++++++++++-- src/main/resources/data.sql | 24 +++++++++---------- src/main/resources/schema.sql | 8 +++---- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/example/apispringgradleb2boost/service/PartnerService.java b/src/main/java/com/example/apispringgradleb2boost/service/PartnerService.java index ccd24ac..23d7025 100644 --- a/src/main/java/com/example/apispringgradleb2boost/service/PartnerService.java +++ b/src/main/java/com/example/apispringgradleb2boost/service/PartnerService.java @@ -14,6 +14,7 @@ import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import javax.validation.constraints.Min; +import java.time.ZonedDateTime; import java.util.Arrays; import java.util.Locale; import java.util.Optional; @@ -40,7 +41,7 @@ public class PartnerService { } public Partner savePartner(Partner partner) { - if (localeIsValid(partner.getLocale())) { + if (localeIsValid(partner.getLocale()) && utcDateTimeValidation(partner.getExpirationTime())) { return partnerRepository.save(partner); } else { return null; @@ -63,7 +64,7 @@ public class PartnerService { currentPartner.setLocale(locale); } String expirationTime = partner.getExpirationTime(); - if (expirationTime != null) { + if (utcDateTimeValidation(expirationTime)) { currentPartner.setExpirationTime(expirationTime); } savePartner(currentPartner); @@ -90,4 +91,23 @@ public class PartnerService { )); } } + + /** + * DateTime validation (ISO-8601 UTC date time) before calling database + * + * @param utcDateTimeToCheck + * @return Boolean + */ + private Boolean utcDateTimeValidation(String utcDateTimeToCheck) { + try { + ZonedDateTime.parse(utcDateTimeToCheck); + return true; + } catch (Exception e) { + throw new CustomError(HttpStatus.BAD_REQUEST.value(), new Gson().toJson( + new CustomError(HttpStatus.BAD_REQUEST.value(), + String.format("Date %s is an invalid ISO-8601 UTC date time! Should be something like %s", + utcDateTimeToCheck, "2017-10-03T12:18:46+00:00")) + )); + } + } } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index d51a12d..5bca724 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1,13 +1,13 @@ INSERT INTO partners (company_name, ref, locale, expires) -VALUES ('B2boost', 'FYI1', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'), - ('Proximus', 'FYI2', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'), - ('KBC Bank', 'FYI3', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'), - ('AB InBev', 'FYI4', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'), - ('Spotify', 'FYI5', 'sv_SE', TIMESTAMP '2022-05-23 12:18:46'), - ('AMSOM-Habitat', 'FYI6', 'fr_FR', TIMESTAMP '2022-05-23 12:18:46'), - ('Microsoft', 'FYI7', 'en_US', TIMESTAMP '2022-05-23 12:18:46'), - ('Sony', 'FYI8', 'ja_JP', TIMESTAMP '2022-05-23 12:18:46'), - ('Intel', 'FYI9', 'en_US', TIMESTAMP '2022-05-23 12:18:46'), - ('Cisco', 'FYI10', 'en_US', TIMESTAMP '2022-05-23 12:18:46'), - ('Dell', 'FYI11', 'en_US', TIMESTAMP '2022-05-23 12:18:46'), - ('Canonical', 'FYI12', 'en_US', TIMESTAMP '2022-05-23 12:18:46'); +VALUES ('B2boost', 'FYI1', 'en_BE', '2022-11-24T17:46:00+01:00'), + ('Proximus', 'FYI2', 'en_BE', '2022-11-24T17:46:00+01:00'), + ('KBC Bank', 'FYI3', 'en_BE', '2022-11-24T17:46:00+01:00'), + ('AB InBev', 'FYI4', 'en_BE', '2022-11-24T17:46:00+01:00'), + ('Spotify', 'FYI5', 'sv_SE', '2022-11-24T17:46:00+01:00'), + ('AMSOM-Habitat', 'FYI6', 'fr_FR', '2022-11-24T17:46:00+01:00'), + ('Microsoft', 'FYI7', 'en_US', '2022-11-24T17:46:00-04:00'), + ('Sony', 'FYI8', 'ja_JP', '2022-11-24T17:46:00+09:00'), + ('Intel', 'FYI9', 'en_US', '2022-11-24T17:46:00-04:00'), + ('Cisco', 'FYI10', 'en_US', '2022-11-24T17:46:00-04:00'), + ('Dell', 'FYI11', 'en_US', '2022-11-24T17:46:00-04:00'), + ('Canonical', 'FYI12', 'en_US', '2022-11-24T17:46:00-04:00'); diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index b4706b1..38126d7 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -1,8 +1,8 @@ CREATE TABLE partners ( id INT AUTO_INCREMENT PRIMARY KEY, - company_name VARCHAR(250) NOT NULL, - ref VARCHAR(250) UNIQUE NOT NULL, - locale VARCHAR(250) NOT NULL, - expires DATETIME NOT NULL + company_name VARCHAR(250) NOT NULL, + ref VARCHAR(250) UNIQUE NOT NULL, + locale VARCHAR(250) NOT NULL, + expires TIMESTAMP WITH TIME ZONE NOT NULL ); \ No newline at end of file