From 193f78b38bb0c11e0e16c8e403f67cecf2883d59 Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Tue, 9 Mar 2021 13:36:36 +0100 Subject: [PATCH] If not specified, the Name, Email, and Comment fields should always be NULL instead of an empty string + updated UpdateEntry method to set values as null if empty or whitespace + added migration to fix existing values in the database --- .../Components/MigrationComponent.cs | 3 ++- .../FixEmptyStringValuesMigration.cs | 27 +++++++++++++++++++ .../Services/FeedbackService.cs | 7 ++++- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/Skybrud.Umbraco.Feedback/Migrations/FixEmptyStringValuesMigration.cs diff --git a/src/Skybrud.Umbraco.Feedback/Components/MigrationComponent.cs b/src/Skybrud.Umbraco.Feedback/Components/MigrationComponent.cs index 47a7fa5..28889e8 100644 --- a/src/Skybrud.Umbraco.Feedback/Components/MigrationComponent.cs +++ b/src/Skybrud.Umbraco.Feedback/Components/MigrationComponent.cs @@ -27,7 +27,8 @@ public void Initialize() { var plan = new MigrationPlan("Skybrud.Umbraco.Feedback"); plan.From(string.Empty) - .To("3.0.0-alpha001"); + .To("3.0.0-alpha001") + .To("3.0.0-alpha004"); var upgrader = new Upgrader(plan); diff --git a/src/Skybrud.Umbraco.Feedback/Migrations/FixEmptyStringValuesMigration.cs b/src/Skybrud.Umbraco.Feedback/Migrations/FixEmptyStringValuesMigration.cs new file mode 100644 index 0000000..47552f0 --- /dev/null +++ b/src/Skybrud.Umbraco.Feedback/Migrations/FixEmptyStringValuesMigration.cs @@ -0,0 +1,27 @@ +using Skybrud.Umbraco.Feedback.Constants; +using Umbraco.Core.Logging; +using Umbraco.Core.Migrations; + +namespace Skybrud.Umbraco.Feedback.Migrations { + + public class FixEmptyStringValuesMigration : MigrationBase { + + public FixEmptyStringValuesMigration(IMigrationContext context) : base(context) { } + + public override void Migrate() { + + if (!TableExists(FeedbackConstants.TableName)) return; + + int affected1 = Context.Database.Execute("UPDATE [SkybrudFeedback] SET [Name] = null WHERE [Name] LIKE '';"); + int affected2 = Context.Database.Execute("UPDATE [SkybrudFeedback] SET [Email] = null WHERE [Email] LIKE '';"); + int affected3 = Context.Database.Execute("UPDATE [SkybrudFeedback] SET [Comment] = null WHERE [Comment] LIKE '';"); + + Logger.Info($"Fixed empty string values for name column in {affected1} rows."); + Logger.Info($"Fixed empty string values for email column in {affected2} rows."); + Logger.Info($"Fixed empty string values for comment column in {affected3} rows."); + + } + + } + +} \ No newline at end of file diff --git a/src/Skybrud.Umbraco.Feedback/Services/FeedbackService.cs b/src/Skybrud.Umbraco.Feedback/Services/FeedbackService.cs index 7b55307..a1e1137 100644 --- a/src/Skybrud.Umbraco.Feedback/Services/FeedbackService.cs +++ b/src/Skybrud.Umbraco.Feedback/Services/FeedbackService.cs @@ -133,7 +133,7 @@ public FeedbackEntry GetEntryByKey(Guid key) { public void Archive(FeedbackEntry entry) { if (entry == null) throw new ArgumentNullException(nameof(entry)); - + entry.IsArchived = true; _databaseService.Update(entry._entry); @@ -313,6 +313,11 @@ public AddRatingResult AddRating(FeedbackSiteSettings site, IPublishedContent pa } public UpdateEntryResult UpdateEntry(FeedbackEntry entry) { + + // Ensure the string values are NULL (opposed to empty or white space) + entry.Name = FeedbackUtils.TrimToNull(entry.Name); + entry.Email = FeedbackUtils.TrimToNull(entry.Email); + entry.Comment = FeedbackUtils.TrimToNull(entry.Comment); // Attempt to add the entry to the database try {