Skip to content

Commit

Permalink
Delete related records when deleting scripts and schedules.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Feb 23, 2024
1 parent bdc9ecf commit d7958a7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 34 deletions.
41 changes: 27 additions & 14 deletions Server/Components/Scripts/SavedScripts.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,6 +37,9 @@ public partial class SavedScripts : AuthComponentBase
[Inject]
public IModalService ModalService { get; set; } = null!;

[Inject]
public required ILogger<SavedScripts> Logger { get; set; }

private bool CanModifyScript
{
get
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 25 additions & 13 deletions Server/Components/Scripts/ScriptSchedules.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,9 @@ public partial class ScriptSchedules : AuthComponentBase
[Inject]
private IToastService ToastService { get; set; } = null!;

[Inject]
public required ILogger<ScriptSchedules> Logger { get; set; }

private bool CanModifySchedule
{
get
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion Server/Components/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
@using Remotely.Server.Components.TreeView
@using Remotely.Server.Auth
@using Remotely.Shared.Entities
@using Remotely.Server.Models
@using Remotely.Server.Models
@using Remotely.Server.Enums;

Check warning on line 29 in Server/Components/_Imports.razor

View workflow job for this annotation

GitHub Actions / build (Release)

The using directive for 'Remotely.Server.Enums' appeared previously in this namespace
3 changes: 0 additions & 3 deletions Server/Services/DataCleanupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ public class DataCleanupService : BackgroundService, IDisposable
private readonly ILogger<DataCleanupService> _logger;
private readonly IServiceScopeFactory _scopeFactory;
private readonly ISystemTime _systemTime;
private readonly IDataService _dataService;

public DataCleanupService(
IServiceScopeFactory scopeFactory,
ISystemTime systemTime,
IDataService dataService,
ILogger<DataCleanupService> logger)
{
_scopeFactory = scopeFactory;
_systemTime = systemTime;
_dataService = dataService;
_logger = logger;
}

Expand Down
26 changes: 23 additions & 3 deletions Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d7958a7

Please sign in to comment.