Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ jobs:
build:
env:
BUILD_CONFIG: 'Release'
DOTNET_ROLL_FORWARD: 'Major'
SOLUTION: 'GDSMultiPageFormService.sln'
name: Build and publish package
runs-on: ubuntu-latest
strategy:
matrix:
dotnet-version: ['3.1.x']
dotnet-version: ['6.0.x']
steps:
- uses: actions/checkout@v2
with:
Expand All @@ -35,6 +36,9 @@ jobs:
echo "Minor: ${{ steps.gitversion.outputs.minor }}"
echo "Patch: ${{ steps.gitversion.outputs.patch }}"
echo "NuGetVersionV2: ${{ steps.gitversion.outputs.nuGetVersionV2 }}"

- name: Add Azure artifact
run: dotnet nuget add source 'https://pkgs.dev.azure.com/e-LfH/_packaging/LearningHubFeed/nuget/v3/index.json' --name 'LearningHubFeed' --username 'kevin.whittaker' --password ${{ secrets.AZUREDEVOPSKEY }} --store-password-in-clear-text

- name: Dotnet build
run: dotnet build GDSMultiPageFormService.sln --configuration release
Expand All @@ -45,4 +49,4 @@ jobs:

- name: Push generated package to GitHub registry
if: startsWith(github.ref, 'refs/tags/v')
run: dotnet nuget push ./package/*.nupkg --source https://nuget.pkg.github.com/TechnologyEnhancedLearning/index.json --api-key ${{ secrets.NUGETAPIKEY }}
run: dotnet nuget push ./package/*.nupkg --source https://nuget.pkg.github.com/TechnologyEnhancedLearning/index.json --api-key ${{ secrets.NUGETAPIKEY }}
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

### ASPNETCore ###
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

# Visual Studio 2015 cache/options directory
.vs/
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file added .vs/GDSMultiPageFormService/v17/.futdcache.v2
Binary file not shown.
Binary file added .vs/GDSMultiPageFormService/v17/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\GDSMultiPageFormService.sln",
"PreviewInSolutionExplorer": false
}
20 changes: 20 additions & 0 deletions GDSMultiPageFormService/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Extensions.Configuration;

namespace GDS.MultiPageFormData
{
public static class AppConfig
{
public static bool GetMultiPageFormDataStore()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json"), optional: false)
.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "appsettings.Development.json"), optional: false)
.Build();
try
{
return configuration.GetSection("MultiPageFormService").GetSection("Database").Value.ToLower().Equals("redis");
}
catch { return false; }
}
}
}
2 changes: 2 additions & 0 deletions GDSMultiPageFormService/GDS.MultiPageFormData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>$(VersionPrefix)</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="LearningHub.Nhs.Caching" Version="2.0.0" />
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="2.5.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
Expand Down
121 changes: 92 additions & 29 deletions GDSMultiPageFormService/MultiPageFormService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,39 @@
using GDS.MultiPageFormData.Enums;
using GDS.MultiPageFormData.Models;
using GDS.MultiPageFormData.Services;
using LearningHub.Nhs.Caching;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Newtonsoft.Json;
using System;
using System.Data;

public class MultiPageFormService

public interface IMultiPageFormService
{
void SetMultiPageFormData(object formData, MultiPageFormDataFeature feature, ITempDataDictionary tempData);


T GetMultiPageFormData<T>(MultiPageFormDataFeature feature, ITempDataDictionary tempData);


void ClearMultiPageFormData(MultiPageFormDataFeature feature, ITempDataDictionary tempData);


bool FormDataExistsForGuidAndFeature(MultiPageFormDataFeature feature, Guid tempDataGuid);

}

public class MultiPageFormService : IMultiPageFormService
{
private static IDbConnection _DbConnection;
private readonly ICacheService cacheService;

public MultiPageFormService(ICacheService cacheService)
{
this.cacheService = cacheService;
}

private static bool useRedisCache = AppConfig.GetMultiPageFormDataStore();

public static void InitConnection(IDbConnection Connection)
{
Expand Down Expand Up @@ -45,12 +70,26 @@ public static void InitConnection(IDbConnection Connection)
}
}

public static void SetMultiPageFormData(object formData, MultiPageFormDataFeature feature, ITempDataDictionary tempData )
public async void SetMultiPageFormData(object formData, MultiPageFormDataFeature feature, ITempDataDictionary tempData)
{
if (_DbConnection != null)
var json = JsonConvert.SerializeObject(formData);
if (useRedisCache)
{
var tempDataGuid = tempData[feature.TempDataKey] == null ? Guid.NewGuid() : (Guid)tempData[feature.TempDataKey];
var multiPageFormData = new MultiPageFormData
{
TempDataGuid = tempDataGuid,
Json = json,
Feature = feature.Name,
CreatedDate = ClockService.UtcNow,
};
string MultiPageFormCacheKey = GetMultiPageFormCacheKey(multiPageFormData.TempDataGuid, multiPageFormData.Feature);
await this.cacheService.SetAsync(MultiPageFormCacheKey, JsonConvert.SerializeObject(multiPageFormData));
tempData[feature.TempDataKey] = tempDataGuid;
return;
}
else if (_DbConnection != null)
{
var json = JsonConvert.SerializeObject(formData);

MultiPageFormDataService multiPageFormDataService = new MultiPageFormDataService(_DbConnection);
if (tempData[feature.TempDataKey] != null)
{
Expand Down Expand Up @@ -82,51 +121,64 @@ public static void SetMultiPageFormData(object formData, MultiPageFormDataFeatur
}
}

public static T GetMultiPageFormData<T>(MultiPageFormDataFeature feature, ITempDataDictionary tempData)
public T GetMultiPageFormData<T>(MultiPageFormDataFeature feature, ITempDataDictionary tempData)
{
if (_DbConnection != null)
if (tempData[feature.TempDataKey] == null)
{
MultiPageFormDataService multiPageFormDataService = new MultiPageFormDataService(_DbConnection);

throw new Exception("Attempted to get data with no Guid identifier");
}
var settings = new JsonSerializerSettings() { ObjectCreationHandling = ObjectCreationHandling.Replace };
var tempDataGuid = (Guid)tempData.Peek(feature.TempDataKey);
if (useRedisCache)
{
string MultiPageFormCacheKey = GetMultiPageFormCacheKey(tempDataGuid, feature.Name);
var existingMultiPageFormData = this.cacheService.GetAsync<MultiPageFormData>(MultiPageFormCacheKey).Result;

if (tempData[feature.TempDataKey] == null)
if (existingMultiPageFormData == null)
{
throw new Exception("Attempted to get data with no Guid identifier");
throw new Exception($"MultiPageFormData not found for {tempDataGuid}");
}

var settings = new JsonSerializerSettings() { ObjectCreationHandling = ObjectCreationHandling.Replace };
var tempDataGuid = (Guid)tempData.Peek(feature.TempDataKey);
tempData[feature.TempDataKey] = tempDataGuid;
return JsonConvert.DeserializeObject<T>(existingMultiPageFormData.Json, settings);


}
else if (_DbConnection != null)
{
MultiPageFormDataService multiPageFormDataService = new MultiPageFormDataService(_DbConnection);

var existingMultiPageFormData =
multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(tempDataGuid, feature.Name);

if (existingMultiPageFormData == null)
{
throw new Exception($"MultiPageFormData not found for {tempDataGuid}");
}

tempData[feature.TempDataKey] = tempDataGuid;

return JsonConvert.DeserializeObject<T>(existingMultiPageFormData.Json, settings);
}
else
{
throw new Exception("Connection object is null or empty");
}

}

public static void ClearMultiPageFormData(MultiPageFormDataFeature feature, ITempDataDictionary tempData)
public void ClearMultiPageFormData(MultiPageFormDataFeature feature, ITempDataDictionary tempData)
{
if (_DbConnection != null)
if (tempData[feature.TempDataKey] == null)
{
throw new Exception("Attempted to clear data with no Guid identifier");
}
var tempDataGuid = (Guid)tempData.Peek(feature.TempDataKey);
if (useRedisCache)
{
string MultiPageFormCacheKey = GetMultiPageFormCacheKey(tempDataGuid, feature.Name);
this.cacheService.RemoveAsync(MultiPageFormCacheKey);
}
else if (_DbConnection != null)
{
MultiPageFormDataService multiPageFormDataService = new MultiPageFormDataService(_DbConnection);

if (tempData[feature.TempDataKey] == null)
{
throw new Exception("Attempted to clear data with no Guid identifier");
}

var tempDataGuid = (Guid)tempData.Peek(feature.TempDataKey);
multiPageFormDataService.DeleteByGuid(tempDataGuid);
tempData.Remove(feature.TempDataKey);
}
Expand All @@ -136,11 +188,17 @@ public static void ClearMultiPageFormData(MultiPageFormDataFeature feature, ITem
}
}

public static bool FormDataExistsForGuidAndFeature(MultiPageFormDataFeature feature, Guid tempDataGuid)
public bool FormDataExistsForGuidAndFeature(MultiPageFormDataFeature feature, Guid tempDataGuid)
{
try
{
if (_DbConnection != null)
if (useRedisCache)
{
string MultiPageFormCacheKey = GetMultiPageFormCacheKey(tempDataGuid, feature.Name);
var existingMultiPageFormData = this.cacheService.GetAsync<MultiPageFormData>(MultiPageFormCacheKey).Result;
return existingMultiPageFormData != null;
}
else if (_DbConnection != null)
{
MultiPageFormDataService multiPageFormDataService = new MultiPageFormDataService(_DbConnection);

Expand All @@ -167,5 +225,10 @@ public static bool FormDataExistsForGuidAndFeature(MultiPageFormDataFeature feat

}

private string GetMultiPageFormCacheKey(Guid guid, string? featureName)
{
return string.IsNullOrWhiteSpace(featureName) ? $"{guid}:MultiPageFormData" : $"{guid}-{featureName}:MultiPageFormData";
}

}
}
}
Loading