From 920d11fffbecf84d737781037992fdfc06ffa1cf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 26 Apr 2026 19:23:46 -0500 Subject: rough in batch service --- src/slskd/Transfers/Batches/BatchService.cs | 179 ++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 src/slskd/Transfers/Batches/BatchService.cs (limited to 'src') diff --git a/src/slskd/Transfers/Batches/BatchService.cs b/src/slskd/Transfers/Batches/BatchService.cs new file mode 100644 index 00000000..515201c9 --- /dev/null +++ b/src/slskd/Transfers/Batches/BatchService.cs @@ -0,0 +1,179 @@ +// +// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ +// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ +// █__ --█ █__ --█ ◄█ - █ +// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ +// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ +// │ Copyright (c) JP Dillingham. +// │ +// │ This program is free software: you can redistribute it and/or modify +// │ it under the terms of the GNU Affero General Public License as published +// │ by the Free Software Foundation, version 3. +// │ +// │ This program is distributed in the hope that it will be useful, +// │ but WITHOUT ANY WARRANTY; without even the implied warranty of +// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// │ GNU Affero General Public License for more details. +// │ +// │ You should have received a copy of the GNU Affero General Public License +// │ along with this program. If not, see https://www.gnu.org/licenses/. +// │ +// │ This program is distributed with Additional Terms pursuant to Section 7 +// │ of the AGPLv3. See the LICENSE file in the root directory of this +// │ project for the complete terms and conditions. +// │ +// │ https://slskd.org +// │ +// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ +// │ SPDX-FileCopyrightText: JP Dillingham +// │ SPDX-License-Identifier: AGPL-3.0-only +// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ +// + +namespace slskd.Transfers +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Linq.Expressions; + using System.Threading.Tasks; + using Microsoft.EntityFrameworkCore; + using Serilog; + + /// + /// Manages transfer batches. + /// + public interface IBatchService + { + /// + /// Creates a new batch record. + /// + /// The batch to create. + /// The created batch. + Batch Create(Batch batch); + + /// + /// Finds a single batch matching the specified . + /// + /// The expression to use to match batches. + /// The found batch, or default if not found. + /// Thrown when an expression is not supplied. + Task FindAsync(Expression> expression); + + /// + /// Returns a list of all batches matching the optional . + /// + /// An optional expression used to match batches. + /// The list of batches matching the specified expression, or all batches if no expression is specified. + Task> ListAsync(Expression> expression = null); + + /// + /// Marks all transfers associated with the specified batch as removed. + /// + /// The unique identifier of the batch. + /// Thrown when the batch does not exist. + void Remove(Guid id); + } + + /// + /// Manages transfer batches. + /// + public class BatchService : IBatchService + { + /// + /// Initializes a new instance of the class. + /// + /// The database context factory to use. + public BatchService(IDbContextFactory contextFactory) + { + ContextFactory = contextFactory; + } + + private IDbContextFactory ContextFactory { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); + + /// + /// Creates a new batch record. + /// + /// The batch to create. + /// The created batch. + public Batch Create(Batch batch) + { + if (batch == default) + { + throw new ArgumentNullException(nameof(batch)); + } + + using var context = ContextFactory.CreateDbContext(); + context.Batches.Add(batch); + context.SaveChanges(); + + Log.Debug("Created batch {Id}", batch.Id); + + return batch; + } + + /// + /// Finds a single batch matching the specified . + /// + /// The expression to use to match batches. + /// The found batch, or default if not found. + /// Thrown when an expression is not supplied. + public Task FindAsync(Expression> expression) + { + if (expression == default) + { + throw new ArgumentException("An expression must be supplied.", nameof(expression)); + } + + using var context = ContextFactory.CreateDbContext(); + + return context.Batches + .AsNoTracking() + .Where(expression) + .Include(b => b.Transfers) + .FirstOrDefaultAsync(); + } + + /// + /// Returns a list of all batches matching the optional . + /// + /// An optional expression used to match batches. + /// The list of batches matching the specified expression, or all batches if no expression is specified. + public Task> ListAsync(Expression> expression = null) + { + expression ??= b => true; + + using var context = ContextFactory.CreateDbContext(); + + return context.Batches + .AsNoTracking() + .Where(expression) + .Include(b => b.Transfers) + .ToListAsync(); + } + + /// + /// Marks all transfers associated with the specified batch as removed. + /// + /// The unique identifier of the batch. + /// Thrown when the batch does not exist. + public void Remove(Guid id) + { + using var context = ContextFactory.CreateDbContext(); + + var exists = context.Batches.Any(b => b.Id == id); + + if (!exists) + { + throw new NotFoundException($"No batch with id {id}"); + } + + context.Transfers + .Where(t => t.BatchId == id) + .ExecuteUpdate(s => s.SetProperty(t => t.Removed, true)); + + Log.Debug("Marked transfers for batch {Id} as removed", id); + } + } +} -- cgit v1.2.3