aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Transfers/TransferService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Transfers/TransferService.cs')
-rw-r--r--src/slskd/Transfers/TransferService.cs92
1 files changed, 92 insertions, 0 deletions
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 <see cref="TransferService"/> class.
/// </summary>
public TransferService(
+ IDbContextFactory<TransfersDbContext> contextFactory,
IUploadService uploadService = null,
IDownloadService downloadService = null)
{
+ ContextFactory = contextFactory;
+
Uploads = uploadService;
Downloads = downloadService;
}
@@ -60,4 +69,87 @@ public class TransferService
/// Gets the download service.
/// </summary>
public virtual IDownloadService Downloads { get; init; }
+
+ private IDbContextFactory<TransfersDbContext> ContextFactory { get; }
+ private ILogger Log { get; } = Serilog.Log.ForContext<TransferService>();
+
+ /// <summary>
+ /// Finds a single transfer matching the specified <paramref name="expression"/>.
+ /// </summary>
+ /// <remarks>
+ /// 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.
+ /// </remarks>
+ /// <param name="expression">The expression to use to match transfers.</param>
+ /// <returns>The found transfer, or default if not found.</returns>
+ public virtual Transfer Find(Expression<Func<Transfer, bool>> 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;
+ }
+ }
+
+ /// <summary>
+ /// Returns a list of all transfers matching the optional <paramref name="expression"/>.
+ /// </summary>
+ /// <remarks>
+ /// 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.
+ /// </remarks>
+ /// <param name="expression">An optional expression used to match transfers.</param>
+ /// <param name="includeRemoved">A value indicating whether to include transfers that have been removed previously.</param>
+ /// <returns>The list of transfers matching the specified expression, or all transfers if no expression is specified.</returns>
+ public virtual List<Transfer> List(Expression<Func<Transfer, bool>> 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;
+ }
+ }
+
+ /// <summary>
+ /// Query the Transfers database arbitrarily. Only use if there's no better way.
+ /// </summary>
+ /// <typeparam name="TResult">The result Type.</typeparam>
+ /// <param name="query">The query function.</param>
+ /// <returns>The result.</returns>
+ public virtual TResult Query<TResult>(Func<IQueryable<Transfer>, 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