添加用户和Mqtt的仓储文件,添加的对Excel进行导入导出功能,并完成单元测试
This commit is contained in:
75
Data/Repositories/MqttRepository.cs
Normal file
75
Data/Repositories/MqttRepository.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PMSWPF.Data.Entities;
|
||||||
|
|
||||||
|
namespace PMSWPF.Data.Repositories;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Mqtt仓储类,用于操作DbMqtt实体
|
||||||
|
/// </summary>
|
||||||
|
public class MqttRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 根据ID获取Mqtt配置
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">主键ID</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<DbMqtt> GetByIdAsync(int id)
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Queryable<DbMqtt>().In(id).SingleAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有Mqtt配置
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<List<DbMqtt>> GetAllAsync()
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Queryable<DbMqtt>().ToListAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增Mqtt配置
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mqtt">Mqtt实体</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<int> AddAsync(DbMqtt mqtt)
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Insertable(mqtt).ExecuteReturnIdentityAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新Mqtt配置
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mqtt">Mqtt实体</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<int> UpdateAsync(DbMqtt mqtt)
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Updateable(mqtt).ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据ID删除Mqtt配置
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">主键ID</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<int> DeleteAsync(int id)
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Deleteable<DbMqtt>().In(id).ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
75
Data/Repositories/UserRepository.cs
Normal file
75
Data/Repositories/UserRepository.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PMSWPF.Data.Entities;
|
||||||
|
|
||||||
|
namespace PMSWPF.Data.Repositories;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户仓储类,用于操作DbUser实体
|
||||||
|
/// </summary>
|
||||||
|
public class UserRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 根据ID获取用户
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">主键ID</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<DbUser> GetByIdAsync(int id)
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Queryable<DbUser>().In(id).SingleAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有用户
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<List<DbUser>> GetAllAsync()
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Queryable<DbUser>().ToListAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增用户
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="user">用户实体</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<int> AddAsync(DbUser user)
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Insertable(user).ExecuteReturnIdentityAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新用户
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="user">用户实体</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<int> UpdateAsync(DbUser user)
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Updateable(user).ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据ID删除用户
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">主键ID</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<int> DeleteAsync(int id)
|
||||||
|
{
|
||||||
|
using (var _db = DbContext.GetInstance())
|
||||||
|
{
|
||||||
|
return await _db.Deleteable<DbUser>().In(id).ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,16 @@
|
|||||||
namespace PMSWPF.Extensions;
|
namespace PMSWPF.Extensions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务扩展类,提供异步任务的扩展方法。
|
||||||
|
/// </summary>
|
||||||
public static class TaskExtensions
|
public static class TaskExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 等待一个没有返回值的 Task 完成,并提供错误处理和完成时的回调。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="task">要等待的 Task。</param>
|
||||||
|
/// <param name="onError">发生异常时的回调函数。</param>
|
||||||
|
/// <param name="onComplete">任务成功完成时的回调函数。</param>
|
||||||
public static async Task Await(this Task task, Action<Exception> onError = null, Action onComplete = null)
|
public static async Task Await(this Task task, Action<Exception> onError = null, Action onComplete = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -14,6 +23,14 @@ public static class TaskExtensions
|
|||||||
onError?.Invoke(e);
|
onError?.Invoke(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 等待一个有返回值的 Task 完成,并提供错误处理和完成时的回调。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">Task 的返回结果类型。</typeparam>
|
||||||
|
/// <param name="task">要等待的 Task。</param>
|
||||||
|
/// <param name="onError">发生异常时的回调函数。</param>
|
||||||
|
/// <param name="onComplete">任务成功完成时的回调函数,接收任务的返回结果。</param>
|
||||||
public static async Task Await<T>(this Task<T> task, Action<Exception> onError = null, Action<T> onComplete = null)
|
public static async Task Await<T>(this Task<T> task, Action<Exception> onError = null, Action<T> onComplete = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
113
PMSWPF.Tests/ExcelHelperTests.cs
Normal file
113
PMSWPF.Tests/ExcelHelperTests.cs
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using PMSWPF.Helper;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace PMSWPF.Tests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class ExcelHelperTests
|
||||||
|
{
|
||||||
|
private string _testFilePath;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
// Create a temporary file for testing
|
||||||
|
// _testFilePath = Path.Combine(Path.GetTempPath(), "test.xlsx");
|
||||||
|
_testFilePath = "e:/test.xlsx";
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
// Clean up the temporary file
|
||||||
|
if (File.Exists(_testFilePath))
|
||||||
|
{
|
||||||
|
File.Delete(_testFilePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ExportToExcel_WithListOfObjects_CreatesFile()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var data = new List<TestData>
|
||||||
|
{
|
||||||
|
new TestData { Id = 1, Name = "Test1" },
|
||||||
|
new TestData { Id = 2, Name = "Test2" }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
ExcelHelper.ExportToExcel(data, _testFilePath);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsTrue(File.Exists(_testFilePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ExportToExcel_WithDataTable_CreatesFile()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var dataTable = new DataTable();
|
||||||
|
dataTable.Columns.Add("Id", typeof(int));
|
||||||
|
dataTable.Columns.Add("Name", typeof(string));
|
||||||
|
dataTable.Rows.Add(1, "Test1");
|
||||||
|
dataTable.Rows.Add(2, "Test2");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
ExcelHelper.ExportToExcel(dataTable, _testFilePath);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsTrue(File.Exists(_testFilePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ImportFromExcel_ToDataTable_ReturnsCorrectData()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var dataTable = new DataTable();
|
||||||
|
dataTable.Columns.Add("Id", typeof(int));
|
||||||
|
dataTable.Columns.Add("Name", typeof(string));
|
||||||
|
dataTable.Rows.Add(1, "Test1");
|
||||||
|
dataTable.Rows.Add(2, "Test2");
|
||||||
|
ExcelHelper.ExportToExcel(dataTable, _testFilePath);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = ExcelHelper.ImportFromExcel(_testFilePath);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(2, result.Rows.Count);
|
||||||
|
Assert.AreEqual("1", result.Rows[0]["Id"]);
|
||||||
|
Assert.AreEqual("Test1", result.Rows[0]["Name"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ImportFromExcel_ToListOfObjects_ReturnsCorrectData()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var data = new List<TestData>
|
||||||
|
{
|
||||||
|
new TestData { Id = 1, Name = "Test1" },
|
||||||
|
new TestData { Id = 2, Name = "Test2" }
|
||||||
|
};
|
||||||
|
ExcelHelper.ExportToExcel(data, _testFilePath);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = ExcelHelper.ImportFromExcel<TestData>(_testFilePath);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(2, result.Count);
|
||||||
|
Assert.AreEqual(1, result[0].Id);
|
||||||
|
Assert.AreEqual("Test1", result[0].Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestData
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
PMSWPF.Tests/PMSWPF.Tests.csproj
Normal file
28
PMSWPF.Tests/PMSWPF.Tests.csproj
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0-windows</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||||
|
<PackageReference Include="NUnit" Version="3.14.0" />
|
||||||
|
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
||||||
|
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Using Include="NUnit.Framework" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\PMSWPF.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
<TargetFramework>net8.0-windows</TargetFramework>
|
<TargetFramework>net8.0-windows</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
@@ -9,17 +10,21 @@
|
|||||||
<ApplicationIcon>AppIcon2.ico</ApplicationIcon>
|
<ApplicationIcon>AppIcon2.ico</ApplicationIcon>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0"/>
|
<Compile Remove="PMSWPF.Tests\**\*.cs" />
|
||||||
<PackageReference Include="HandyControl" Version="3.5.1"/>
|
</ItemGroup>
|
||||||
<PackageReference Include="iNKORE.UI.WPF.Modern" Version="0.10.0"/>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5"/>
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5"/>
|
<PackageReference Include="HandyControl" Version="3.5.1" />
|
||||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135"/>
|
<PackageReference Include="iNKORE.UI.WPF.Modern" Version="0.10.0" />
|
||||||
<PackageReference Include="MySql.Data" Version="9.3.0"/>
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
|
||||||
<PackageReference Include="NLog" Version="6.0.0"/>
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
|
||||||
<PackageReference Include="NLog.Database" Version="6.0.0"/>
|
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
|
||||||
<PackageReference Include="SqlSugarCore.MySql" Version="5.1.4.178"/>
|
<PackageReference Include="MySql.Data" Version="9.3.0" />
|
||||||
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.193"/>
|
<PackageReference Include="NLog" Version="6.0.0" />
|
||||||
|
<PackageReference Include="NLog.Database" Version="6.0.0" />
|
||||||
|
<PackageReference Include="NPOI" Version="2.7.4" />
|
||||||
|
<PackageReference Include="SqlSugarCore.MySql" Version="5.1.4.178" />
|
||||||
|
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.193" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Update="Config\nlog.config">
|
<None Update="Config\nlog.config">
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ VisualStudioVersion = 17.12.35728.132 d17.12
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PMSWPF", "PMSWPF.csproj", "{CD3529C9-218C-41EE-B64B-A884DC56E21E}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PMSWPF", "PMSWPF.csproj", "{CD3529C9-218C-41EE-B64B-A884DC56E21E}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PMSWPF.Tests", "PMSWPF.Tests\PMSWPF.Tests.csproj", "{5EEE2682-FB7B-4E77-AB22-1B7C4E47F53A}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -15,6 +17,10 @@ Global
|
|||||||
{CD3529C9-218C-41EE-B64B-A884DC56E21E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{CD3529C9-218C-41EE-B64B-A884DC56E21E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{CD3529C9-218C-41EE-B64B-A884DC56E21E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{CD3529C9-218C-41EE-B64B-A884DC56E21E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{CD3529C9-218C-41EE-B64B-A884DC56E21E}.Release|Any CPU.Build.0 = Release|Any CPU
|
{CD3529C9-218C-41EE-B64B-A884DC56E21E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{5EEE2682-FB7B-4E77-AB22-1B7C4E47F53A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{5EEE2682-FB7B-4E77-AB22-1B7C4E47F53A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{5EEE2682-FB7B-4E77-AB22-1B7C4E47F53A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{5EEE2682-FB7B-4E77-AB22-1B7C4E47F53A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user