71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using PageManager.Api.Api.Dtos;
|
|
using PageManager.Api.Data;
|
|
using PageManager.Api.Data.Models;
|
|
|
|
namespace PageManager.Api.Services;
|
|
|
|
public class ImportService(AppDbContext db) : IImportService
|
|
{
|
|
public async Task<IEnumerable<ImportSourceDto>> GetSourcesAsync() =>
|
|
(await db.ImportSources.ToListAsync()).Select(ToDto);
|
|
|
|
public async Task<ImportSourceDto?> UpdateSourceAsync(string id, bool enabled)
|
|
{
|
|
var source = await db.ImportSources.FindAsync(id);
|
|
if (source is null) return null;
|
|
|
|
source.Enabled = enabled;
|
|
await db.SaveChangesAsync();
|
|
return ToDto(source);
|
|
}
|
|
|
|
public async Task<IEnumerable<QueueItemDto>> GetQueueAsync() =>
|
|
(await db.ImportQueueItems
|
|
.OrderByDescending(i => i.Status == QueueItemStatus.Downloading)
|
|
.ThenByDescending(i => i.Status == QueueItemStatus.Queued)
|
|
.ToListAsync())
|
|
.Select(ToDto);
|
|
|
|
public async Task<bool> RemoveQueueItemAsync(string id)
|
|
{
|
|
var item = await db.ImportQueueItems.FindAsync(id);
|
|
if (item is null) return false;
|
|
|
|
db.ImportQueueItems.Remove(item);
|
|
await db.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> RetryQueueItemAsync(string id)
|
|
{
|
|
var item = await db.ImportQueueItems.FindAsync(id);
|
|
if (item is null || item.Status != QueueItemStatus.Failed) return false;
|
|
|
|
item.Status = QueueItemStatus.Queued;
|
|
item.Error = null;
|
|
await db.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
private static ImportSourceDto ToDto(ImportSource s) => new()
|
|
{
|
|
Id = s.Id,
|
|
Name = s.Name,
|
|
Type = s.Type.ToString().ToLowerInvariant(),
|
|
Path = s.Path,
|
|
Enabled = s.Enabled,
|
|
};
|
|
|
|
private static QueueItemDto ToDto(ImportQueueItem i) => new()
|
|
{
|
|
Id = i.Id,
|
|
Filename = i.Filename,
|
|
SizeBytes = i.SizeBytes,
|
|
DownloadedBytes = i.DownloadedBytes,
|
|
Status = i.Status.ToString().ToLowerInvariant(),
|
|
Source = i.Source,
|
|
Error = i.Error,
|
|
};
|
|
}
|