diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-12-26 17:32:18 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-12-26 17:32:18 -0600 |
| commit | 9adc7ddbe387627ae7195927aa09340f3fec5e87 (patch) | |
| tree | 8cde8fd96ca1dbe67af6b9613163f3da0c0b11d9 /src | |
| parent | e2789eb6f131d859f501017dd2877b4f0ae4d328 (diff) | |
| download | slskd-9adc7ddbe387627ae7195927aa09340f3fec5e87.tar.gz | |
add remove overload accepting an expression
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 50 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 48 |
2 files changed, 59 insertions, 39 deletions
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 /// </summary> /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> /// <param name="id">The unique identifier of the download.</param> - void Remove(Guid id); + /// <returns>A value indicating whether the record was removed.</returns> + bool Remove(Guid id); + + /// <summary> + /// Removes all downloads matching the specified <paramref name="expression"/>. + /// </summary> + /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> + /// <param name="expression">The expression used to match downloads.</param> + /// <returns>The number of records removed.</returns> + int Remove(Expression<Func<Transfer, bool>> expression); /// <summary> /// Cancels the download matching the specified <paramref name="id"/>, 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 /// </summary> /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> /// <param name="id">The unique identifier of the download.</param> - public void Remove(Guid id) + /// <returns>A value indicating whether the record was removed.</returns> + public bool Remove(Guid id) + { + return Remove(t => t.Id == id) > 0; + } + + /// <summary> + /// Removes all downloads matching the specified <paramref name="expression"/>. + /// </summary> + /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> + /// <param name="expression">The expression used to match downloads.</param> + /// <returns>The number of records removed.</returns> + public int Remove(Expression<Func<Transfer, bool>> 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 /// </summary> /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> /// <param name="id">The unique identifier of the upload.</param> - void Remove(Guid id); + /// <returns>A value indicating whether the record was removed.</returns> + bool Remove(Guid id); + + /// <summary> + /// Removes all uploads matching the specified <paramref name="expression"/>. + /// </summary> + /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> + /// <param name="expression">The expression used to match uploads.</param> + /// <returns>The number of records removed.</returns> + int Remove(Expression<Func<Transfer, bool>> expression); /// <summary> /// Cancels the upload matching the specified <paramref name="id"/>, if it is in progress. @@ -823,34 +832,35 @@ namespace slskd.Transfers.Uploads /// </summary> /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> /// <param name="id">The unique identifier of the upload.</param> - public void Remove(Guid id) + /// <returns>A value indicating whether the record was removed.</returns> + public bool Remove(Guid id) + { + return Remove(t => t.Id == id) > 0; + } + + /// <summary> + /// Removes all uploads matching the specified <paramref name="expression"/>. + /// </summary> + /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> + /// <param name="expression">The expression used to match uploads.</param> + /// <returns>The number of records removed.</returns> + public int Remove(Expression<Func<Transfer, bool>> 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; } } |
