72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
using DownloadManager.Core.Data.Repositories;
|
|
using DownloadManager.Core.Engine;
|
|
using DownloadManager.Core.Models;
|
|
using DownloadManager.Core.Queue;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DownloadManager.Core.Services;
|
|
|
|
public interface IDownloadService
|
|
{
|
|
Task<IEnumerable<DownloadItem>> GetAllDownloadsAsync();
|
|
Task AddDownloadAsync(DownloadItem item);
|
|
Task UpdateDownloadAsync(DownloadItem item);
|
|
Task PauseDownloadAsync(Guid id);
|
|
Task ResumeDownloadAsync(Guid id);
|
|
Task DeleteDownloadAsync(Guid id);
|
|
}
|
|
|
|
public class DownloadService : IDownloadService
|
|
{
|
|
private readonly IDownloadRepository _repository;
|
|
private readonly DownloadEngine _engine;
|
|
private readonly DownloadQueue _queue;
|
|
|
|
public DownloadService(IDownloadRepository repository, DownloadEngine engine, DownloadQueue queue)
|
|
{
|
|
_repository = repository;
|
|
_engine = engine;
|
|
_queue = queue;
|
|
}
|
|
|
|
public Task<IEnumerable<DownloadItem>> GetAllDownloadsAsync()
|
|
=> _repository.GetAllAsync();
|
|
|
|
public async Task AddDownloadAsync(DownloadItem item)
|
|
{
|
|
await _repository.AddAsync(item);
|
|
_queue.Enqueue(item);
|
|
}
|
|
|
|
public async Task UpdateDownloadAsync(DownloadItem item)
|
|
{
|
|
await _repository.UpdateAsync(item);
|
|
}
|
|
|
|
public Task PauseDownloadAsync(Guid id)
|
|
{
|
|
_engine.CancelDownload(id);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task ResumeDownloadAsync(Guid id)
|
|
{
|
|
var item = await _repository.GetByIdAsync(id);
|
|
if (item != null && item.Status != Enums.DownloadStatus.Completed)
|
|
{
|
|
item.Status = Enums.DownloadStatus.Queued;
|
|
await _repository.UpdateAsync(item);
|
|
_queue.Enqueue(item);
|
|
}
|
|
}
|
|
|
|
|
|
public async Task DeleteDownloadAsync(Guid id)
|
|
{
|
|
await _repository.DeleteAsync(id);
|
|
_engine.CancelDownload(id);
|
|
}
|
|
}
|