From fd073b4762540db3f20e3e8d81304061d591fd5e Mon Sep 17 00:00:00 2001 From: Cucumberrbob <128094686+Cucumberrbob@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:57:29 +0000 Subject: [PATCH] Add interfaces for `TorrentData`, `Downloads`, `Process`, `ProcessFactory` Making them interfaces lets us dependency inject them, allowing a given class to be tested. --- server/RdtClient.Data/Data/ITorrentData.cs | 28 ++++++++++++ server/RdtClient.Data/Data/TorrentData.cs | 2 +- .../RdtClient.Service/Services/Downloads.cs | 2 +- .../RdtClient.Service/Services/IDownloads.cs | 24 ++++++++++ server/RdtClient.Service/Wrappers/IProcess.cs | 16 +++++++ .../Wrappers/IProcessFactory.cs | 6 +++ server/RdtClient.Service/Wrappers/Process.cs | 44 +++++++++++++++++++ .../Wrappers/ProcessFactory.cs | 9 ++++ 8 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 server/RdtClient.Data/Data/ITorrentData.cs create mode 100644 server/RdtClient.Service/Services/IDownloads.cs create mode 100644 server/RdtClient.Service/Wrappers/IProcess.cs create mode 100644 server/RdtClient.Service/Wrappers/IProcessFactory.cs create mode 100644 server/RdtClient.Service/Wrappers/Process.cs create mode 100644 server/RdtClient.Service/Wrappers/ProcessFactory.cs diff --git a/server/RdtClient.Data/Data/ITorrentData.cs b/server/RdtClient.Data/Data/ITorrentData.cs new file mode 100644 index 0000000..4bbaa24 --- /dev/null +++ b/server/RdtClient.Data/Data/ITorrentData.cs @@ -0,0 +1,28 @@ +using RdtClient.Data.Enums; +using RdtClient.Data.Models.Data; + +namespace RdtClient.Data.Data; + +public interface ITorrentData +{ + Task> Get(); + Task GetById(Guid torrentId); + Task GetByHash(String hash); + + Task Add(String rdId, + String hash, + String? fileOrMagnetContents, + Boolean isFile, + DownloadClient downloadClient, + Torrent torrent); + + Task UpdateRdData(Torrent torrent); + Task Update(Torrent torrent); + Task UpdateCategory(Guid torrentId, String? category); + Task UpdateComplete(Guid torrentId, String? error, DateTimeOffset? datetime, Boolean retry); + Task UpdateFilesSelected(Guid torrentId, DateTimeOffset datetime); + Task UpdatePriority(Guid torrentId, Int32? priority); + Task UpdateRetry(Guid torrentId, DateTimeOffset? dateTime, Int32 retryCount); + Task UpdateError(Guid torrentId, String error); + Task Delete(Guid torrentId); +} diff --git a/server/RdtClient.Data/Data/TorrentData.cs b/server/RdtClient.Data/Data/TorrentData.cs index 34f43a3..4578348 100644 --- a/server/RdtClient.Data/Data/TorrentData.cs +++ b/server/RdtClient.Data/Data/TorrentData.cs @@ -4,7 +4,7 @@ using RdtClient.Data.Models.Data; namespace RdtClient.Data.Data; -public class TorrentData(DataContext dataContext) +public class TorrentData(DataContext dataContext) : ITorrentData { private static IList? _torrentCache; diff --git a/server/RdtClient.Service/Services/Downloads.cs b/server/RdtClient.Service/Services/Downloads.cs index 020b36d..925c8e0 100644 --- a/server/RdtClient.Service/Services/Downloads.cs +++ b/server/RdtClient.Service/Services/Downloads.cs @@ -3,7 +3,7 @@ using Download = RdtClient.Data.Models.Data.Download; namespace RdtClient.Service.Services; -public class Downloads(DownloadData downloadData) +public class Downloads(DownloadData downloadData) : IDownloads { public async Task> GetForTorrent(Guid torrentId) { diff --git a/server/RdtClient.Service/Services/IDownloads.cs b/server/RdtClient.Service/Services/IDownloads.cs new file mode 100644 index 0000000..d0550b9 --- /dev/null +++ b/server/RdtClient.Service/Services/IDownloads.cs @@ -0,0 +1,24 @@ +using RdtClient.Data.Models.Data; + +namespace RdtClient.Service.Services; + +public interface IDownloads +{ + Task> GetForTorrent(Guid torrentId); + Task GetById(Guid downloadId); + Task Get(Guid torrentId, String path); + Task Add(Guid torrentId, String path); + Task UpdateUnrestrictedLink(Guid downloadId, String unrestrictedLink); + Task UpdateFileName(Guid downloadId, String fileName); + Task UpdateDownloadStarted(Guid downloadId, DateTimeOffset? dateTime); + Task UpdateDownloadFinished(Guid downloadId, DateTimeOffset? dateTime); + Task UpdateUnpackingQueued(Guid downloadId, DateTimeOffset? dateTime); + Task UpdateUnpackingStarted(Guid downloadId, DateTimeOffset? dateTime); + Task UpdateUnpackingFinished(Guid downloadId, DateTimeOffset? dateTime); + Task UpdateCompleted(Guid downloadId, DateTimeOffset? dateTime); + Task UpdateError(Guid downloadId, String? error); + Task UpdateRetryCount(Guid downloadId, Int32 retryCount); + Task UpdateRemoteId(Guid downloadId, String remoteId); + Task DeleteForTorrent(Guid torrentId); + Task Reset(Guid downloadId); +} diff --git a/server/RdtClient.Service/Wrappers/IProcess.cs b/server/RdtClient.Service/Wrappers/IProcess.cs new file mode 100644 index 0000000..84f4cfe --- /dev/null +++ b/server/RdtClient.Service/Wrappers/IProcess.cs @@ -0,0 +1,16 @@ +using System.Diagnostics; + +namespace RdtClient.Service.Wrappers; + +public interface IProcess : IDisposable +{ + event EventHandler? OutputDataReceived; + event EventHandler? ErrorDataReceived; + + public ProcessStartInfo StartInfo { get; set; } + + void BeginOutputReadLine(); + void BeginErrorReadLine(); + Boolean WaitForExit(Int32 milliseconds); + void Start(); +} diff --git a/server/RdtClient.Service/Wrappers/IProcessFactory.cs b/server/RdtClient.Service/Wrappers/IProcessFactory.cs new file mode 100644 index 0000000..cb2b22a --- /dev/null +++ b/server/RdtClient.Service/Wrappers/IProcessFactory.cs @@ -0,0 +1,6 @@ +namespace RdtClient.Service.Wrappers; + +public interface IProcessFactory +{ + public IProcess NewProcess(); +} diff --git a/server/RdtClient.Service/Wrappers/Process.cs b/server/RdtClient.Service/Wrappers/Process.cs new file mode 100644 index 0000000..fa0e211 --- /dev/null +++ b/server/RdtClient.Service/Wrappers/Process.cs @@ -0,0 +1,44 @@ +using System.Diagnostics; + +namespace RdtClient.Service.Wrappers; + +public class Process : IProcess +{ + private readonly System.Diagnostics.Process _process = new(); + + public ProcessStartInfo StartInfo + { + get => _process.StartInfo; + set => _process.StartInfo = value; + } + + public event EventHandler? OutputDataReceived; + public event EventHandler? ErrorDataReceived; + + public void Dispose() + { + _process.Dispose(); + } + + public void BeginOutputReadLine() + { + _process.OutputDataReceived += (sender, args) => OutputDataReceived?.Invoke(sender, args.Data); + _process.BeginOutputReadLine(); + } + + public void BeginErrorReadLine() + { + _process.ErrorDataReceived += (sender, args) => ErrorDataReceived?.Invoke(sender, args.Data); + _process.BeginErrorReadLine(); + } + + public Boolean WaitForExit(Int32 milliseconds) + { + return _process.WaitForExit(milliseconds); + } + + public void Start() + { + _process.Start(); + } +} diff --git a/server/RdtClient.Service/Wrappers/ProcessFactory.cs b/server/RdtClient.Service/Wrappers/ProcessFactory.cs new file mode 100644 index 0000000..ba8814f --- /dev/null +++ b/server/RdtClient.Service/Wrappers/ProcessFactory.cs @@ -0,0 +1,9 @@ +namespace RdtClient.Service.Wrappers; + +public class ProcessFactory: IProcessFactory +{ + public IProcess NewProcess() + { + return new Process(); + } +}