From 21ee5137b07c527b60a423dad2574596216e7dc2 Mon Sep 17 00:00:00 2001 From: MarcoE Date: Wed, 4 Mar 2026 12:08:57 +0100 Subject: [PATCH] =?UTF-8?q?Creato=20metodo=20per=20la=20rimozione=20delle?= =?UTF-8?q?=20ispezioni=20pi=C3=B9=20vecchie=20di=2060=20giorni?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SteUp.Maui/Core/Services/FileManager.cs | 48 ++++++++++++++++++- .../Modal/ModalFormScheda.razor | 3 +- SteUp.Shared/Core/Data/SteupDataService.cs | 17 +++++++ .../Core/Interface/System/IFileManager.cs | 1 + 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/SteUp.Maui/Core/Services/FileManager.cs b/SteUp.Maui/Core/Services/FileManager.cs index 26ef2e6..fbdea41 100644 --- a/SteUp.Maui/Core/Services/FileManager.cs +++ b/SteUp.Maui/Core/Services/FileManager.cs @@ -1,4 +1,5 @@ using System.IO.Compression; +using Microsoft.Extensions.Logging; using SteUp.Data.LocalDb; using SteUp.Shared.Core.Dto; using SteUp.Shared.Core.Entities; @@ -7,7 +8,9 @@ using SteUp.Shared.Core.Interface.System; namespace SteUp.Maui.Core.Services; -public class FileManager(IDbPathProvider dbPathProvider) : IFileManager +public class FileManager( + IDbPathProvider dbPathProvider, + ILogger logger) : IFileManager { private static string AttachedRoot => Path.Combine(FileSystem.CacheDirectory, "attached"); @@ -212,6 +215,49 @@ public class FileManager(IDbPathProvider dbPathProvider) : IFileManager return removed; } + + public bool RemoveInspection(Ispezione ispezione, bool removeAlsoFromFinal, bool removeAlsoFromToUpload) + { + ArgumentNullException.ThrowIfNull(ispezione); + + var removedAnything = false; + + if (removeAlsoFromToUpload) + TryDeleteDirectory(GetInspectionToUploadDir(ispezione)); + + if (removeAlsoFromFinal) + TryDeleteDirectory(GetInspectionFinalDir(ispezione)); + + try + { + CleanupDirectoriesIfEmpty(ispezione); + } + catch(Exception e) + { + logger.LogError(e, e.Message); + SentrySdk.CaptureException(e); + } + + return removedAnything; + + void TryDeleteDirectory(string? dir) + { + if (string.IsNullOrWhiteSpace(dir)) return; + + try + { + if (!Directory.Exists(dir)) return; + + Directory.Delete(dir, recursive: true); + removedAnything = true; + } + catch(Exception e) + { + logger.LogError(e, e.Message); + SentrySdk.CaptureException(e); + } + } + } private void CleanupDirectoriesIfEmpty(Ispezione ispezione) { diff --git a/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor b/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor index 6654ac4..c25d495 100644 --- a/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor +++ b/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor @@ -1,5 +1,4 @@ -@using System.Data.Common -@using Microsoft.Extensions.Logging +@using Microsoft.Extensions.Logging @using SteUp.Shared.Components.Layout @using SteUp.Shared.Components.Layout.Overlay @using SteUp.Shared.Components.Layout.Spinner diff --git a/SteUp.Shared/Core/Data/SteupDataService.cs b/SteUp.Shared/Core/Data/SteupDataService.cs index 7c77b50..66d7cc0 100644 --- a/SteUp.Shared/Core/Data/SteupDataService.cs +++ b/SteUp.Shared/Core/Data/SteupDataService.cs @@ -17,6 +17,7 @@ public class SteupDataService( IDeviceService deviceService, IGenericSystemService genericSystemService, IIspezioniService ispezioniService, + IFileManager fileManager, IDbInitializer dbInitializer) : ISteupDataService { public async Task Init() @@ -24,6 +25,7 @@ public class SteupDataService( await dbInitializer.InitializeAsync(); await LoadDataAsync(); await CheckAndUpdateStatus(); + await CleanOldClosedInspection(); RegisterAppVersion(); } @@ -34,6 +36,21 @@ public class SteupDataService( ); } + private async Task CleanOldClosedInspection() + { + var ispezioni = (await ispezioniService.GetAllIspezioniWithSchedeAsync()) + .Where(x => + x.Stato == StatusEnum.Completata && + x.Data < DateTime.Now.AddDays(-60) + ).ToList(); + + foreach (var ispezione in ispezioni) + { + fileManager.RemoveInspection(ispezione); + await ispezioniService.DeleteIspezioneAsync(ispezione.CodMdep, ispezione.Data, ispezione.Rilevatore); + } + } + public async Task CheckAndUpdateStatus() { var ispezioni = await ispezioniService.GetAllIspezioniWithSchedeAsync(); diff --git a/SteUp.Shared/Core/Interface/System/IFileManager.cs b/SteUp.Shared/Core/Interface/System/IFileManager.cs index 8ae8331..d7f95d6 100644 --- a/SteUp.Shared/Core/Interface/System/IFileManager.cs +++ b/SteUp.Shared/Core/Interface/System/IFileManager.cs @@ -12,6 +12,7 @@ public interface IFileManager string GetFileToUploadDir(Ispezione ispezione, string fileName); + bool RemoveInspection(Ispezione ispezione, bool removeAlsoFromFinal = true, bool removeAlsoFromToUpload = true); bool RemoveInspectionFile(Ispezione ispezione, string fileName, bool removeAlsoFromFinal = true, bool removeAlsoFromToUpload = true);