69 lines
2.4 KiB
C#
69 lines
2.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace OpenArchival.DataAccess;
|
|
|
|
public class FilePathListingProvider : IFilePathListingProvider
|
|
{
|
|
private readonly IDbContextFactory<ApplicationDbContext> _contextFactory;
|
|
private readonly ILogger<FilePathListingProvider> _logger;
|
|
|
|
[SetsRequiredMembers]
|
|
public FilePathListingProvider(IDbContextFactory<ApplicationDbContext> factory, ILogger<FilePathListingProvider> logger)
|
|
{
|
|
_contextFactory = factory;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<FilePathListing?> GetFilePathListingAsync(int id)
|
|
{
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|
return await context.ArtifactFilePaths.Where(f => f.Id == id).FirstOrDefaultAsync();
|
|
}
|
|
|
|
public async Task<FilePathListing?> GetFilePathListingByPathAsync(string path)
|
|
{
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|
return await context.ArtifactFilePaths.Where(f => f.Path == path).FirstOrDefaultAsync();
|
|
}
|
|
|
|
public async Task CreateFilePathListingAsync(FilePathListing filePathListing)
|
|
{
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|
context.ArtifactFilePaths.Add(filePathListing);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateFilePathListingAsync(FilePathListing filePathListing)
|
|
{
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|
context.ArtifactFilePaths.Update(filePathListing);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteFilePathListingAsync(FilePathListing filePathListing)
|
|
{
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|
context.ArtifactFilePaths.Remove(filePathListing);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<bool> DeleteFilePathListingAsync(string originalFileName, string diskPath)
|
|
{
|
|
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|
var listingToDelete = await context.ArtifactFilePaths
|
|
.Where(p => p.OriginalName == originalFileName)
|
|
.Where(p => p.Path == diskPath)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (listingToDelete == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
context.RemoveRange(listingToDelete);
|
|
await context.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
} |