From 997a3020c795541e8c347ba7c8142c13c415e4c0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 5 Jul 2026 16:24:38 -0500 Subject: add transfers.query --- src/slskd/Transfers/TransferService.cs | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'src') diff --git a/src/slskd/Transfers/TransferService.cs b/src/slskd/Transfers/TransferService.cs index 6dd852a0..156b63ec 100644 --- a/src/slskd/Transfers/TransferService.cs +++ b/src/slskd/Transfers/TransferService.cs @@ -32,6 +32,12 @@ namespace slskd.Transfers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore; +using Serilog; using slskd.Transfers.Downloads; using slskd.Transfers.Uploads; @@ -44,9 +50,12 @@ public class TransferService /// Initializes a new instance of the class. /// public TransferService( + IDbContextFactory contextFactory, IUploadService uploadService = null, IDownloadService downloadService = null) { + ContextFactory = contextFactory; + Uploads = uploadService; Downloads = downloadService; } @@ -60,4 +69,87 @@ public class TransferService /// Gets the download service. /// public virtual IDownloadService Downloads { get; init; } + + private IDbContextFactory ContextFactory { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); + + /// + /// Finds a single transfer matching the specified . + /// + /// + /// Use this only to avoid needing to call this method on both the Upload and Download services. If the expression + /// contains a direction, this method is being used in error. + /// + /// The expression to use to match transfers. + /// The found transfer, or default if not found. + public virtual Transfer Find(Expression> expression) + { + ArgumentNullException.ThrowIfNull(expression); + + try + { + using var context = ContextFactory.CreateDbContext(); + + return context.Transfers + .AsNoTracking() + .Where(expression) + .SingleOrDefault(); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to list transfers: {Message}", ex.Message); + throw; + } + } + + /// + /// Returns a list of all transfers matching the optional . + /// + /// + /// Use this only to avoid needing to call this method on both the Upload and Download services. If the expression + /// contains a direction, this method is being used in error. + /// + /// An optional expression used to match transfers. + /// A value indicating whether to include transfers that have been removed previously. + /// The list of transfers matching the specified expression, or all transfers if no expression is specified. + public virtual List List(Expression> expression, bool includeRemoved) + { + expression ??= t => true; + + try + { + using var context = ContextFactory.CreateDbContext(); + + return context.Transfers + .AsNoTracking() + .Where(t => !t.Removed || includeRemoved) + .Where(expression) + .ToList(); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to list uploads: {Message}", ex.Message); + throw; + } + } + + /// + /// Query the Transfers database arbitrarily. Only use if there's no better way. + /// + /// The result Type. + /// The query function. + /// The result. + public virtual TResult Query(Func, TResult> query) + { + try + { + using var context = ContextFactory.CreateDbContext(); + return query(context.Transfers.AsNoTracking()); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to query transfers: {Message}", ex.Message); + throw; + } + } } \ No newline at end of file -- cgit v1.2.3