diff --git a/src/Spd.Manager.Licence/PersonalLicenceAppContract.cs b/src/Spd.Manager.Licence/PersonalLicenceAppContract.cs index 7ad493dc9..3823499ae 100644 --- a/src/Spd.Manager.Licence/PersonalLicenceAppContract.cs +++ b/src/Spd.Manager.Licence/PersonalLicenceAppContract.cs @@ -185,7 +185,7 @@ public record WorkerLicenceCreateResponse public record LicenceAppDocumentsCache { - public List LicAppFileInfos { get; set; } = new List(); + public List Items { get; set; } = new List(); } public record LicAppFileInfo { diff --git a/src/Spd.Manager.Licence/PersonalLicenceAppManager.cs b/src/Spd.Manager.Licence/PersonalLicenceAppManager.cs index ef4395ac9..85053737e 100644 --- a/src/Spd.Manager.Licence/PersonalLicenceAppManager.cs +++ b/src/Spd.Manager.Licence/PersonalLicenceAppManager.cs @@ -1,4 +1,4 @@ -using AutoMapper; +using AutoMapper; using MediatR; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Logging; @@ -166,7 +166,7 @@ public async Task Handle(AnonymousWorkerLicenceA SaveLicenceApplicationCmd saveCmd = _mapper.Map(request); var response = await _licenceAppRepository.SaveLicenceApplicationAsync(saveCmd, ct); - foreach (LicAppFileInfo licAppFile in appDocCache.LicAppFileInfos) + foreach (LicAppFileInfo licAppFile in appDocCache.Items) { DocumentTypeEnum? docType1 = GetDocumentType1Enum(licAppFile.LicenceDocumentTypeCode); DocumentTypeEnum? docType2 = GetDocumentType2Enum(licAppFile.LicenceDocumentTypeCode); diff --git a/src/Spd.Presentation.Licensing/Controllers/WorkerLicensingController.cs b/src/Spd.Presentation.Licensing/Controllers/WorkerLicensingController.cs index 78c9be7cf..fdea7d114 100644 --- a/src/Spd.Presentation.Licensing/Controllers/WorkerLicensingController.cs +++ b/src/Spd.Presentation.Licensing/Controllers/WorkerLicensingController.cs @@ -36,6 +36,7 @@ public class WorkerLicensingController : SpdControllerBase private readonly IMapper _mapper; private readonly IRecaptchaVerificationService _recaptchaVerificationService; private readonly IDistributedCache _cache; + private static Mutex mutex = new(false); public WorkerLicensingController(ILogger logger, IPrincipal currentUser, @@ -286,8 +287,7 @@ public async Task UploadLicenceAppFilesAnonymous([FromForm][Required] Lice CreateDocumentInCacheCommand command = new CreateDocumentInCacheCommand(fileUploadRequest); var newFileInfos = await _mediator.Send(command); - existingFileInfo.LicAppFileInfos.AddRange(newFileInfos); - await _cache.Set($"{keyCode}", existingFileInfo, TimeSpan.FromMinutes(30)); + await UpdateCache(_cache,keyCode,newFileInfos); return keyCode; } @@ -314,7 +314,32 @@ public async Task SubmitSecurityWorkerLicenceApp AnonymousWorkerLicenceAppSubmitCommand command = new AnonymousWorkerLicenceAppSubmitCommand(jsonRequest, keyCode); return await _mediator.Send(command); + } + private static async Task UpdateCache(IDistributedCache cache, Guid keyCode, IEnumerable newFileInfos) + { + try + { + mutex.WaitOne(); + } + catch (AbandonedMutexException) + { + return; + } + try + { + LicenceAppDocumentsCache existingFileInfo = await cache.Get(keyCode.ToString()); + if (existingFileInfo == null) + { + throw new ApiException(HttpStatusCode.BadRequest, "invalid key code."); + } + existingFileInfo.Items.AddRange(newFileInfos); + await cache.Set($"{keyCode}", existingFileInfo, TimeSpan.FromMinutes(30)); + } + finally + { + mutex.ReleaseMutex(); + } } #endregion }