mirror of
https://github.com/immense/Remotely.git
synced 2025-10-26 11:27:15 +00:00
Delete related records when deleting scripts and schedules.
This commit is contained in:
parent
bdc9ecfb8f
commit
d7958a7be0
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components;
|
||||
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<SavedScripts> Logger { get; set; }
|
||||
|
||||
private bool CanModifyScript
|
||||
{
|
||||
get
|
||||
@ -106,24 +110,33 @@ public partial class SavedScripts : AuthComponentBase
|
||||
|
||||
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()
|
||||
{
|
||||
Name = string.Empty
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error while deleting script.");
|
||||
ToastService.ShowToast2("Failed to delete script", ToastType.Error);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
Name = string.Empty
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ScriptSelected(ScriptTreeNode viewModel)
|
||||
|
||||
@ -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<ScriptSchedules> Logger { get; set; }
|
||||
|
||||
private bool CanModifySchedule
|
||||
{
|
||||
get
|
||||
@ -102,21 +106,29 @@ public partial class ScriptSchedules : AuthComponentBase
|
||||
|
||||
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 @@ public partial class ScriptSchedules : AuthComponentBase
|
||||
{
|
||||
if (schedule?.Id == _selectedSchedule?.Id)
|
||||
{
|
||||
return "bg-primary text-white";
|
||||
return "table-primary";
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -908,19 +908,39 @@ public class DataService : IDataService
|
||||
{
|
||||
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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user