From 9adc7ddbe387627ae7195927aa09340f3fec5e87 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 26 Dec 2025 17:32:18 -0600 Subject: add remove overload accepting an expression --- src/slskd/Transfers/Downloads/DownloadService.cs | 50 ++++++++++++++---------- src/slskd/Transfers/Uploads/UploadService.cs | 48 ++++++++++++++--------- 2 files changed, 59 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index fad6712b..289bcb40 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -98,7 +98,16 @@ namespace slskd.Transfers.Downloads /// /// This is a soft delete; the record is retained for historical retrieval. /// The unique identifier of the download. - void Remove(Guid id); + /// A value indicating whether the record was removed. + bool Remove(Guid id); + + /// + /// Removes all downloads matching the specified . + /// + /// This is a soft delete; the record is retained for historical retrieval. + /// The expression used to match downloads. + /// The number of records removed. + int Remove(Expression> expression); /// /// Cancels the download matching the specified , if it is in progress. @@ -318,7 +327,7 @@ namespace slskd.Transfers.Downloads var existingRecordsNotYetRemoved = context.Transfers .Where(t => t.Direction == TransferDirection.Download) .Where(t => t.Username == username) - .Where(t => !t.Removed || TransferStateCategories.Incomplete.Contains((int)t.State)) + .Where(t => !t.Removed || !TransferStateCategories.Completed.Contains((int)t.State)) .AsNoTracking() .ToList(); @@ -711,34 +720,35 @@ namespace slskd.Transfers.Downloads /// /// This is a soft delete; the record is retained for historical retrieval. /// The unique identifier of the download. - public void Remove(Guid id) + /// A value indicating whether the record was removed. + public bool Remove(Guid id) + { + return Remove(t => t.Id == id) > 0; + } + + /// + /// Removes all downloads matching the specified . + /// + /// This is a soft delete; the record is retained for historical retrieval. + /// The expression used to match downloads. + /// The number of records removed. + public int Remove(Expression> expression) { try { using var context = ContextFactory.CreateDbContext(); - var transfer = context.Transfers + var count = context.Transfers .Where(t => t.Direction == TransferDirection.Download) - .Where(t => t.Id == id) - .FirstOrDefault(); - - if (transfer == default) - { - throw new NotFoundException($"No download matching id ${id}"); - } - - if (!transfer.State.HasFlag(TransferStates.Completed)) - { - throw new InvalidOperationException($"Invalid attempt to remove a download before it is complete"); - } - - transfer.Removed = true; + .Where(expression) + .ExecuteUpdate(r => r.SetProperty(c => c.Removed, true)); - context.SaveChanges(); + Log.Debug("Removed {Count} downloads by expression", count); + return count; } catch (Exception ex) { - Log.Error(ex, "Failed to remove download {Id}: {Message}", id, ex.Message); + Log.Error(ex, "Failed to remove downloads by expression: {Message}", ex.Message); throw; } } diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 6657a28f..87899d78 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -116,7 +116,16 @@ namespace slskd.Transfers.Uploads /// /// This is a soft delete; the record is retained for historical retrieval. /// The unique identifier of the upload. - void Remove(Guid id); + /// A value indicating whether the record was removed. + bool Remove(Guid id); + + /// + /// Removes all uploads matching the specified . + /// + /// This is a soft delete; the record is retained for historical retrieval. + /// The expression used to match uploads. + /// The number of records removed. + int Remove(Expression> expression); /// /// Cancels the upload matching the specified , if it is in progress. @@ -823,34 +832,35 @@ namespace slskd.Transfers.Uploads /// /// This is a soft delete; the record is retained for historical retrieval. /// The unique identifier of the upload. - public void Remove(Guid id) + /// A value indicating whether the record was removed. + public bool Remove(Guid id) + { + return Remove(t => t.Id == id) > 0; + } + + /// + /// Removes all uploads matching the specified . + /// + /// This is a soft delete; the record is retained for historical retrieval. + /// The expression used to match uploads. + /// The number of records removed. + public int Remove(Expression> expression) { try { using var context = ContextFactory.CreateDbContext(); - var transfer = context.Transfers + var count = context.Transfers .Where(t => t.Direction == TransferDirection.Upload) - .Where(t => t.Id == id) - .FirstOrDefault(); - - if (transfer == default) - { - throw new NotFoundException($"No upload matching id ${id}"); - } - - if (!transfer.State.HasFlag(TransferStates.Completed)) - { - throw new InvalidOperationException($"Invalid attempt to remove an upload before it is complete"); - } - - transfer.Removed = true; + .Where(expression) + .ExecuteUpdate(r => r.SetProperty(c => c.Removed, true)); - context.SaveChanges(); + Log.Debug("Removed {Count} uploads by expression", count); + return count; } catch (Exception ex) { - Log.Error(ex, "Failed to remove upload {Id}: {Message}", id, ex.Message); + Log.Error(ex, "Failed to remove uploads by expression: {Message}", ex.Message); throw; } } -- cgit v1.2.3