From d7958a7be07b9693c5f99b77b7568914dfec0cf7 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Fri, 23 Feb 2024 10:17:37 -0800 Subject: [PATCH] Delete related records when deleting scripts and schedules. --- .../Components/Scripts/SavedScripts.razor.cs | 41 ++++++++++++------- .../Scripts/ScriptSchedules.razor.cs | 38 +++++++++++------ Server/Components/_Imports.razor | 3 +- Server/Services/DataCleanupService.cs | 3 -- Server/Services/DataService.cs | 26 ++++++++++-- 5 files changed, 77 insertions(+), 34 deletions(-) diff --git a/Server/Components/Scripts/SavedScripts.razor.cs b/Server/Components/Scripts/SavedScripts.razor.cs index b1a50e6ae..a2ae1eb1a 100644 --- a/Server/Components/Scripts/SavedScripts.razor.cs +++ b/Server/Components/Scripts/SavedScripts.razor.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components.Forms; using Microsoft.CodeAnalysis.Scripting; using Remotely.Server.Components.Pages; +using Remotely.Server.Enums; using Remotely.Server.Services; using Remotely.Shared.Entities; using System; @@ -36,6 +37,9 @@ public partial class SavedScripts : AuthComponentBase [Inject] public IModalService ModalService { get; set; } = null!; + [Inject] + public required ILogger Logger { get; set; } + private bool CanModifyScript { get @@ -106,24 +110,33 @@ private void CreateNew() private async Task DeleteSelectedScript() { - if (!CanDeleteScript) + try { - ToastService.ShowToast("You can't delete other people's scripts.", classString: "bg-warning"); - return; - } + if (!CanDeleteScript) + { + ToastService.ShowToast("You can't delete other people's scripts.", classString: "bg-warning"); + return; + } - var result = await JsInterop.Confirm($"Are you sure you want to delete the script {_selectedScript.Name}?"); - if (result) - { - await DataService.DeleteSavedScript(_selectedScript.Id); - ToastService.ShowToast("Script deleted."); - _alertMessage = "Script deleted."; - await ParentPage.RefreshScripts(); - _selectedScript = new() + var result = await JsInterop.Confirm($"Are you sure you want to delete the script {_selectedScript.Name}?"); + if (result) { - Name = string.Empty - }; + await DataService.DeleteSavedScript(_selectedScript.Id); + ToastService.ShowToast("Script deleted."); + _alertMessage = "Script deleted."; + await ParentPage.RefreshScripts(); + _selectedScript = new() + { + Name = string.Empty + }; + } } + catch (Exception ex) + { + Logger.LogError(ex, "Error while deleting script."); + ToastService.ShowToast2("Failed to delete script", ToastType.Error); + } + } private async Task ScriptSelected(ScriptTreeNode viewModel) diff --git a/Server/Components/Scripts/ScriptSchedules.razor.cs b/Server/Components/Scripts/ScriptSchedules.razor.cs index 13e38e66f..068e9bf09 100644 --- a/Server/Components/Scripts/ScriptSchedules.razor.cs +++ b/Server/Components/Scripts/ScriptSchedules.razor.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; using Remotely.Server.Components.Pages; +using Remotely.Server.Enums; using Remotely.Server.Services; using Remotely.Shared.Entities; using Remotely.Shared.Utilities; @@ -48,6 +49,9 @@ public partial class ScriptSchedules : AuthComponentBase [Inject] private IToastService ToastService { get; set; } = null!; + [Inject] + public required ILogger Logger { get; set; } + private bool CanModifySchedule { get @@ -102,21 +106,29 @@ private void CreateNew() private async Task DeleteSelectedSchedule() { - if (User?.Id != _selectedSchedule.CreatorId) + try { - ToastService.ShowToast("You can't delete other people's script schedules.", classString: "bg-warning"); - return; - } + if (User?.Id != _selectedSchedule.CreatorId) + { + ToastService.ShowToast("You can't delete other people's script schedules.", classString: "bg-warning"); + return; + } - var result = await JsInterop.Confirm($"Are you sure you want to delete the schedule {_selectedSchedule.Name}?"); - if (result) + var result = await JsInterop.Confirm($"Are you sure you want to delete the schedule {_selectedSchedule.Name}?"); + if (result) + { + await DataService.DeleteScriptSchedule(_selectedSchedule.Id); + ToastService.ShowToast("Schedule deleted."); + _alertMessage = "Schedule deleted."; + CreateNew(); + await ParentPage.RefreshScripts(); + await RefreshSchedules(); + } + } + catch (Exception ex) { - await DataService.DeleteScriptSchedule(_selectedSchedule.Id); - ToastService.ShowToast("Schedule deleted."); - _alertMessage = "Schedule deleted."; - CreateNew(); - await ParentPage.RefreshScripts(); - await RefreshSchedules(); + Logger.LogError(ex, "Error while deleting script schedule."); + ToastService.ShowToast2("Failed to delete schedule", ToastType.Error); } } @@ -213,7 +225,7 @@ private string GetTableRowClass(ScriptSchedule schedule) { if (schedule?.Id == _selectedSchedule?.Id) { - return "bg-primary text-white"; + return "table-primary"; } return string.Empty; } diff --git a/Server/Components/_Imports.razor b/Server/Components/_Imports.razor index bbc5eac2e..a3afb55f4 100644 --- a/Server/Components/_Imports.razor +++ b/Server/Components/_Imports.razor @@ -25,4 +25,5 @@ @using Remotely.Server.Components.TreeView @using Remotely.Server.Auth @using Remotely.Shared.Entities -@using Remotely.Server.Models \ No newline at end of file +@using Remotely.Server.Models +@using Remotely.Server.Enums; \ No newline at end of file diff --git a/Server/Services/DataCleanupService.cs b/Server/Services/DataCleanupService.cs index 47a5bc41b..111cd7c20 100644 --- a/Server/Services/DataCleanupService.cs +++ b/Server/Services/DataCleanupService.cs @@ -19,17 +19,14 @@ public class DataCleanupService : BackgroundService, IDisposable private readonly ILogger _logger; private readonly IServiceScopeFactory _scopeFactory; private readonly ISystemTime _systemTime; - private readonly IDataService _dataService; public DataCleanupService( IServiceScopeFactory scopeFactory, ISystemTime systemTime, - IDataService dataService, ILogger logger) { _scopeFactory = scopeFactory; _systemTime = systemTime; - _dataService = dataService; _logger = logger; } diff --git a/Server/Services/DataService.cs b/Server/Services/DataService.cs index 9afc921a5..65c4d98b1 100644 --- a/Server/Services/DataService.cs +++ b/Server/Services/DataService.cs @@ -908,19 +908,39 @@ public async Task DeleteSavedScript(Guid scriptId) { using var dbContext = _appDbFactory.GetContext(); - var script = dbContext.SavedScripts.Find(scriptId); + var schedules = await dbContext.ScriptSchedules + .Where(x => x.SavedScriptId == scriptId) + .ToListAsync(); + + if (schedules.Count > 0) + { + dbContext.ScriptSchedules.RemoveRange(schedules); + } + + var script = await dbContext.SavedScripts + .Include(x => x.ScriptResults) + .Include(x => x.ScriptRuns) + .FirstOrDefaultAsync(x => x.Id == scriptId); + if (script is not null) { dbContext.SavedScripts.Remove(script); - await dbContext.SaveChangesAsync(); } + + await dbContext.SaveChangesAsync(); } public async Task DeleteScriptSchedule(int scriptScheduleId) { using var dbContext = _appDbFactory.GetContext(); - var schedule = dbContext.ScriptSchedules.Find(scriptScheduleId); + var schedule = await dbContext.ScriptSchedules + .Include(x => x.ScriptRuns) + .ThenInclude(x => x.Results) + .Include(x => x.Devices) + .Include(x => x.DeviceGroups) + .FirstOrDefaultAsync(x => x.Id == scriptScheduleId); + if (schedule is not null) { dbContext.ScriptSchedules.Remove(schedule);