Adds LocalLibrariesController (L1-L7: list/get/create/update/delete/move-path/ path-exists) wrapping the existing local-library MediatR commands, mapping to the shared S0 response DTOs. Per design #202 §A.1/§C5/§C6: - 404 for L4/L5/L6 comes from a controller pre-check (GetLocalLibraryById is None), not the handler -- .Apply/.ToEitherAsync both .Join() a NotFoundError into a plain 422, so relying on the handler would be dead code. This is check-then-act; a delete racing the pre-check falls through to the handler's 422, documented in the controller. - L4/L5 409 via IEntityLocker.IsLibraryLocked(id); L6 resolves the source library from the path id (new ILibraryRepository.GetLibraryIdForPath) before its own lock check. - MoveLocalLibraryPathHandler gains same-MediaKind and different-library validation (finding 3) -- Blazor only filtered these client-side in the move dialog, so an API/MCP client could bypass them. - CreateLocalLibraryHandler/UpdateLocalLibraryHandler gain a shared NewPathsMustExist validation (LocalLibraryHandlerBase) that Directory.Exists- checks only new paths (Id < 1); existing rows stay exempt so an unmounted share doesn't block a rename. L7 (path-exists) is a controller-local IFileSystem check with no command. Tests: controller route/mediator-arg tests incl. 404-pre-check vs fall-through-422 and 409-lock cases; handler tests for the move-path cross-kind/same-library 422s, new-path 422 (missing/mixed), and a lossless round-trip proving local paths are identified by normalized path string, not id. Full solution test suite (Scanner/Core/Architecture/Tests/Infrastructure) green, 0 regressions. Deviations: none from the S1 slice description. Did not touch MediaSourceRepository.cs or any Plex/Jellyfin/Emby file (S2/S3 scope). Did not run the OpenAPI regen scripts (separate gate after S1-S3 merge per design §E).
236 lines
8.5 KiB
C#
236 lines
8.5 KiB
C#
using System.IO.Abstractions;
|
|
using Dapper;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Interfaces.Repositories;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Infrastructure.Data.Repositories;
|
|
|
|
public class LibraryRepository(IFileSystem fileSystem, IDbContextFactory<TvContext> dbContextFactory)
|
|
: ILibraryRepository
|
|
{
|
|
public async Task<LibraryPath> Add(LibraryPath libraryPath)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
await dbContext.LibraryPaths.AddAsync(libraryPath);
|
|
await dbContext.SaveChangesAsync();
|
|
return libraryPath;
|
|
}
|
|
|
|
public async Task<Option<Library>> GetLibrary(int libraryId)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
return await dbContext.Libraries
|
|
.Include(l => l.Paths)
|
|
.ThenInclude(p => p.LibraryFolders)
|
|
.ThenInclude(lf => lf.ImageFolderDuration)
|
|
.OrderBy(l => l.Id)
|
|
.SingleOrDefaultAsync(l => l.Id == libraryId)
|
|
.Map(Optional);
|
|
}
|
|
|
|
public async Task<Option<LocalLibrary>> GetLocal(int libraryId)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
return await dbContext.LocalLibraries
|
|
.OrderBy(l => l.Id)
|
|
.SingleOrDefaultAsync(l => l.Id == libraryId)
|
|
.Map(Optional);
|
|
}
|
|
|
|
public async Task<Option<int>> GetLibraryIdForPath(int libraryPathId)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
int? libraryId = await dbContext.Connection.QuerySingleOrDefaultAsync<int?>(
|
|
"SELECT LibraryId FROM LibraryPath WHERE Id = @LibraryPathId",
|
|
new { LibraryPathId = libraryPathId });
|
|
return Optional(libraryId);
|
|
}
|
|
|
|
public async Task<List<Library>> GetAll()
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
return await dbContext.Libraries
|
|
.AsNoTracking()
|
|
.Include(l => l.MediaSource)
|
|
.Include(l => l.Paths)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<Unit> UpdateLastScan(Library library)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
return await dbContext.Connection.ExecuteAsync(
|
|
"UPDATE Library SET LastScan = @LastScan WHERE Id = @Id",
|
|
new { library.LastScan, library.Id }).ToUnit();
|
|
}
|
|
|
|
public async Task<Unit> UpdateLastScan(LibraryPath libraryPath)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
return await dbContext.Connection.ExecuteAsync(
|
|
"UPDATE LibraryPath SET LastScan = @LastScan WHERE Id = @Id",
|
|
new { libraryPath.LastScan, libraryPath.Id }).ToUnit();
|
|
}
|
|
|
|
public async Task<List<LibraryPath>> GetLocalPaths(int libraryId)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
return await dbContext.LocalLibraries
|
|
.Include(l => l.Paths)
|
|
.OrderBy(l => l.Id)
|
|
.SingleOrDefaultAsync(l => l.Id == libraryId)
|
|
.Map(Optional)
|
|
.Match(l => l.Paths, () => new List<LibraryPath>());
|
|
}
|
|
|
|
public async Task<int> CountMediaItemsByPath(int libraryPathId)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
return await dbContext.Connection.QuerySingleAsync<int>(
|
|
@"SELECT COUNT(*) FROM MediaItem WHERE LibraryPathId = @LibraryPathId",
|
|
new { LibraryPathId = libraryPathId });
|
|
}
|
|
|
|
public async Task SetEtag(
|
|
LibraryPath libraryPath,
|
|
Option<LibraryFolder> knownFolder,
|
|
string path,
|
|
string etag)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
|
|
foreach (LibraryFolder folder in knownFolder)
|
|
{
|
|
await dbContext.Connection.ExecuteAsync(
|
|
"UPDATE LibraryFolder SET Etag = @Etag WHERE Id = @Id",
|
|
new { folder.Id, Etag = etag });
|
|
}
|
|
|
|
if (knownFolder.IsNone)
|
|
{
|
|
await dbContext.LibraryFolders.AddAsync(
|
|
new LibraryFolder
|
|
{
|
|
Path = path,
|
|
Etag = etag,
|
|
LibraryPathId = libraryPath.Id
|
|
});
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task CleanEtagsForLibraryPath(LibraryPath libraryPath)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
|
|
IOrderedEnumerable<LibraryFolder> orderedFolders = libraryPath.LibraryFolders
|
|
.Where(f => !fileSystem.Directory.Exists(f.Path))
|
|
.OrderByDescending(lp => lp.Path.Length);
|
|
|
|
foreach (LibraryFolder folder in orderedFolders)
|
|
{
|
|
await dbContext.Connection.ExecuteAsync(
|
|
"""
|
|
DELETE FROM LibraryFolder WHERE Id = @LibraryFolderId
|
|
AND NOT EXISTS (SELECT Id FROM MediaFile WHERE LibraryFolderId = @LibraryFolderId)
|
|
AND NOT EXISTS (SELECT Id FROM LibraryFolder WHERE ParentId = @LibraryFolderId)
|
|
""",
|
|
new { LibraryFolderId = folder.Id });
|
|
}
|
|
}
|
|
|
|
public async Task<Option<int>> GetParentFolderId(
|
|
LibraryPath libraryPath,
|
|
string folder,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
DirectoryInfo parent = new DirectoryInfo(folder).Parent;
|
|
if (parent is null)
|
|
{
|
|
return Option<int>.None;
|
|
}
|
|
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
return await dbContext.LibraryFolders
|
|
.AsNoTracking()
|
|
.Filter(lf => lf.LibraryPathId == libraryPath.Id)
|
|
.SelectOneAsync(lf => lf.Path, lf => lf.Path == parent.FullName, cancellationToken)
|
|
.MapT(lf => lf.Id);
|
|
}
|
|
|
|
public async Task<LibraryFolder> GetOrAddFolder(
|
|
LibraryPath libraryPath,
|
|
Option<int> maybeParentFolder,
|
|
string folder)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
|
|
// load from db or create new folder
|
|
LibraryFolder knownFolder = await libraryPath.LibraryFolders
|
|
.Filter(f => f.Path == folder && f.LibraryPathId == libraryPath.Id)
|
|
.HeadOrNone()
|
|
.IfNoneAsync(CreateNewFolder(libraryPath, maybeParentFolder, folder));
|
|
|
|
// update parent folder if not present
|
|
foreach (int parentFolder in maybeParentFolder)
|
|
{
|
|
if (knownFolder.ParentId != parentFolder)
|
|
{
|
|
knownFolder.ParentId = parentFolder;
|
|
|
|
await dbContext.Connection.ExecuteAsync(
|
|
"UPDATE LibraryFolder SET ParentId = @ParentId WHERE Id = @Id",
|
|
new { ParentId = parentFolder, knownFolder.Id });
|
|
}
|
|
}
|
|
|
|
// add new folder to library path
|
|
if (knownFolder.Id < 1)
|
|
{
|
|
await dbContext.LibraryFolders.AddAsync(knownFolder);
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
return knownFolder;
|
|
}
|
|
|
|
public async Task UpdateLibraryFolderId(MediaFile mediaFile, int libraryFolderId)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
mediaFile.LibraryFolderId = libraryFolderId;
|
|
await dbContext.Connection.ExecuteAsync(
|
|
"UPDATE MediaFile SET LibraryFolderId = @LibraryFolderId WHERE Id = @Id",
|
|
new { LibraryFolderId = libraryFolderId, mediaFile.Id });
|
|
}
|
|
|
|
public async Task UpdatePath(LibraryPath libraryPath, string normalizedLibraryPath)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync();
|
|
libraryPath.Path = normalizedLibraryPath;
|
|
await dbContext.Connection.ExecuteAsync(
|
|
"UPDATE LibraryPath SET Path = @Path WHERE Id = @Id",
|
|
new { Path = normalizedLibraryPath, libraryPath.Id });
|
|
}
|
|
|
|
private static LibraryFolder CreateNewFolder(LibraryPath libraryPath, Option<int> maybeParentFolder, string folder)
|
|
{
|
|
int? parentId = null;
|
|
foreach (int parentFolder in maybeParentFolder)
|
|
{
|
|
parentId = parentFolder;
|
|
}
|
|
|
|
return new LibraryFolder
|
|
{
|
|
Path = folder,
|
|
Etag = null,
|
|
LibraryPathId = libraryPath.Id,
|
|
ParentId = parentId
|
|
};
|
|
}
|
|
}
|