//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ 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.Downloads;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Serilog;
///
/// Manages transfer batches.
///
public interface IBatchService
{
///
/// Creates a new batch record.
///
/// The batch to create.
/// The created batch.
Task CreateAsync(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);
}
///
/// 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 async Task CreateAsync(Batch batch)
{
if (batch == default)
{
throw new ArgumentNullException(nameof(batch));
}
if (batch.Id == Guid.Empty)
{
throw new ArgumentOutOfRangeException(nameof(batch.Id), message: $"Batch ID may not be an empty uuid ({Guid.Empty})");
}
try
{
using var context = ContextFactory.CreateDbContext();
context.Batches.Add(batch);
await context.SaveChangesAsync();
return batch;
}
catch (DbUpdateException ex) when (ex.InnerException is SqliteException { SqliteErrorCode: 19 })
{
throw new DuplicateException($"A Batch with ID {batch.Id} already exists.");
}
catch (Exception ex)
{
Log.Error("Failed to create Batch: {Message}", ex.Message);
throw;
}
}
///
/// 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 async Task FindAsync(Expression> expression)
{
if (expression == default)
{
throw new ArgumentException("An expression must be supplied.", nameof(expression));
}
using var context = ContextFactory.CreateDbContext();
return await context.Batches
.AsNoTracking()
.Include(b => b.Transfers)
.Where(expression)
.SingleOrDefaultAsync();
}
}