ilk commit
This commit is contained in:
71
src/DownloadManager.Core/Services/DownloadService.cs
Normal file
71
src/DownloadManager.Core/Services/DownloadService.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user